src/EventSubscriber/User/UserCreatePostActionSubscriber.php line 97

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\User;
  3. use App\Constant\EmailConfiguration;
  4. use App\Entity\User\Partenaire;
  5. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Mime\Address;
  11. use Symfony\Component\Mime\Email;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Symfony\Component\Mailer\MailerInterface;
  16. use Symfony\Contracts\EventDispatcher\Event;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. use Twig\Environment;
  19. use App\Service\User\ExpertUserManager;
  20. use App\Events\User\UserEvent;
  21. use App\AppEvents;
  22. //use App\Security\Rights\ModuleRights;
  23. class UserCreatePostActionSubscriber implements EventSubscriberInterface
  24. {
  25.     const PLATFORM 'MyFidelia';
  26.     private $em;
  27.     private $mailer;
  28.     private $token;
  29.     private $template;
  30.     private $router;
  31.     private $usermanager;
  32.     private $dispatcher;
  33.     protected $request;
  34.     public function __construct(
  35.         EntityManagerInterface $em,
  36.         MailerInterface $mailer,
  37.         Environment $template,
  38.         TokenStorageInterface $token,
  39.         RouterInterface $router,
  40.         ExpertUserManager $usermanager,
  41.         EventDispatcherInterface $dispatcher,
  42.         RequestStack $requestStack
  43. )
  44.     {
  45.         $this->em $em;
  46.         $this->mailer $mailer;
  47.         $this->token $token;
  48.         $this->template $template;
  49.         $this->router $router;
  50.         $this->usermanager $usermanager;
  51.         $this->dispatcher $dispatcher;
  52.         $this->request $requestStack->getCurrentRequest();;
  53.     }
  54.     public function onUserCreated(UserEvent $event)
  55.     {
  56.         $user $event->getUser();
  57.         $email = (new TemplatedEmail())
  58.             ->from(new Address(EmailConfiguration::SENDER_EMAILEmailConfiguration::SENDER_NAME))
  59.             ->to($user->getEmail())
  60.             ->subject(EmailConfiguration::SUBJECT_ACCOUNT_ACTIVATION)
  61.             ->context([
  62.                 'user' => $user
  63.             ])
  64.             ->htmlTemplate(EmailConfiguration::ACCOUNT_ACTIVATION_EMAIL);
  65.         $this->dispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use ($email) {
  66.             $this->mailer->send($email);
  67.         });
  68.     }
  69.     public function onPasswordForgotten(UserEvent $event){
  70.         $user $event->getUser();
  71.         $email = (new TemplatedEmail())
  72.             ->from(new Address(EmailConfiguration::SENDER_EMAILEmailConfiguration::SENDER_NAME))
  73.             ->to($user->getEmail())
  74.             ->subject(EmailConfiguration::SUBJECT_PASSWORD_FORGOTTEN)
  75.             ->context([
  76.                 'user' => $user,
  77.                 'code' => $user->getActivationCode()
  78.             ])
  79.             ->htmlTemplate(EmailConfiguration::PASSWORD_FORGOTTEN_EMAIL);
  80.         $this->dispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use ($email) {
  81.             $this->mailer->send($email);
  82.         });
  83.     }
  84.     public function onPasswordReset(UserEvent $event){
  85.         $user $event->getUser();
  86.         $email = (new TemplatedEmail())
  87.             ->from(new Address(EmailConfiguration::SENDER_EMAILEmailConfiguration::SENDER_NAME))
  88.             ->to($user->getEmail())
  89.             ->subject(EmailConfiguration::SUBJECT_PASSWORD_RESET)
  90.             ->context([
  91.                 'user' => $user
  92.             ])
  93.             ->htmlTemplate(EmailConfiguration::PASSWORD_RESET_EMAIL);
  94.         $this->dispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use ($email) {
  95.             $this->mailer->send($email);
  96.         });
  97.     }
  98.     public static function getSubscribedEvents()
  99.     {
  100.         return [
  101.             AppEvents::USER_CREATE_SUCCESS => 'onUserCreated',
  102.             AppEvents::USER_PASSWORD_FORGOTTEN => 'onPasswordForgotten',
  103.             AppEvents::USER_PASSWORD_RESET => 'onPasswordReset'
  104.         ];
  105.     }
  106. }