<?php
namespace App\Domain\User\EventSubscriber;
use App\Domain\Application\Service\NotifyService;
use App\Domain\User\Event\UserEvent;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class UserSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly UrlGeneratorInterface $urlGenerator,
private readonly NotifyService $notifyService,
) {
}
public static function getSubscribedEvents()
{
return [
UserEvent::PASSWORD_RESET_ASKED => 'onPasswordResetAsked',
];
}
public function onPasswordResetAsked(UserEvent $event): void
{
$user = $event->getUser();
$token = $event->getUserToken();
$email = (new TemplatedEmail())
->to(new Address($user->getEmail(), $user->getFullname()))
->subject('RĂ©initialisation du mot de passe')
->htmlTemplate('auth/email/password_reset.email.twig')
->context([
'user' => $user,
'reset_link' => $this->urlGenerator->generate('password_reset', [
'id' => $user->getId(),
'token' => $token->getToken(),
], UrlGeneratorInterface::ABSOLUTE_URL),
])
;
$this->notifyService->sendEmail($email);
}
}