src/EventListener/ForgotPasswordRequestSubscriber.php line 50

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 ForgotPasswordRequestSubscriber.
  12.  */
  13. final class ForgotPasswordRequestSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var CustomerManager
  17.      */
  18.     private $manager;
  19.     /**
  20.      * ForgotPasswordRequestSubscriber 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 => ['sendPasswordResetUrl'EventPriorities::POST_VALIDATE],
  35.         ];
  36.     }
  37.     /**
  38.      * @param GetResponseForControllerResultEvent $event
  39.      *
  40.      * @throws \Twig_Error_Loader
  41.      * @throws \Twig_Error_Runtime
  42.      * @throws \Twig_Error_Syntax
  43.      */
  44.     public function sendPasswordResetUrl(GetResponseForControllerResultEvent $event)
  45.     {
  46.         $request $event->getRequest();
  47.         if ('api_forgot_password_requests_post_collection' !== $request->attributes->get('_route')) {
  48.             return;
  49.         }
  50.         $forgotPasswordRequest $event->getControllerResult();
  51.         $user $this->manager->findOneByEmail($forgotPasswordRequest->email);
  52.         if (!$user) {
  53.             throw new NotFoundHttpException("No user found with email $forgotPasswordRequest->email");
  54.         }
  55.         $this->manager->requestPasswordReset($user);
  56.         $event->setResponse(new JsonResponse(null204));
  57.     }
  58. }