src/Domain/User/EventSubscriber/UserSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Domain\User\EventSubscriber;
  3. use App\Domain\Application\Service\NotifyService;
  4. use App\Domain\User\Event\UserEvent;
  5. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Mime\Address;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. class UserSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private readonly UrlGeneratorInterface $urlGenerator,
  13.         private readonly NotifyService $notifyService,
  14.     ) {
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             UserEvent::PASSWORD_RESET_ASKED => 'onPasswordResetAsked',
  20.         ];
  21.     }
  22.     public function onPasswordResetAsked(UserEvent $event): void
  23.     {
  24.         $user $event->getUser();
  25.         $token $event->getUserToken();
  26.         $email = (new TemplatedEmail())
  27.             ->to(new Address($user->getEmail(), $user->getFullname()))
  28.             ->subject('RĂ©initialisation du mot de passe')
  29.             ->htmlTemplate('auth/email/password_reset.email.twig')
  30.             ->context([
  31.                 'user' => $user,
  32.                 'reset_link' => $this->urlGenerator->generate('password_reset', [
  33.                     'id' => $user->getId(),
  34.                     'token' => $token->getToken(),
  35.                 ], UrlGeneratorInterface::ABSOLUTE_URL),
  36.             ])
  37.         ;
  38.         $this->notifyService->sendEmail($email);
  39.     }
  40. }