src/EventSubscriber/KernelResponseSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\KernelEvents;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. use App\Service\RequestService;
  7. class KernelResponseSubscriber implements EventSubscriberInterface
  8. {
  9.     public function __construct(
  10.         RequestService $requestService
  11.     ) {
  12.         $this->requestService $requestService;
  13.     }
  14.     public static function getSubscribedEvents()
  15.     {
  16.         return [
  17.             KernelEvents::RESPONSE => 'onKernelResponse',
  18.         ];
  19.     }
  20.     public function onKernelResponse(ResponseEvent $event) {
  21.         if(is_null($this->requestService->exception)) {
  22.             $this
  23.             ->requestService
  24.             ->saveLog(
  25.                 $event->getResponse()->getStatusCode(),
  26.                 $event->getResponse()->getContent()
  27.             );
  28.             if($this->requestService->useCache === true) {
  29.                 $cache $this->requestService->cache;
  30.                 $cacheResponse $cache->getItem($this->requestService->cacheResponseKey);
  31.                 $payload = [
  32.                     'code' => $event->getResponse()->getStatusCode(),
  33.                     'data' => json_decode($event->getResponse()->getContent(), 1)
  34.                 ];
  35.                 $cacheResponse->set(json_encode($payload1));
  36.                 $cache->save($cacheResponse);
  37.             }
  38.         }
  39.     }
  40. }