src/EventSubscriber/Mailer/UserResetPasswordSendTokenSubscriber.php line 56

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\UserResetPasswordSendTokenEvent;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Mailer\MailerInterface;
  18. use Symfony\Component\Mime\Address;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. use Twig\Environment;
  21. final class UserResetPasswordSendTokenSubscriber implements EventSubscriberInterface
  22. {
  23.     private string $senderEmail;
  24.     private MailerInterface $mailer;
  25.     private EntityManagerInterface $entityManager;
  26.     private string $senderName;
  27.     private Environment $twig;
  28.     private UrlGeneratorInterface $urlGenerator;
  29.     public function __construct(EntityManagerInterface $entityManagerMailerInterface $mailerEnvironment $twig,
  30.                                 string $senderEmailstring $senderNameUrlGeneratorInterface $urlGenerator)
  31.     {
  32.         $this->entityManager $entityManager;
  33.         $this->mailer $mailer;
  34.         $this->senderEmail $senderEmail;
  35.         $this->senderName $senderName;
  36.         $this->twig $twig;
  37.         $this->urlGenerator $urlGenerator;
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [UserResetPasswordSendTokenEvent::NAME => 'sendMailToUser'];
  42.     }
  43.     public function sendMailToUser(UserResetPasswordSendTokenEvent $event): void
  44.     {
  45.         $context = [
  46.             'resetToken' => $event->getResetToken(),
  47.             'tokenLifetime' => $event->getTokenLifeTime(),
  48.         ];
  49.         $user $event->getUser();
  50. //        $context['site'] = 'url app front';
  51.         $emailContent $this->twig->render('emails/user/reset_password_send_token.html.twig',
  52.             $context);
  53.         $email = (new TemplatedEmail())
  54.             ->from(new Address($this->senderEmail$this->senderName))
  55.             ->to((string) $user->getEmail())
  56.             ->subject('BikingMan - modification mot de passe')
  57.             ->html($emailContent)
  58.         ;
  59.         $this->mailer->send($email);
  60.     }
  61. }