src/EventListener/ForgotPasswordTokenSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Manager\CustomerManager;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. /**
  11.  * Class ForgotPasswordTokenSubscriber.
  12.  */
  13. final class ForgotPasswordTokenSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var CustomerManager
  17.      */
  18.     private $manager;
  19.     /**
  20.      * ForgotPasswordTokenSubscriber constructor.
  21.      *
  22.      * @param CustomerManager $manager
  23.      */
  24.     public function __construct(CustomerManager $manager)
  25.     {
  26.         $this->manager $manager;
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             KernelEvents::VIEW => ['getUserByToken'EventPriorities::POST_VALIDATE],
  35.         ];
  36.     }
  37.     /**
  38.      * @param GetResponseForControllerResultEvent $event
  39.      */
  40.     public function getUserByToken(GetResponseForControllerResultEvent $event)
  41.     {
  42.         $request $event->getRequest();
  43.         if ('api_forgot_password_tokens_post_collection' !== $request->attributes->get('_route')) {
  44.             return;
  45.         }
  46.         $forgotPasswordToken $event->getControllerResult();
  47.         $user $this->manager->findUserByValidToken($forgotPasswordToken->token);
  48.         if (!$user) {
  49.             throw new NotFoundHttpException('Invalid token.');
  50.         }
  51.         $event->setResponse(new JsonResponse(null204));
  52.     }
  53. }