src/EventSubscriber/SendVerificationEmailEventSubscriber.php line 39

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\SendEmailEvent;
  4. use App\Event\SendVerificationEmailEvent;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class SendVerificationEmailEventSubscriber implements EventSubscriberInterface
  9. {
  10.     private EntityManagerInterface $entityManager;
  11.     private EventDispatcherInterface $eventDispatcher;
  12.     private string $fromEmail;
  13.     private string $fromName;
  14.     public function __construct(EntityManagerInterface $entityManagerEventDispatcherInterface $eventDispatcher,
  15.                             string $fromEmailstring $fromName)
  16.     {
  17.         $this->entityManager $entityManager;
  18.         $this->eventDispatcher $eventDispatcher;
  19.         $this->fromEmail $fromEmail;
  20.         $this->fromName $fromName;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             SendVerificationEmailEvent::NAME => [
  26.                 ['handleVerificationEmail'10]
  27.             ]
  28.         ];
  29.     }
  30.     public function handleVerificationEmail(SendVerificationEmailEvent $event): void
  31.     {
  32.         $user $event->getUser();
  33.         if($event->getAction() == 'register')
  34.         {
  35.             $user->setEmailOtp($this->generateOtp());
  36.             $user->setEmailOtpGeneratedAt(new \DateTimeImmutable('now'));
  37.             $otp $user->getEmailOtp();
  38.         }
  39.         else {
  40.             $user->setResetPasswordOtp($this->generateOtp());
  41.             $user->setResetPasswordOtpGeneratedAt(new \DateTimeImmutable('now'));
  42.             $otp $user->getResetPasswordOtp();
  43.         }
  44.         $this->entityManager->flush();
  45.         $emailParameters = [
  46.             'from' => [
  47.                 'email' => $this->fromEmail,
  48.                 'name' => $this->fromName
  49.             ],
  50.             'to' => [
  51.                 'email' => $user->getEmail()
  52.             ],
  53.             'subject' => 'Verify your email',
  54.             'template' => 'registration/verification_email.html.twig',
  55.             'parameters' => [
  56.                 'emailOtp' => $otp
  57.             ]
  58.         ];
  59.         $emailEvent =  new SendEmailEvent($emailParameters);
  60.         $this->eventDispatcher->dispatch($emailEventSendEmailEvent::NAME);
  61.     }
  62.     public function generateOtp(): string
  63.     {
  64.         $length 6$chars '1234567890';
  65.         $chars_length = (strlen($chars) - 1);
  66.         $string $chars[rand(0$chars_length)];
  67.         for ($i 1$i $length$i strlen($string)) {
  68.             $r $chars[rand(0$chars_length)];
  69.             if ($r != $string[$i 1])
  70.                 $string .= $r;
  71.         }
  72.         return $string;
  73.     }
  74. }