vendor/gollumsf/rest-bundle/src/EventSubscriber/ExceptionSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. namespace GollumSF\RestBundle\EventSubscriber;
  3. use GollumSF\RestBundle\Annotation\Serialize;
  4. use GollumSF\RestBundle\Configuration\ApiConfigurationInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  9. use Symfony\Component\HttpKernel\Exception\HttpException;
  10. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. class ExceptionSubscriber implements EventSubscriberInterface {
  16.     /** @var SerializerInterface */
  17.     private $serializer;
  18.     /** @var ApiConfigurationInterface */
  19.     private $apiConfiguration;
  20.     /** @var bool */
  21.     private $debug;
  22.     /** @var TokenStorageInterface */
  23.     private $tokenStorage;
  24.     public static function getSubscribedEvents() {
  25.         return [
  26.             KernelEvents::EXCEPTION => [
  27.                 ['onKernelException'256],
  28.             ],
  29.         ];
  30.     }
  31.     public function __construct(
  32.         SerializerInterface $serializer,
  33.         ApiConfigurationInterface $apiConfiguration,
  34.         bool $debug
  35.     ) {
  36.         $this->serializer $serializer;
  37.         $this->apiConfiguration $apiConfiguration;
  38.         $this->debug $debug;
  39.     }
  40.     public function setTokenStorage(TokenStorageInterface $tokenStorage) {
  41.         $this->tokenStorage $tokenStorage;
  42.     }
  43.     public function onKernelException(ExceptionEvent $event) {
  44.         if (
  45.             $this->apiConfiguration->isAlwaysSerializedException() ||
  46.             $event->getRequest()->attributes->has('_'.Serialize::ALIAS_NAME)
  47.         ) {
  48.             $code Response::HTTP_INTERNAL_SERVER_ERROR;
  49.             $e $event->getThrowable();
  50.             if (
  51.                 $e instanceof UnauthorizedHttpException ||
  52.                 $e instanceof AccessDeniedHttpException ||
  53.                 $e instanceof AccessDeniedException
  54.             ) {
  55.                 $code Response::HTTP_UNAUTHORIZED;
  56.                 if ($this->isAuthenticated()) {
  57.                     $code Response::HTTP_FORBIDDEN;
  58.                 }
  59.             } else
  60.             if (
  61.                 $e instanceof HttpException
  62.             ) {
  63.                 $code $e->getStatusCode();
  64.             }
  65.             $json $this->debug ? [
  66.                 'message' => $e->getMessage(),
  67.                 'code' => $e->getCode(),
  68.                 'file' => $e->getFile(),
  69.                 'line' => $e->getLine(),
  70.                 'stack' => array_map(function ($trace) {
  71.                     unset($trace['args']);
  72.                     return $trace;
  73.                 }, $e->getTrace()),
  74.                 'class' => get_class($e)
  75.             ] : [
  76.                 'message' => $e->getMessage(),
  77.                 'code' => $e->getCode(),
  78.             ];
  79.             $content $this->serialize($json'json');
  80.             $headers = [
  81.                 'Content-Type'   => 'application/json',
  82.                 'Content-Length' => mb_strlen($content'UTF-8')
  83.             ];
  84.             $event->setResponse(new Response($content$code$headers));
  85.         }
  86.     }
  87.     protected function serialize($datastring $format) {
  88.         return $this->serializer->serialize($data$format);
  89.     }
  90.     protected function isAuthenticated(): bool {
  91.         return $this->tokenStorage && $this->tokenStorage->getToken() && $this->tokenStorage->getToken()->isAuthenticated();
  92.     }
  93. }