src/EventSubscriber/KernelResponseSubscriber.php line 25

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.         public RequestService $requestService
  11.     ) {
  12.     }
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.             KernelEvents::RESPONSE => 'onKernelResponse',
  17.         ];
  18.     }
  19.     public function onKernelResponse(ResponseEvent $event) {
  20.         if(is_null($this->requestService->exception)) {
  21.             $this
  22.             ->requestService
  23.             ->saveLog(
  24.                 $event->getResponse()->getStatusCode(),
  25.                 $event->getResponse()->getContent()
  26.             );
  27.             if($this->requestService->useCache === true) {
  28.                 $appCache $this->requestService->appCache;
  29.                 $cacheResponse $appCache->getItem($this->requestService->cacheResponseKey);
  30.                 $payload = [
  31.                     'code' => $event->getResponse()->getStatusCode(),
  32.                     'data' => json_decode($event->getResponse()->getContent(), 1)
  33.                 ];
  34.                 $cacheResponse->set(json_encode($payload1));
  35.                 $appCache->save($cacheResponse);
  36.             }
  37.         }
  38.     }
  39. }