<?php
declare(strict_types=1);
namespace Hitso\Bundle\CommonBundle\EventListener;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Hitso\Bundle\CommonBundle\Event\UserResetPasswordEvent;
use Hitso\Bundle\CommonBundle\Helper\Mailer\MailerHelperInterface;
use Hitso\Bundle\CommonBundle\Helper\Router\RouterHelperInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserSubscriber implements EventSubscriberInterface
{
use ContainerAwareTrait;
/**
* @var MailerHelperInterface
*/
private $mailerHelper;
/**
* @var TokenGeneratorInterface
*/
private $tokenGenerator;
/**
* @var RouterHelperInterface
*/
private $routerHelper;
public function __construct(
MailerHelperInterface $mailerHelper,
TokenGeneratorInterface $tokenGenerator,
RouterHelperInterface $routerHelper
) {
$this->mailerHelper = $mailerHelper;
$this->tokenGenerator = $tokenGenerator;
$this->routerHelper = $routerHelper;
}
public static function getSubscribedEvents()
{
return [
UserResetPasswordEvent::EVENT_NAME => 'onUserPasswordReset',
];
}
public function onUserPasswordReset(UserResetPasswordEvent $event)
{
$user = $event->getUser();
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
$user->setPasswordRequestedAt(new \DateTime());
$this->mailerHelper->sendEmail([
'recipient' => $user->getEmail(),
'template' => 'HitsoCommonBundle:Mail:layout.html.twig',
'block' => 'mail_reset_password',
'parameters' => [
'user' => $user,
'link' => $this->routerHelper->generateUrl('password_change', ['token' => $user->getConfirmationToken()]),
],
]);
}
}