src/EventSubscriber/JsonResponseSubscriber.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Model\Response\DataPersisterResponse;
  4. use App\Service\Cache\UserMappingCacheManager;
  5. use App\V4\Logger\SentryLogger;
  6. use Exception;
  7. use Psr\Cache\CacheException;
  8. use Psr\Cache\InvalidArgumentException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  13. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  14. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  17. /**
  18.  * Class JsonResponseSuscriber.
  19.  */
  20. final class JsonResponseSubscriber implements EventSubscriberInterface
  21. {
  22.     const KEYS_TO_REPLACE = ['createdBy''managedBy''updatedBy'];
  23.     /**
  24.      * @var UserMappingCacheManager
  25.      */
  26.     private $userMappingCacheManager;
  27.     /**
  28.      * @var SentryLogger
  29.      */
  30.     private $sentryLogger;
  31.     /**
  32.      * JsonResponseSuscriber constructor.
  33.      *
  34.      * @param UserMappingCacheManager $userMappingCacheManager
  35.      * @param SentryLogger            $sentryLogger
  36.      */
  37.     public function __construct(UserMappingCacheManager $userMappingCacheManagerSentryLogger $sentryLogger)
  38.     {
  39.         $this->userMappingCacheManager $userMappingCacheManager;
  40.         $this->sentryLogger $sentryLogger;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             KernelEvents::RESPONSE => [
  49.                 ['processResponse'0],
  50.             ],
  51.         ];
  52.     }
  53.     /**
  54.      * @param FilterResponseEvent $event
  55.      *
  56.      * @return void
  57.      *
  58.      * @throws CacheException
  59.      * @throws ClientExceptionInterface
  60.      * @throws DecodingExceptionInterface
  61.      * @throws InvalidArgumentException
  62.      * @throws RedirectionExceptionInterface
  63.      * @throws ServerExceptionInterface
  64.      * @throws TransportExceptionInterface
  65.      * @throws Exception
  66.      */
  67.     public function processResponse(FilterResponseEvent $event): void
  68.     {
  69.         if ($event->getResponse() instanceof DataPersisterResponse) {
  70.             $mappings = [];
  71.             $jsonDecode json_decode($event->getResponse()->getContent(), true);
  72.             $data $jsonDecode['data'] ?? $jsonDecode;
  73.             foreach (self::KEYS_TO_REPLACE as $key) {
  74.                 if (isset($data[$key])) {
  75.                     if (empty($mappings)) {
  76.                         try {
  77.                             $mappings $this->userMappingCacheManager->getUserMappingArray();
  78.                         } catch (Exception $e) {
  79.                             $this->sentryLogger->captureException(
  80.                                 SentryLogger::CHANNEL_SUBSCRIBER,
  81.                                 $e,
  82.                                 [
  83.                                     'catchOnClass' => self::class,
  84.                                     'data' => $data,
  85.                                 ]
  86.                             );
  87.                         }
  88.                     }
  89.                     $data[$key] = $mappings[$data[$key]] ?? $data[$key];
  90.                 }
  91.             }
  92.             if (isset($jsonDecode['data'])) {
  93.                 $jsonDecode['data'] = $data;
  94.                 $event->setResponse(new DataPersisterResponse($jsonDecode));
  95.                 return;
  96.             }
  97.             $jsonDecode $data;
  98.             $event->setResponse(new DataPersisterResponse($jsonDecode));
  99.         }
  100.     }
  101. }