murph-skeleton/core/EventSuscriber/Account/PasswordRequestEventSubscriber.php
2021-03-24 17:20:02 +01:00

69 lines
2.2 KiB
PHP

<?php
namespace App\Core\EventSuscriber\Account;
use App\Core\Event\Account\PasswordRequestEvent;
use App\Core\Manager\EntityManager;
use App\Core\Notification\MailNotifier;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
/**
* class EventListener.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class PasswordRequestEventSubscriber implements EventSubscriberInterface
{
protected MailNotifier $notifier;
protected UrlGeneratorInterface $urlGenerator;
protected EntityManager $entityManager;
protected TokenGeneratorInterface $tokenGenerator;
protected TranslatorInterface $translator;
public function __construct(
MailNotifier $notifier,
UrlGeneratorInterface $urlGenerator,
EntityManager $entityManager,
TokenGeneratorInterface $tokenGenerator,
TranslatorInterface $translator
) {
$this->notifier = $notifier;
$this->urlGenerator = $urlGenerator;
$this->entityManager = $entityManager;
$this->tokenGenerator = $tokenGenerator;
$this->translator = $translator;
}
public static function getSubscribedEvents()
{
return [
PasswordRequestEvent::EVENT => 'onRequest',
];
}
public function onRequest(PasswordRequestEvent $event)
{
$user = $event->getUser();
$user->setConfirmationToken($this->tokenGenerator->generateToken());
$user->setPasswordRequestedAt(new \DateTime('now'));
$this->entityManager->update($user);
$this->notifier
->setSubject($translator->trans('Mot de passe perdu'))
->addRecipient($user->getEmail())
->notify('@Core/mail/account/resetting_request.html.twig', [
'reseting_update_link' => $this->urlGenerator->generate(
'auth_resetting_update',
[
'token' => $user->getConfirmationToken(),
],
UrlGeneratorInterface::ABSOLUTE_URL
),
])
;
}
}