src/EventListener/InvalidApiPathListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. class InvalidApiPathListener
  6. {
  7.     private string $advertisersApiSubdomain;
  8.     public function __construct(string $advertisersApiSubdomain)
  9.     {
  10.         $this->advertisersApiSubdomain $advertisersApiSubdomain;
  11.     }
  12.     public function onKernelRequest(RequestEvent $event)
  13.     {
  14. //        var_dump($this->advertisersApiSubdomain);
  15. //        die(123);
  16.         $request $event->getRequest();
  17.         $host $request->getHost();
  18.         $path $request->getPathInfo();
  19.         // Allowed paths
  20.         $validPaths = ['/api/traffic-report''/doc'];
  21.         $allowedSymfonyDebugPaths = ['/_wdt''/_profiler''/doc'];
  22.         if ($host !== $this->advertisersApiSubdomain) {
  23.             return;
  24.         }
  25.         // Allow Symfony internal/debug paths
  26.         foreach ($allowedSymfonyDebugPaths as $debugPath) {
  27.             if (str_starts_with($path$debugPath)) {
  28.                 return;
  29.             }
  30.         }
  31.         if (!in_array($path$validPaths)) {
  32.             $response = new JsonResponse(['message' => 'Invalid Api path'], 404);
  33.             $event->setResponse($response);
  34.         }
  35.     }
  36. }