<?php
namespace App\EventSubscriber;
use App\Service\Notifier;
use App\UserBundle\Events\ResettingPasswordEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
/**
* Class ResettingPasswordSubscriber
*
* Le UserBundle émet un event "ResettingPasswordEvent" permettant d'effectuer des actions lors de la demande de réinitialisation du mot de passe
* comme par exemple : envoyer un email avec un lien de reset au user concerné
*
* @package App\EventSubscriber
*/
class ResettingPasswordSubscriber implements EventSubscriberInterface
{
private Notifier $notifier;
private RouterInterface $router;
public function __construct(Notifier $notifier, RouterInterface $router)
{
$this->notifier = $notifier;
$this->router = $router;
}
public static function getSubscribedEvents(): array
{
return [
ResettingPasswordEvent::class => 'OnPasswordReset',
];
}
public function OnPasswordReset(ResettingPasswordEvent $event)
{
$user = $event->getUser();
$confirmation_url = $this->router->generate('resetting_action', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
$this->notifier->createMail($user, 'resetting_password', [
'username' => $user->getNiceName(),
'confirmation_url' => $confirmation_url
]);
}
}