src/Core/EventSubscriber/PasswordResetRequestSubscriber.php line 61
<?phpdeclare(strict_types=1);namespace App\Core\EventSubscriber;use App\Core\Event\UserPasswordResetRequestEvent;use App\Core\Model\UserInterface;use App\Core\Service\TokenGeneratorInterface;use App\Core\Service\UserServiceInterface;use JetBrains\PhpStorm\ArrayShape;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Mailer\MailerInterface;use Symfony\Component\Mime\Email;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Contracts\Translation\TranslatorInterface;use Twig\Environment;class PasswordResetRequestSubscriber implements EventSubscriberInterface{private UserServiceInterface $userService;private TokenGeneratorInterface $tokenGenerator;private UrlGeneratorInterface $urlGenerator;private Environment $twig;private MailerInterface $mailer;private TranslatorInterface $translator;private int $forgottenPasswordTokenLifetimeHours;public function __construct(UserServiceInterface $userService,MailerInterface $mailer,TokenGeneratorInterface $tokenGenerator,UrlGeneratorInterface $urlGenerator,Environment $twig,TranslatorInterface $translator,int $forgottenPasswordTokenLifetimeHours) {$this->userService = $userService;$this->tokenGenerator = $tokenGenerator;$this->urlGenerator = $urlGenerator;$this->twig = $twig;$this->mailer = $mailer;$this->translator = $translator;$this->forgottenPasswordTokenLifetimeHours = $forgottenPasswordTokenLifetimeHours;}#[ArrayShape([UserPasswordResetRequestEvent::class => 'string'])]public static function getSubscribedEvents(): array{return [UserPasswordResetRequestEvent::class => 'onPasswordResetRequest',];}public function onPasswordResetRequest(UserPasswordResetRequestEvent $resetRequestEvent){$user = $resetRequestEvent->getUser();if (null === $user->getConfirmationToken()) {$user->setPasswordResetToken($this->tokenGenerator->generateToken());$this->userService->save($user);$this->sendPasswordResetEmail($user);}}public function sendPasswordResetEmail(UserInterface $user){$templateHtml = $this->twig->load('packages/core/email/password_reset.html.twig');$templateTxt = $this->twig->load('packages/core/email/password_reset.txt.twig');$passwordResetLink = $this->urlGenerator->generate('app_forgotten_password_reset', ['token' => $user->getPasswordResetToken()], UrlGeneratorInterface::ABSOLUTE_URL);$message = (new Email())->from('no-reply@tecla.no')->to($user->getEmail())->subject($this->translator->trans('title.password_reset', [], 'core@messages'))->html($templateHtml->render(['password_reset_url' => $passwordResetLink,'contact' => $user->getContact(),'password_token_lifetime' => $this->forgottenPasswordTokenLifetimeHours,]))->text($templateTxt->render(['password_reset_url' => $passwordResetLink,'contact' => $user->getContact(),]));$this->mailer->send($message);}}