<?php
namespace App\EventListener;
use App\Service\MailerService;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class UserCreatedListener implements EventSubscriberInterface
{
private $mailerService;
public function __construct(MailerService $mailerService)
{
$this->mailerService = $mailerService;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['sendRegistrationEmail', EventPriorities::POST_WRITE],
];
}
public function sendRegistrationEmail(ViewEvent $event)
{
$user = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$user instanceof User || Request::METHOD_POST !== $method) {
return;
}
// $userEmail = $user->getEmail(); // Отримати email з вашого об'єкта користувача
// $subject = 'Реєстрація пройшла успішно';
// $body = 'Вітаємо, ви були успішно зареєстровані! Ваш логін: ' . $user->getUsername();
// $this->mailerService->sendRegistrationEmail($userEmail, $subject, $body);
}
}