vendor/friendsofsymfony/user-bundle/EventListener/EmailConfirmationListener.php line 52

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\UserBundle\EventListener;
  11. use FOS\UserBundle\Event\FormEvent;
  12. use FOS\UserBundle\FOSUserEvents;
  13. use FOS\UserBundle\Mailer\MailerInterface;
  14. use FOS\UserBundle\Util\TokenGeneratorInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. /**
  20.  * @internal
  21.  * @final
  22.  */
  23. class EmailConfirmationListener implements EventSubscriberInterface
  24. {
  25.     private $mailer;
  26.     private $tokenGenerator;
  27.     private $router;
  28.     private $session;
  29.     /**
  30.      * EmailConfirmationListener constructor.
  31.      */
  32.     public function __construct(MailerInterface $mailerTokenGeneratorInterface $tokenGeneratorUrlGeneratorInterface $routerSessionInterface $session)
  33.     {
  34.         $this->mailer $mailer;
  35.         $this->tokenGenerator $tokenGenerator;
  36.         $this->router $router;
  37.         $this->session $session;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
  43.         ];
  44.     }
  45.     public function onRegistrationSuccess(FormEvent $event)
  46.     {
  47.         /** @var $user \FOS\UserBundle\Model\UserInterface */
  48.         $user $event->getForm()->getData();
  49.         $user->setEnabled(false);
  50.         if (null === $user->getConfirmationToken()) {
  51.             $user->setConfirmationToken($this->tokenGenerator->generateToken());
  52.         }
  53.         $this->mailer->sendConfirmationEmailMessage($user);
  54.         $this->session->set('fos_user_send_confirmation_email/email'$user->getEmail());
  55.         $url $this->router->generate('fos_user_registration_check_email');
  56.         $event->setResponse(new RedirectResponse($url));
  57.     }
  58. }