src/EventSubscriber/ExceptionListener.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Slivki\EventSubscriber;
  4. use Slivki\Controller\Admin\BePaid\ValidateBePaidCredentialsAction;
  5. use Slivki\Controller\Api\OnlineOrder\Vendor\GetNearestTimeAction;
  6. use Slivki\Controller\Payment\Click\CompletePaymentAction;
  7. use Slivki\Controller\Payment\Click\PreparePaymentAction;
  8. use Slivki\Controller\Subscription\GetChildSubscribersAction;
  9. use Slivki\Controller\Subscription\ShareSubscriptionAction;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  13. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  14. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  15. use function json_decode;
  16. final class ExceptionListener
  17. {
  18.     private const API_NAMESPACES_START = [
  19.         'Api\V2',
  20.         'UserController::loginAction',
  21.         'Api\Partner',
  22.         'Api\Scheduler',
  23.         'Admin\Api',
  24.         'OnlineOrder\GiftCertificate\EditGiftCertificateOnlineOrderAction',
  25.         'MobileApi\Partner',
  26.         'Api\OnlineOrder\Payment\Method',
  27.         ValidateBePaidCredentialsAction::class,
  28.         GetNearestTimeAction::class,
  29.         ShareSubscriptionAction::class,
  30.         GetChildSubscribersAction::class,
  31.         PreparePaymentAction::class,
  32.         CompletePaymentAction::class,
  33.     ];
  34.     public function onKernelException(ExceptionEvent $event): void
  35.     {
  36.         $exception $this->getException($event->getThrowable());
  37.         $exceptionMessage $exception->getMessage();
  38.         $controller $event->getRequest()->attributes->get('_controller');
  39.         if (null !== $controller && $this->isIncludedToRoute($controller)) {
  40.             json_decode($exceptionMessagetrue);
  41.             $isJson = \json_last_error() === JSON_ERROR_NONE;
  42.             $response = new JsonResponse(
  43.                 $isJson $exceptionMessage : ['error' => $exceptionMessage],
  44.                 $exception->getCode() !== $exception->getCode() : Response::HTTP_INTERNAL_SERVER_ERROR,
  45.                 [],
  46.                 $isJson
  47.             );
  48.             if ($exception instanceof HttpExceptionInterface) {
  49.                 $response->setStatusCode($exception->getStatusCode());
  50.                 $response->headers->replace($exception->getHeaders());
  51.             }
  52.             $event->setResponse($response);
  53.         }
  54.     }
  55.     private function isIncludedToRoute(string $controller): bool
  56.     {
  57.         $matches = \array_filter(
  58.             self::API_NAMESPACES_START,
  59.             static fn(string $needle): bool => strpos($controller$needle) !== false,
  60.         );
  61.         return \count($matches) > 0;
  62.     }
  63.     private function getException(\Throwable $exception): \Throwable
  64.     {
  65.         return $exception instanceof HandlerFailedException
  66.             $this->getException($exception->getPrevious())
  67.             : $exception;
  68.     }
  69. }