<?php
namespace App\EventSubscriber\Onboarding;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\App\Role;
use App\Entity\App\User;
use App\Entity\App\Vendor;
use App\Entity\Onboarding\Lead;
use App\Exception\AccessDeniedException;
use App\Exception\CaptchaTokenInvalidException;
use App\Exception\NotFoundException;
use App\Repository\App\UserRepository;
use App\Repository\App\VendorRepository;
use App\Services\UtilsService;
use App\Services\VendorService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class LeadPreValidateSubscriber implements EventSubscriberInterface
{
private $utilsService;
public function __construct(UtilsService $utilsService)
{
$this->utilsService = $utilsService;
}
public function onKernelView(ViewEvent $event)
{
if ($this->utilsService->isAPublicRequest($event)) {
return;
}
$lead = $event->getControllerResult();
$request = $event->getRequest();
$method = $event->getRequest()->getMethod();
if (!($lead instanceof Lead) ||
(Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method))
return;
$content = $request->getContent();
$data = json_decode($content, true);
if (isset($data['captchaToken'])) {
if (!$this->utilsService->checkRecaptchaTokenIsValid($data['captchaToken'])) {
throw new CaptchaTokenInvalidException('Sent token is not valid');
}
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::PRE_VALIDATE]
];
}
}