<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\JsonResponse;
class InvalidApiPathListener
{
private string $advertisersApiSubdomain;
public function __construct(string $advertisersApiSubdomain)
{
$this->advertisersApiSubdomain = $advertisersApiSubdomain;
}
public function onKernelRequest(RequestEvent $event)
{
// var_dump($this->advertisersApiSubdomain);
// die(123);
$request = $event->getRequest();
$host = $request->getHost();
$path = $request->getPathInfo();
// Allowed paths
$validPaths = ['/api/traffic-report', '/doc'];
$allowedSymfonyDebugPaths = ['/_wdt', '/_profiler', '/doc'];
if ($host !== $this->advertisersApiSubdomain) {
return;
}
// Allow Symfony internal/debug paths
foreach ($allowedSymfonyDebugPaths as $debugPath) {
if (str_starts_with($path, $debugPath)) {
return;
}
}
if (!in_array($path, $validPaths)) {
$response = new JsonResponse(['message' => 'Invalid Api path'], 404);
$event->setResponse($response);
}
}
}