src/EventListener/ForgotPasswordResetSubscriber.php line 47

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