src/EventListener/KernelEventListener.php line 72
<?phpnamespace App\EventListener;use App\Entity\Channel;use App\Entity\Shop\Shop;use App\Entity\User;use App\Filter\ChannelFilter;use App\Helper\ApiMarker;use App\PortoContainers\Zapier\Helper\ZapierRequestMarker;use App\Service\ChannelService;use App\Service\Logger\RequestLoggerService;use App\ViewHandler\FormatterHandler;use Doctrine\ORM\EntityManagerInterface;use Exception;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpKernel\Event\ExceptionEvent;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\HttpKernel\Event\ResponseEvent;use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;use Symfony\Component\Security\Core\User\UserInterface;use App\PortoContainers\Zapier\Actions\IsApiRequestForZapierAction;use App\PortoContainers\Zapier\Actions\ValidateApiKeyInRequestAction;use App\PortoContainers\Global\DTO\RequestData;class KernelEventListener{/*** @param RequestLoggerService $loggerService* @param FormatterHandler $formatterHandler* @param TokenStorageInterface $tokenStorage* @param ChannelService $channelService* @param EntityManagerInterface $em* @param IsApiRequestForZapierAction $isApiRequestForZapierAction* @param ValidateApiKeyInRequestAction $validateApiKeyInRequestAction*/public function __construct(private readonly RequestLoggerService $loggerService,private readonly FormatterHandler $formatterHandler,private readonly TokenStorageInterface $tokenStorage,private readonly ChannelService $channelService,private readonly EntityManagerInterface $em,private readonly IsApiRequestForZapierAction $isApiRequestForZapierAction,private readonly ValidateApiKeyInRequestAction $validateApiKeyInRequestAction){}/*** @param RequestEvent $event*/public function onKernelRequest(RequestEvent $event): void{if (ApiMarker::isApiRequest()) {$this->convertApiJsonRequest($event);} elseif ($this->isApiRequestForZapierAction->run($requestData = new RequestData($event->getRequest()))) {$this->validateApiKeyInRequestAction->run($requestData);} elseif ($this->getUser() instanceof UserInterface) {$this->checkCustomerShopToken($event->getRequest());}}/*** @param ResponseEvent $event** @throws Exception*/public function onKernelResponse(ResponseEvent $event): void{if (ApiMarker::isApiRequest() || ZapierRequestMarker::isRequest()) {$this->logApiResponse($event);}}/*** @param ExceptionEvent $event* @return void*/public function onKernelException(ExceptionEvent $event): void{if (ApiMarker::isApiRequest() || ZapierRequestMarker::isRequest()) {$this->formatExceptionResponse($event);}}/*** @param RequestEvent $event* @return void*/private function convertApiJsonRequest(RequestEvent $event): void{$request = $event->getRequest();if (0 === strpos($request->headers->get('content-type'), 'application/json')) {$data = json_decode($request->getContent(), true);$request->request->replace(is_array($data) ? $data : []);}}/*** @param ResponseEvent $event** @throws Exception*/private function logApiResponse(ResponseEvent $event): void{$this->loggerService->addLog($event);//try {// $this->loggerService->addLog($event);//} catch (Exception $ex) {// # TODO send log to telegram//}}/*** @param ExceptionEvent $event*/private function formatExceptionResponse(ExceptionEvent $event): void{$exception = $event->getThrowable();if ($exception instanceof HttpExceptionInterface) {$status = $exception->getStatusCode();} else {$status = $exception->getCode() ?: Response::HTTP_BAD_REQUEST;}$event->setResponse($this->formatterHandler->createResponse($exception, $status));}/*** @return User|null*/private function getUser(): ?UserInterface{$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;return $user instanceof UserInterface ? $user : null;}/*** @param Request $request*/private function checkCustomerShopToken(Request $request): void{/** @var ChannelFilter $filter */$filters = $this->em->getFilters();$filter = $filters->getFilter('channelable');$channel = $this->channelService->getCustomerChannel($this->getUser());if ($channel instanceof Channel) {$request->headers->set('authorization', 'bearer ' . $channel->getToken());/*** Parameter is setting for making queries unique,* without this for client side with many shops queries will be load from previous shop*/$filter->setParameter('shop_id', $channel->getShop()->getId());$filter->setShop($channel->getShop());$filter->disableForEntity(Shop::class);} elseif ($request->getPathInfo() === '/shop/') {$filter->setParameter('shop_id', 'unknown_shop');} else {/*** For 'prod' environment with cache pools:* Without this dashboard statistic won't work after switching to shop side and back*/$filter->setParameter('shop_id', 'admin');}}}