src/EventSubscriber/KernelExceptionSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\RedirectionService;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  13. use App\Exception\CacheHitResponse;
  14. use App\Exception\ValidateException;
  15. use App\Exception\ApiExceptionInterface;
  16. use App\Exception\BadRequestException;
  17. use App\Service\RequestService;
  18. use App\Constant\Redirect301Helper;
  19. class KernelExceptionSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(
  22.         public RequestService $requestService,
  23.         public ParameterBagInterface $params,
  24.         public RedirectionService $redirectionService
  25.     ) {
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             KernelEvents::EXCEPTION => [
  31.                 ['onKernelException'10],
  32.                 ['redirect301'9],
  33.             ],
  34.         ];
  35.     }
  36.     public function redirect301(ExceptionEvent $event)
  37.     {
  38.         $exception $event->getThrowable();
  39.         $request $event->getRequest();
  40.         if ($exception instanceof NotFoundHttpException) {
  41.             $redirectUrl $this->redirectionService->getRedirectUrl($request->getRequestUri());
  42.             if ($redirectUrl) {
  43.                 $event->setResponse(
  44.                     new RedirectResponse($redirectUrlResponse::HTTP_MOVED_PERMANENTLY)
  45.                 );
  46.             }
  47.         }
  48.     }
  49.     public function onKernelException(ExceptionEvent $event)
  50.     {
  51.         if (!in_array('application/json'$event->getRequest()->getAcceptableContentTypes())) {
  52.             //return;
  53.         }
  54.         $pathUrl $event->getRequest()->getRequestUri();
  55.         if(substr($pathUrl, -1) == '/'){
  56.             $pathUrl substr($pathUrl0, -1);
  57.         }
  58.         if(in_array($pathUrlarray_keys(Redirect301Helper::link301()))){
  59.             $event->setResponse(new RedirectResponse(Redirect301Helper::link301()[$pathUrl]));
  60.             return;
  61.         }
  62.         $exception $event->getThrowable();
  63.         $response = new JsonResponse();
  64.         if ($exception instanceof CacheHitResponse) {
  65.             $this->requestService->exception $exception;
  66.             $event->allowCustomResponseCode();
  67.             $response->setStatusCode(($exception->getCode() == 500) ? 200 $exception->getCode());
  68.             $response->setData(json_decode($exception->getMessage(), 1));
  69.             $response->headers->set('X-Cache-Hit''True');
  70.             $event->setResponse($response);
  71.         } else if ($exception instanceof ApiExceptionInterface) {
  72.             $responseData $exception->getMessages();
  73.             if ($this->params->get('kernel.environment') == 'dev') {
  74.                 $responseData['trace'] = [
  75.                     'position' => $exception->getLine() . " of " $exception->getFile(),
  76.                     'stack' => $exception->getTrace()
  77.                 ];
  78.             }
  79.             $response->setStatusCode($responseData['code']);
  80.             $response->setData($responseData);
  81.             $event->setResponse($response);
  82.             $this->requestService->exception $exception;
  83.             $this->requestService->exceptionCode $responseData['code'];
  84.             $this
  85.                 ->requestService
  86.                 ->saveLog(
  87.                     $event->getResponse()->getStatusCode(),
  88.                     $event->getResponse()->getContent(),
  89.                     null,
  90.                     true
  91.                 );
  92.         } else if ($exception instanceof ForeignKeyConstraintViolationException) {
  93.             $response->setStatusCode(Response::HTTP_BAD_REQUEST);
  94.             $response->setData([
  95.                 'error' => BadRequestException::ENTITY_DELETE_RELATION,
  96.                 'code' => Response::HTTP_BAD_REQUEST
  97.             ]);
  98.             $event->setResponse($response);
  99.             $this->requestService->exception $exception;
  100.             $this->requestService->exceptionCode Response::HTTP_BAD_REQUEST;
  101.             $this
  102.                 ->requestService
  103.                 ->saveLog(
  104.                     $event->getResponse()->getStatusCode(),
  105.                     $event->getResponse()->getContent(),
  106.                     null,
  107.                     true
  108.                 );
  109.         } else if ($exception->getCode() === 500 || $exception->getCode() === 0) {
  110.             $this->requestService->exception $exception;
  111.             $this->requestService->exceptionCode 500;
  112.             
  113.             /*$this
  114.                 ->requestService
  115.                 ->saveLog(
  116.                     500,
  117.                     '',
  118.                     null,
  119.                     true
  120.                 );
  121.             */
  122.         }
  123.     }
  124. }