src/EventSubscriber/AppErrorSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license SILK SOFTWARE HOUSE SP Z O O
  4.  */
  5. namespace App\EventSubscriber;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. /**
  11.  * Class AppErrorSubscriber.
  12.  */
  13. class AppErrorSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * Device ping path.
  17.      *
  18.      * @var string[]
  19.      */
  20.     private $encryptedPaths;
  21.     /**
  22.      * RequestDecryptingSubscriber constructor.
  23.      *
  24.      * @param string[] $encryptedPaths Array of encrypted paths.
  25.      */
  26.     public function __construct(array $encryptedPaths)
  27.     {
  28.         $this->encryptedPaths $encryptedPaths;
  29.     }
  30.     /**
  31.      * Get subscribed events.
  32.      *
  33.      * @return array
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             KernelEvents::RESPONSE => ['onKernelResponse'],
  39.         ];
  40.     }
  41.     /**
  42.      * @param ResponseEvent $event
  43.      */
  44.     public function onKernelResponse(ResponseEvent $event)
  45.     {
  46.         if (!$event->isMasterRequest() || !in_array($event->getRequest()->getPathInfo(), $this->encryptedPaths)) {
  47.             return;
  48.         }
  49.         $response $event->getResponse();
  50.         if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
  51.             return;
  52.         }
  53.         $responseBody json_decode($response->getContent(), true);
  54.         $newResponse = [
  55.             'result' => false,
  56.             'message' => Response::$statusTexts[$response->getStatusCode()] ?? '',
  57.         ];
  58.         if (400 === $response->getStatusCode()) {
  59.             $newResponse['errors'] = $responseBody;
  60.         }
  61.         $response->setContent(json_encode($newResponse));
  62.         $event->setResponse($response);
  63.     }
  64. }