<?php
namespace Eckinox\SecurityBundle\EventSubscriber;
use Eckinox\SecurityBundle\Event\UserEvent;
use Eckinox\SecurityBundle\Notifier\CreatePasswordLoginLinkNotification;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class UserSubscriber implements EventSubscriberInterface
{
public function __construct(
private RequestStack $requestStack,
private LoginLinkHandlerInterface $loginLinkHandler,
private NotifierInterface $notifier,
private TranslatorInterface $translator
) {
}
public static function getSubscribedEvents(): array
{
return [
UserEvent::CREATED_SUCCESS => [['onUserCreate', 0]],
];
}
public function onUserCreate(UserEvent $event): void
{
$user = $event->getUser();
$recipient = new Recipient($user->getEmail());
$request = $this->requestStack->getCurrentRequest();
$loginLinkDetails = $this->loginLinkHandler->createLoginLink($user, $request);
$notification = new CreatePasswordLoginLinkNotification(
$this->translator,
$loginLinkDetails,
$this->translator->trans('eckinox_security.messages.welcome')
);
$this->notifier->send($notification, $recipient);
}
}