src/EventSubscriber/UserConfirmationSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\UserConfirmation;
  5. use App\Security\UserConfirmationService;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class UserConfirmationSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var UserConfirmationService
  15.      */
  16.     private $userConfirmationService;
  17.     public function __construct(
  18.         UserConfirmationService $userConfirmationService
  19.     ) {
  20.         $this->userConfirmationService $userConfirmationService;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.           KernelEvents::VIEW => ['confirmUser'EventPriorities::POST_VALIDATE],
  26.         ];
  27.     }
  28.     public function confirmUser(ViewEvent $event)
  29.     {
  30.         $request $event->getRequest();
  31.         if ('api_user_confirmations_post_collection' !== $request->get('_route')) {
  32.             return;
  33.         }
  34.         /** @var UserConfirmation $confirmationToken */
  35.         $confirmationToken $event->getControllerResult();
  36.         $this->userConfirmationService->confirmUser(
  37.             $confirmationToken->confirmationToken
  38.         );
  39.         $event->setResponse(new JsonResponse(
  40.            null,
  41.            Response::HTTP_OK
  42.         ));
  43.     }
  44. }