vendor/boldr/users-bundle/src/EventSubscriber/UserEventSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace Boldr\Cms\UsersBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Boldr\Cms\UsersBundle\EmailFactory;
  5. use Boldr\Cms\UsersBundle\Event\UserCreatedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. class UserEventSubscriber implements EventSubscriberInterface
  9. {
  10.     private MailerInterface $mailer;
  11.     private EmailFactory $emailFactory;
  12.     private EntityManagerInterface $em;
  13.     public function __construct(
  14.         MailerInterface $mailer,
  15.         EmailFactory $emailFactory,
  16.         EntityManagerInterface $em
  17.     )
  18.     {
  19.         $this->mailer $mailer;
  20.         $this->emailFactory $emailFactory;
  21.         $this->em $em;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             UserCreatedEvent::class => [
  27.                 ['onUserCreated'10]
  28.             ]
  29.         ];
  30.     }
  31.     public function onUserCreated(UserCreatedEvent $event)
  32.     {
  33.         $user $event->getUser();
  34.         $confirmEmailAddressToken bin2hex(random_bytes(32));
  35.         $user->setConfirmEmailAddressToken($confirmEmailAddressToken);
  36.         $this->em->flush();
  37.         if ($event->getSendWelcomeEmail())
  38.         {
  39.             // try
  40.             // {
  41.                 $this->mailer->send($this->emailFactory->createWelcomeEmail($user));
  42.             // }
  43.             // catch (\Exception $ex)
  44.             // {
  45.                 // could not send email
  46.             // }
  47.         }
  48.     }
  49. }