src/Familia2/Infrastructure/Listeners/ApiExceptionListener.php line 64

Open in your IDE?
  1. <?php
  2. namespace Familia2\Infrastructure\Listeners;
  3. use Exception;
  4. use Familia2\Application\Services\Error\ErrorResponse;
  5. use Familia2\Application\Services\Error\ExceptionError;
  6. use Familia2\Application\Services\Exceptions\RequestException;
  7. use Familia2\Infrastructure\EventLogger\EventLogger;
  8. use JMS\Serializer\Serializer;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException;
  15. use Symfony\Component\Translation\TranslatorInterface;
  16. class ApiExceptionListener
  17. {
  18.     /**
  19.      * @var Serializer $serializer
  20.      */
  21.     private $serializer;
  22.     /**
  23.      * @var EventLogger $eventLogger
  24.      */
  25.     private $eventLogger;
  26.     /**
  27.      * @var ContainerInterface $container
  28.      */
  29.     private $container;
  30.     /**
  31.      * @var TranslatorInterface $translator
  32.      */
  33.     private $translator;
  34.     /**
  35.      * @param Serializer                  $serializer
  36.      * @param EventLogger                 $eventLogger
  37.      * @param ContainerInterface          $container
  38.      * @param TranslatorInterface         $translator
  39.      */
  40.     public function __construct(
  41.         Serializer $serializer,
  42.         EventLogger $eventLogger,
  43.         ContainerInterface $container,
  44.         TranslatorInterface $translator
  45.     ) {
  46.         $this->serializer $serializer;
  47.         $this->eventLogger $eventLogger;
  48.         $this->container $container;
  49.         $this->translator $translator;
  50.     }
  51.     /**
  52.      * @param $event
  53.      *
  54.      * @throws Exception
  55.      */
  56.     public function onKernelException(GetResponseForExceptionEvent $event)
  57.     {
  58.         if (strpos($event->getRequest()->getUri(), '/api') !== false) {
  59.             $exception $event->getException();
  60.             if ($exception instanceof RequestException) {
  61.                 $errors $exception->getErrors();
  62.                 $exceptionCode $exception->getCode();
  63.             } elseif ($exception instanceof InsufficientAuthenticationException) {
  64.                 $unauthorizedError = new ExceptionError(
  65.                     $this->translator->trans('error.session.unauthorized', [], 'validators')
  66.                 );
  67.                 $errors ErrorResponse::forError($unauthorizedError);
  68.                 $exceptionCode Response::HTTP_UNAUTHORIZED;
  69.             } else {
  70.                 $this->eventLogger->log($exception);
  71.                 $errors ErrorResponse::forError(new ExceptionError($exception->getMessage()));
  72.                 $exceptionCode $exception->getCode();
  73.                 if (!isset(Response::$statusTexts[$exceptionCode])) {
  74.                     $exceptionCode Response::HTTP_INTERNAL_SERVER_ERROR;
  75.                 }
  76.             }
  77.             $errors $this->serializer->serialize($errors'json');
  78.             $event->setResponse(new Response($errors$exceptionCode));
  79.         } elseif ($event->getException() instanceof NotFoundHttpException) {
  80.             $event->setResponse(new RedirectResponse($this->container->getParameter('path.front.404_error')));
  81.         }
  82.     }
  83. }