src/Core/EventSubscriber/PasswordResetRequestSubscriber.php line 61

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Core\EventSubscriber;
  4. use App\Core\Event\UserPasswordResetRequestEvent;
  5. use App\Core\Model\UserInterface;
  6. use App\Core\Service\TokenGeneratorInterface;
  7. use App\Core\Service\UserServiceInterface;
  8. use JetBrains\PhpStorm\ArrayShape;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\Mime\Email;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Twig\Environment;
  15. class PasswordResetRequestSubscriber implements EventSubscriberInterface
  16. {
  17.     private UserServiceInterface $userService;
  18.     private TokenGeneratorInterface $tokenGenerator;
  19.     private UrlGeneratorInterface $urlGenerator;
  20.     private Environment $twig;
  21.     private MailerInterface $mailer;
  22.     private TranslatorInterface $translator;
  23.     private int $forgottenPasswordTokenLifetimeHours;
  24.     public function __construct(
  25.         UserServiceInterface $userService,
  26.         MailerInterface $mailer,
  27.         TokenGeneratorInterface $tokenGenerator,
  28.         UrlGeneratorInterface $urlGenerator,
  29.         Environment $twig,
  30.         TranslatorInterface $translator,
  31.         int $forgottenPasswordTokenLifetimeHours
  32.     ) {
  33.         $this->userService $userService;
  34.         $this->tokenGenerator $tokenGenerator;
  35.         $this->urlGenerator $urlGenerator;
  36.         $this->twig $twig;
  37.         $this->mailer $mailer;
  38.         $this->translator $translator;
  39.         $this->forgottenPasswordTokenLifetimeHours $forgottenPasswordTokenLifetimeHours;
  40.     }
  41.     #[ArrayShape([UserPasswordResetRequestEvent::class => 'string'])]
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             UserPasswordResetRequestEvent::class => 'onPasswordResetRequest',
  46.         ];
  47.     }
  48.     public function onPasswordResetRequest(UserPasswordResetRequestEvent $resetRequestEvent)
  49.     {
  50.         $user $resetRequestEvent->getUser();
  51.         if (null === $user->getConfirmationToken()) {
  52.             $user->setPasswordResetToken($this->tokenGenerator->generateToken());
  53.             $this->userService->save($user);
  54.             $this->sendPasswordResetEmail($user);
  55.         }
  56.     }
  57.     public function sendPasswordResetEmail(UserInterface $user)
  58.     {
  59.         $templateHtml $this->twig->load('packages/core/email/password_reset.html.twig');
  60.         $templateTxt $this->twig->load('packages/core/email/password_reset.txt.twig');
  61.         $passwordResetLink $this->urlGenerator->generate('app_forgotten_password_reset', ['token' => $user->getPasswordResetToken()], UrlGeneratorInterface::ABSOLUTE_URL);
  62.         $message = (new Email())
  63.             ->from('no-reply@tecla.no')
  64.             ->to($user->getEmail())
  65.             ->subject($this->translator->trans('title.password_reset', [], 'core@messages'))
  66.             ->html($templateHtml->render([
  67.                 'password_reset_url' => $passwordResetLink,
  68.                 'contact' => $user->getContact(),
  69.                 'password_token_lifetime' => $this->forgottenPasswordTokenLifetimeHours,
  70.             ]))
  71.             ->text($templateTxt->render([
  72.                 'password_reset_url' => $passwordResetLink,
  73.                 'contact' => $user->getContact(),
  74.             ]))
  75.         ;
  76.         $this->mailer->send($message);
  77.     }
  78. }