src/EventSubscriber/PasswordResetSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of an Innatial Developers development or collaboration.
  4.  *
  5.  * @author Innatial Developers, S.L.
  6.  * @link https://innatial.com
  7.  * @copyright Copyright (c) 2020. Innatial Developers, S.L.
  8.  *
  9.  * All rights are reserved.
  10.  * Copyright (c) 2020. Innatial Developers, S.L.
  11.  */
  12. namespace App\EventSubscriber;
  13. use App\Event\UserForgotPasswordEvent;
  14. use App\Event\UserResetPasswordEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Templating\EngineInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class PasswordResetSubscriber implements EventSubscriberInterface{
  19.     protected $mailer;
  20.     protected $twig;
  21.     protected $translator;
  22.     public function __construct(\Swift_Mailer $mailerEngineInterface $twigTranslatorInterface $translator)
  23.     {
  24.         $this->mailer $mailer;
  25.         $this->twig $twig;
  26.         $this->translator $translator;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             UserForgotPasswordEvent::NAME => 'onUserPasswordForgotEvent',
  32.             UserResetPasswordEvent::NAME => 'onUserPasswordResetEvent',
  33.         ];
  34.     }
  35.     public function onUserPasswordForgotEvent(UserForgotPasswordEvent $event){
  36.         $user $event->getUser();
  37.         $message = (new \Swift_Message($this->translator->trans('auth.mailer.templates.user_forgot_password.subject')))
  38.             ->setTo($user->getEmail())
  39.             ->setBody(
  40.                 $this->twig->render(
  41.                     'mailer/user_forgot_password.html.twig',
  42.                     ['user' => $user]
  43.                 ),
  44.                 'text/html'
  45.             );
  46.         $this->mailer->send($message);
  47.     }
  48.     public function onUserPasswordResetEvent(UserResetPasswordEvent $event){
  49.         $user $event->getUser();
  50.         $message = (new \Swift_Message($this->translator->trans('auth.mailer.templates.user_resetted_password_successfully.subject')))
  51.             ->setTo($user->getEmail())
  52.             ->setBody(
  53.                 $this->twig->render(
  54.                     'mailer/user_reset_password.html.twig',
  55.                     ['user' => $user]
  56.                 ),
  57.                 'text/html'
  58.             );
  59.         $this->mailer->send($message);
  60.     }
  61. }