<?php
declare(strict_types=1);
namespace App\Bundles\OrganizationBundle\EventListener;
use App\Bundles\OrganizationBundle\Exception\ApiKeyNotFoundException;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
#[AsEventListener(event: ExceptionEvent::class, method: 'onKernelException')]
class ApiKeyNotFoundEventListener
{
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if ($exception instanceof ApiKeyNotFoundException) {
$event->setResponse(new JsonResponse(status: Response::HTTP_UNAUTHORIZED));
}
}
}