src/EventSubscriber/KernelExceptionSubscriber.php line 49

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.     /**
  22.      * @var RedirectionService
  23.      */
  24.     private RedirectionService $redirectionService;
  25.     public function __construct(
  26.         RequestService $requestService,
  27.         ParameterBagInterface $params,
  28.         RedirectionService $redirectionService
  29.     ) {
  30.         $this->requestService $requestService;
  31.         $this->params $params;
  32.         $this->redirectionService $redirectionService;
  33.     }
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             KernelEvents::EXCEPTION => [
  38.                 ['onKernelException'10],
  39.                 ['redirect301'9],
  40.             ],
  41.         ];
  42.     }
  43.     public function redirect301(ExceptionEvent $event)
  44.     {
  45.         $exception $event->getThrowable();
  46.         $request $event->getRequest();
  47.         if ($exception instanceof NotFoundHttpException) {
  48.             $redirectUrl $this->redirectionService->getRedirectUrl($request->getRequestUri());
  49.             if ($redirectUrl) {
  50.                 $event->setResponse(
  51.                     new RedirectResponse($redirectUrlResponse::HTTP_MOVED_PERMANENTLY)
  52.                 );
  53.             }
  54.         }
  55.     }
  56.     public function onKernelException(ExceptionEvent $event)
  57.     {
  58.         if (!in_array('application/json'$event->getRequest()->getAcceptableContentTypes())) {
  59.             //return;
  60.         }
  61.         $pathUrl $event->getRequest()->getRequestUri();
  62.         if(substr($pathUrl, -1) == '/'){
  63.             $pathUrl substr($pathUrl0, -1);
  64.         }
  65.         if(in_array($pathUrlarray_keys(Redirect301Helper::link301()))){
  66.             $event->setResponse(new RedirectResponse(Redirect301Helper::link301()[$pathUrl]));
  67.             return;
  68.         }
  69.         $exception $event->getThrowable();
  70.         $response = new JsonResponse();
  71.         if ($exception instanceof CacheHitResponse) {
  72.             $this->requestService->exception $exception;
  73.             $event->allowCustomResponseCode();
  74.             $response->setStatusCode(($exception->getCode() == 500) ? 200 $exception->getCode());
  75.             $response->setData(json_decode($exception->getMessage(), 1));
  76.             $response->headers->set('X-Cache-Hit''True');
  77.             $event->setResponse($response);
  78.         } else if ($exception instanceof ApiExceptionInterface) {
  79.             $responseData $exception->getMessages();
  80.             if ($this->params->get('kernel.environment') == 'dev') {
  81.                 $responseData['trace'] = [
  82.                     'position' => $exception->getLine() . " of " $exception->getFile(),
  83.                     'stack' => $exception->getTrace()
  84.                 ];
  85.             }
  86.             $response->setStatusCode($responseData['code']);
  87.             $response->setData($responseData);
  88.             $event->setResponse($response);
  89.             $this->requestService->exception $exception;
  90.             $this->requestService->exceptionCode $responseData['code'];
  91.             $this
  92.                 ->requestService
  93.                 ->saveLog(
  94.                     $event->getResponse()->getStatusCode(),
  95.                     $event->getResponse()->getContent(),
  96.                     null,
  97.                     true
  98.                 );
  99.         } else if ($exception instanceof ForeignKeyConstraintViolationException) {
  100.             $response->setStatusCode(Response::HTTP_BAD_REQUEST);
  101.             $response->setData([
  102.                 'error' => BadRequestException::ENTITY_DELETE_RELATION,
  103.                 'code' => Response::HTTP_BAD_REQUEST
  104.             ]);
  105.             $event->setResponse($response);
  106.             $this->requestService->exception $exception;
  107.             $this->requestService->exceptionCode Response::HTTP_BAD_REQUEST;
  108.             $this
  109.                 ->requestService
  110.                 ->saveLog(
  111.                     $event->getResponse()->getStatusCode(),
  112.                     $event->getResponse()->getContent(),
  113.                     null,
  114.                     true
  115.                 );
  116.         } else if ($exception->getCode() === 500 || $exception->getCode() === 0) {
  117.             $this->requestService->exception $exception;
  118.             $this->requestService->exceptionCode 500;
  119.             
  120.             /*$this
  121.                 ->requestService
  122.                 ->saveLog(
  123.                     500,
  124.                     '',
  125.                     null,
  126.                     true
  127.                 );
  128.             */
  129.         }
  130.     }
  131. }