src/Controller/Api/Subscribers/PostRequestSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\Subscribers;
  3. use App\Controller\Api\Interfaces\IImportController;
  4. use App\Services\ApiRequestQueueService;
  5. use App\Tools\IndexHelper;
  6. use Pimcore\Log\ApplicationLogger;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Finder\Exception\AccessDeniedException;
  9. use Symfony\Component\HttpClient\Exception\JsonException;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Security\Core\Exception\RuntimeException;
  15. class PostRequestSubscriber implements EventSubscriberInterface
  16. {
  17.     protected const COMPONENT 'PostRequestSubscriber';
  18.     protected string $token;
  19.     public function __construct(protected string $apiToken, protected ApiRequestQueueService $apiRequestProcessorService, protected ApplicationLogger $applicationLogger)
  20.     {
  21.         $this->token hash('haval224,3'$apiToken);
  22.     }
  23.     //https://symfony.com/doc/4.0/event_dispatcher/before_after_filters.html
  24.     /**
  25.      * @param ControllerEvent $event
  26.      *
  27.      * @throws JsonException
  28.      */
  29.     public function onKernelController(ControllerEvent $event): void
  30.     {
  31.         $controller $event->getController();
  32.         /**
  33.          * $controller passed can be either a class or a Closure.
  34.          * This is not usual in Symfony but it may happen.
  35.          * If it is a class, it comes in array format
  36.          */
  37.         if (!is_array($controller)) {
  38.             return;
  39.         }
  40.         if ($controller[0] instanceof IImportController) {
  41.             $request $event->getRequest();
  42.             $isPost $request->isMethod('POST');
  43.             if (!$isPost) {
  44.                 throw new MethodNotAllowedHttpException([Request::METHOD_GET]);
  45.             }
  46.             $token hash('haval224,3'$request->headers->get('api-token'));
  47.             if(!$token || $token !== $this->token) {
  48.                 $this->applicationLogger->error("Error trying to access api without or wrong token from " $request->getClientIp(), [IndexHelper::INDEX_COMPONENT => self::COMPONENT]);
  49.                 throw new AccessDeniedException('Security');
  50.             }
  51.             $systemName $controller[0]->getSourceSystemName();
  52.             /**
  53.              * json_decode throws exeption automatically due to last param
  54.              */
  55.             try {
  56.                 json_decode($request->getContent(), true512JSON_THROW_ON_ERROR);
  57.             } catch (\JsonException $jsonException) {
  58.                 $this->applicationLogger->error("Invalid json from $systemName " $request->getPathInfo() . "\n" $request->getContent(). "\n" $jsonException, [IndexHelper::INDEX_COMPONENT => self::COMPONENT]);
  59.                 throw new RuntimeException('Invalid json'400);
  60.             }
  61.             try {
  62.                 $this->apiRequestProcessorService->process($request->getContent(), $systemName$request->getMethod(), $request->getPathInfo());
  63.             } catch (\Exception $ex) {
  64.                 $this->applicationLogger->info("Failed to insert $systemName data. Cause: $ex", [IndexHelper::INDEX_COMPONENT => self::COMPONENT]);
  65.             }
  66.         }
  67.     }
  68.     /**
  69.      * @return string[]
  70.      */
  71.     public static function getSubscribedEvents(): array
  72.     {
  73.         return [
  74.             KernelEvents::CONTROLLER => 'onKernelController',
  75.         ];
  76.     }
  77. }