src/EventSubscriber/ExceptionListener.php line 62

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