src/Social/UserBundle/EventListener/EmailConfirmationListener.php line 94

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 Social\UserBundle\EventListener;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use FOS\UserBundle\FOSUserEvents;
  13. use Social\FrontendBundle\Entity\BootLiveSchedule;
  14. use Social\InternalBundle\Entity\BootProfile;
  15. use Social\UserBundle\Entity\User;
  16. use FOS\UserBundle\Event\FormEvent;
  17. use FOS\UserBundle\Mailer\MailerInterface;
  18. use FOS\UserBundle\Util\TokenGeneratorInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  22. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  23. /**
  24.  * Class EmailConfirmationListener
  25.  *
  26.  * @package Social\UserBundle\EventListener
  27.  */
  28. class EmailConfirmationListener implements EventSubscriberInterface
  29. {
  30.     /**
  31.      * @var MailerInterface $mailer
  32.      */
  33.     private $mailer;
  34.     /**
  35.      * @var TokenGeneratorInterface $tokenGenerator
  36.      */
  37.     private $tokenGenerator;
  38.     /**
  39.      * @var UrlGeneratorInterface $router
  40.      */
  41.     private $router;
  42.     /**
  43.      * @var SessionInterface $session
  44.      */
  45.     private $session;
  46.     /**
  47.      * @var EntityManagerInterface
  48.      */
  49.     private $em;
  50.     /**
  51.      * EmailConfirmationListener constructor.
  52.      *
  53.      * @param MailerInterface $mailer
  54.      * @param TokenGeneratorInterface $tokenGenerator
  55.      * @param UrlGeneratorInterface $router
  56.      * @param SessionInterface $session
  57.      * @param EntityManagerInterface $entityManager
  58.      */
  59.     public function __construct(
  60.         MailerInterface $mailer,
  61.         TokenGeneratorInterface $tokenGenerator,
  62.         UrlGeneratorInterface $router,
  63.         SessionInterface $session,
  64.         EntityManagerInterface $entityManager
  65.     ) {
  66.         $this->mailer         $mailer;
  67.         $this->tokenGenerator $tokenGenerator;
  68.         $this->router         $router;
  69.         $this->session        $session;
  70.         $this->em $entityManager;
  71.     }
  72.     /**
  73.      * @return string[]
  74.      */
  75.     public static function getSubscribedEvents(): array
  76.     {
  77.         return [
  78.             FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
  79.         ];
  80.     }
  81.     /**
  82.      * @param FormEvent $event
  83.      */
  84.     public function onRegistrationSuccess(FormEvent $event)
  85.     {
  86.         /** @var User $user */
  87.         $user $event->getForm()->getData();
  88.         $user->setEnabled(true);
  89.         $user->setConfirmed(false);
  90.         if (null === $user->getConfirmationToken()) {
  91.             $user->setConfirmationToken($this->tokenGenerator->generateToken());
  92.         }
  93.         $this->mailer->sendConfirmationEmailMessage($user);
  94.     }
  95. }