<?php
namespace App\EventListener;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Manager\CustomerManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class ForgotPasswordTokenSubscriber.
*/
final class ForgotPasswordTokenSubscriber implements EventSubscriberInterface
{
/**
* @var CustomerManager
*/
private $manager;
/**
* ForgotPasswordTokenSubscriber constructor.
*
* @param CustomerManager $manager
*/
public function __construct(CustomerManager $manager)
{
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['getUserByToken', EventPriorities::POST_VALIDATE],
];
}
/**
* @param GetResponseForControllerResultEvent $event
*/
public function getUserByToken(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
if ('api_forgot_password_tokens_post_collection' !== $request->attributes->get('_route')) {
return;
}
$forgotPasswordToken = $event->getControllerResult();
$user = $this->manager->findUserByValidToken($forgotPasswordToken->token);
if (!$user) {
throw new NotFoundHttpException('Invalid token.');
}
$event->setResponse(new JsonResponse(null, 204));
}
}