src/Subscriber/ExceptionSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Subscriber;
  4. use App\Exception\GeneralException;
  5. use App\Service\Context\ContextInterface;
  6. use App\Service\Exception\ExceptionJsonLogger;
  7. use App\Service\Exception\Handler\ExceptionHandler;
  8. use ReflectionClass;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  15. readonly class ExceptionSubscriber implements EventSubscriberInterface
  16. {
  17.     public function __construct(
  18.         private ContextInterface $context,
  19.         private ExceptionHandler $exceptionHandler,
  20.         private ExceptionJsonLogger $jsonLogger
  21.     ) {}
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [KernelEvents::EXCEPTION => ['prepareResponse', -127]];
  25.     }
  26.     public function prepareResponse(ExceptionEvent $event): void
  27.     {
  28.         $exception $event->getThrowable();
  29.         if ($exception instanceof HandlerFailedException) {
  30.             $exception $exception->getNestedExceptions()[0];
  31.         }
  32.         if (str_starts_with(get_class($exception), "App\Exception") === true) {
  33.             $additionalInfo $this->checkEventParameters($event,$exception);
  34.             $this->jsonLogger->multiLogger($exception$additionalInfo);
  35.         }
  36.         if($exception instanceof GeneralException) {
  37.             $exception $exception->getPrevious();
  38.         }
  39.         $httpStatusCode method_exists($exception'getStatusCode')
  40.             ? $exception->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR;
  41.         $exceptionDTO $this->exceptionHandler->handle($exception);
  42.         $response = new JsonResponse($exceptionDTO->toArray(), $httpStatusCode);
  43.         $event->setResponse($response);
  44.     }
  45.     public function checkEventParameters($event$exception): array
  46.     {
  47.         $additionalInfo = [];
  48.         $parameters $event->getRequest()->attributes;
  49.         if (!is_null($parameters->get('orderId'))){
  50.             $additionalInfo[] = ['orderId' => $parameters->get('orderId')];
  51.         }
  52.         if (!is_null($parameters->get('cinemaId'))){
  53.             $additionalInfo[] = ['cinemaId' => $parameters->get('cinemaId')];
  54.         }
  55.         if (!is_null($parameters->get('paymentProvider'))){
  56.             $additionalInfo[] = ['paymentProvider' => $parameters->get('paymentProvider')];
  57.         }
  58.         if (!is_null($parameters->get('providerId'))){
  59.             $additionalInfo[] = ['providerId' => $parameters->get('providerId')];
  60.         }
  61.         if (!is_null($parameters->get('screeningsId'))){
  62.             $additionalInfo[] = ['screeningsId' => $parameters->get('screeningsId')];
  63.         }
  64.         if (!is_null($parameters->get('screeningItemId'))){
  65.             $additionalInfo[] = ['screeningItemId' => $parameters->get('screeningItemId')];
  66.         }
  67.         if (!is_null($parameters->get('paymentMethodId'))){
  68.             $additionalInfo[] = ['paymentMethodId' => $parameters->get('paymentMethodId')];
  69.         }
  70.         if (!is_null($parameters->get('bookingId'))){
  71.             $additionalInfo[] = ['bookingId' => $parameters->get('bookingId')];
  72.         }
  73.         if (!is_null($parameters->get('itemId'))){
  74.             $additionalInfo[] = ['itemId' => $parameters->get('itemId')];
  75.         }
  76.         if (!is_null($parameters->get('groupId'))){
  77.             $additionalInfo[] = ['groupId' => $parameters->get('groupId')];
  78.         }
  79.         if (!is_null($parameters->get('promotion'))){
  80.             $additionalInfo[] = ['promotion' => $parameters->get('promotion')];
  81.         }
  82.         if (!is_null($parameters->get('pickupTimeId'))){
  83.             $additionalInfo[] = ['pickupTimeId' => $parameters->get('pickupTimeId')];
  84.         }
  85.         if (!empty($event->getRequest()->request->all())){
  86.             $additionalInfo[] = ['request' => $event->getRequest()->request->all()];
  87.         }
  88.         if ($exception !== null &&
  89.             method_exists($exception,'getProperties') && !empty($exception->getProperties())){
  90.             $additionalInfo[] = $exception->getProperties();
  91.         }
  92.         return $additionalInfo;
  93.     }
  94. }