src/EventSubscriber/UserRegistrationSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Unknown
  5.  * Date: 07/10/2019
  6.  * Time: 14:46
  7.  */
  8. namespace App\EventSubscriber;
  9. use App\Email\Mail;
  10. use App\Entity\Event;
  11. use App\Entity\User;
  12. use App\Security\TokenGenerator;
  13. use App\Events;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\EventDispatcher\GenericEvent;
  17. use Symfony\Component\Validator\Constraints\DateTime;
  18. use Twilio\Exceptions\TwilioException;
  19. class UserRegistrationSubscriber implements EventSubscriberInterface
  20. {
  21.     private $tokenGenerator;
  22.     private $mailer;
  23.     private $entityManager;
  24.     public function __construct(TokenGenerator $tokenGenerator,Mail $mailer,EntityManagerInterface $entityManager)
  25.     {
  26.         $this->tokenGenerator $tokenGenerator;
  27.         $this->mailer $mailer;
  28.         $this->entityManager $entityManager;
  29.     }
  30.     public static function getSubscribedEvents() : array
  31.     {
  32.         return [
  33.             Events::USER_REGISTERED => 'registrationSuccess',
  34.         ];
  35.     }
  36.     /**
  37.      * @param GenericEvent $event
  38.      * @throws \Twig\Error\LoaderError
  39.      * @throws \Twig\Error\RuntimeError
  40.      * @throws \Twig\Error\SyntaxError
  41.      */
  42.     public function registrationSuccess(GenericEvent $event): void
  43.     {
  44.         /** @var User $user */
  45.         $user $event->getSubject();
  46.         if (!$user instanceof User){
  47.             return;
  48.         }
  49.         $user
  50.             ->setConfirmationToken($this->tokenGenerator->getRandomSecureToken())
  51.             ->setConfirmationTokenSentAt(new \DateTime());
  52.         $this->entityManager->persist($user);
  53.         $this->entityManager->flush();
  54.         $this->mailer->sendConfirmationEmail($user);
  55.         if($user->getSmsEnabled() == 1) {
  56.             try {
  57.                 $this->mailer->sendVerificationSms($user);
  58.             } catch (TwilioException $e) {
  59.             }
  60.         }
  61.     }
  62. }