src/EventSubscriber/Mailer/UserResetPasswordUpdatedSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * Unauthorized copying of this file, via any medium is strictly prohibited
  4.  * Proprietary and confidential.
  5.  *
  6.  * @author Bilel AZRI          <azri.bilel@gmail.com>
  7.  * @author Assma BEN SASSI     <bensassiasma.bws@gmail.com>
  8.  *
  9.  * Bicking man (c) 2019-present.
  10.  */
  11. declare(strict_types=1);
  12. namespace App\EventSubscriber\Mailer;
  13. use App\Event\UserResetPasswordUpdatedEvent;
  14. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Mailer\MailerInterface;
  17. use Symfony\Component\Mime\Address;
  18. use Twig\Environment;
  19. final class UserResetPasswordUpdatedSubscriber implements EventSubscriberInterface
  20. {
  21.     private $senderEmail;
  22.     private $mailer;
  23.     private $twig;
  24.     private $senderName;
  25.     public function __construct(MailerInterface $mailerEnvironment $twigstring $senderEmailstring $senderName)
  26.     {
  27.         $this->mailer $mailer;
  28.         $this->twig $twig;
  29.         $this->senderEmail $senderEmail;
  30.         $this->senderName $senderName;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [UserResetPasswordUpdatedEvent::NAME => 'sendMailToUser'];
  35.     }
  36.     public function sendMailToUser(UserResetPasswordUpdatedEvent $event): void
  37.     {
  38.         $user $event->getUser();
  39.         $emailContent $this->twig->render('emails/user/reset_password_updated.html.twig',
  40.             ['user' => $user]);
  41.         $email = (new TemplatedEmail())
  42.             ->from(new Address($this->senderEmail$this->senderName))
  43.             ->to((string) $user->getEmail())
  44.             ->subject('Password updated!')
  45.             ->html($emailContent)
  46.         ;
  47.         $this->mailer->send($email);
  48.     }
  49. }