tinternet.net/core/EventSuscriber/Account/PasswordRequestEventSubscriber.php

66 lines
2 KiB
PHP
Raw Normal View History

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