src/EventListener/WelcomeSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\EventListener\EntityManager;
  4. use App\EventListener\TokenGenerator;
  5. use App\Event\WelcomeEvent;
  6. use App\Model\User\User;
  7. use App\Service\MailerService;
  8. use DateTime;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use FOS\UserBundle\Util\TokenGeneratorInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class WelcomeSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var TokenGenerator
  16.      */
  17.     private $tokenGenerator;
  18.     /**
  19.      * @var MailerService
  20.      */
  21.     private $mailerService;
  22.     /**
  23.      * @var EntityManager
  24.      */
  25.     private $em;
  26.     /**
  27.      * @param TokenGenerator $tokenGenerator
  28.      * @param EntityManager $em
  29.      */
  30.     public function __construct(TokenGeneratorInterface $tokenGeneratorMailerService $mailerEntityManagerInterface $em)
  31.     {
  32.         $this->tokenGenerator $tokenGenerator;
  33.         $this->mailerService  $mailer;
  34.         $this->em             $em;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             WelcomeEvent::USER_CREATION                  => [ 'onUserCreation' ],
  40.             WelcomeEvent::USER_CREATED_USER_NOTIFY_AGAIN => [ 'onUserFailsToClickOnPasswordRegistrationLinkInTime' ]
  41.         ];
  42.     }
  43.     public function onUserFailsToClickOnPasswordRegistrationLinkInTime(WelcomeEvent $event)
  44.     {
  45.         $newUser $event->getUser();
  46.         if ($newUser instanceof User) {
  47.             // Set a new token and reset the password request date at now
  48.             $newUser->getEntity()->setPasswordRequestedAt(new DateTime());
  49.             $newUser->getEntity()->setConfirmationToken($this->tokenGenerator->generateToken());
  50.             $this->em->flush();
  51.             // Send another mail
  52.             $this->mailerService->sendNewAccountWelcomingLink($newUser->getEntity(), false);
  53.         }
  54.     }
  55.     public function onUserCreation(WelcomeEvent $event)
  56.     {
  57.         $newUser $event->getUser();
  58.         if ($newUser instanceof User) {
  59.             /**
  60.              * @var \App\Entity\User $entity
  61.              */
  62.             $entity $newUser->getEntity();
  63.             // Sets a token to the newly created user
  64.             if (null === $entity->getConfirmationToken()) {
  65.                 $entity->setPasswordRequestedAt(new DateTime());
  66.                 $entity->setConfirmationToken($this->tokenGenerator->generateToken());
  67.                 $this->em->flush();
  68.             }
  69.             // Send a mail with instructions to change their password
  70.             $this->mailerService->sendNewAccountWelcomingLink($entity);
  71.             $shops        $newUser->getShops();
  72.             $salesPersons = [];
  73.             foreach ($shops as $shop) {
  74.                 $salesPersons += $shop->getAllAssociatedSalesPersons();
  75.             }
  76.             // Send a mail to the salespersons attached to that user through his/her shops
  77.             $this->mailerService->sendNewAccountNotificationToSalesPersons($entity$salesPersons);
  78.         }
  79.     }
  80. }