vendor/eckinox/security-bundle/src/EventSubscriber/UserSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace Eckinox\SecurityBundle\EventSubscriber;
  3. use Eckinox\SecurityBundle\Event\UserEvent;
  4. use Eckinox\SecurityBundle\Notifier\CreatePasswordLoginLinkNotification;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\Notifier\NotifierInterface;
  8. use Symfony\Component\Notifier\Recipient\Recipient;
  9. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. class UserSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private RequestStack $requestStack,
  15.         private LoginLinkHandlerInterface $loginLinkHandler,
  16.         private NotifierInterface $notifier,
  17.         private TranslatorInterface $translator
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             UserEvent::CREATED_SUCCESS => [['onUserCreate'0]],
  24.         ];
  25.     }
  26.     public function onUserCreate(UserEvent $event): void
  27.     {
  28.         $user $event->getUser();
  29.         $recipient = new Recipient($user->getEmail());
  30.         $request $this->requestStack->getCurrentRequest();
  31.         $loginLinkDetails $this->loginLinkHandler->createLoginLink($user$request);
  32.         $notification = new CreatePasswordLoginLinkNotification(
  33.             $this->translator,
  34.             $loginLinkDetails,
  35.             $this->translator->trans('eckinox_security.messages.welcome')
  36.         );
  37.         $this->notifier->send($notification$recipient);
  38.     }
  39. }