src/EventSubscriber/Onboarding/LeadPreValidateSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Onboarding;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\App\Role;
  5. use App\Entity\App\User;
  6. use App\Entity\App\Vendor;
  7. use App\Entity\Onboarding\Lead;
  8. use App\Exception\AccessDeniedException;
  9. use App\Exception\CaptchaTokenInvalidException;
  10. use App\Exception\NotFoundException;
  11. use App\Repository\App\UserRepository;
  12. use App\Repository\App\VendorRepository;
  13. use App\Services\UtilsService;
  14. use App\Services\VendorService;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Event\ViewEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  22. class LeadPreValidateSubscriber  implements EventSubscriberInterface
  23. {
  24.     private $utilsService;
  25.     public function __construct(UtilsService $utilsService)
  26.     {
  27.         $this->utilsService $utilsService;
  28.     }
  29.     
  30.     public function onKernelView(ViewEvent $event)
  31.     {
  32.         if ($this->utilsService->isAPublicRequest($event)) {
  33.             return;
  34.         }
  35.         $lead $event->getControllerResult();
  36.         $request $event->getRequest();
  37.         $method $event->getRequest()->getMethod();
  38.         if (!($lead instanceof Lead) ||
  39.             (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method))
  40.             return;
  41.         $content $request->getContent();
  42.         $data json_decode($contenttrue);
  43.         if (isset($data['captchaToken'])) {
  44.             if (!$this->utilsService->checkRecaptchaTokenIsValid($data['captchaToken'])) {
  45.                 throw new CaptchaTokenInvalidException('Sent token is not valid');
  46.             }
  47.         }
  48.     }
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_VALIDATE]
  53.         ];
  54.     }
  55. }