src/Subscriber/ExceptionSubscriber.php line 34

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