src/EventSubscriber/DateTimeFieldsResponseSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\TenantPerson\TenantPersonService;
  4. use App\Util\Converter;
  5. use Carbon\Carbon;
  6. use Carbon\Exceptions\InvalidFormatException;
  7. use Psr\Cache\InvalidArgumentException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. class DateTimeFieldsResponseSubscriber implements EventSubscriberInterface
  13. {
  14.     private $tenantPersonService;
  15.     public function __construct(TenantPersonService $tenantPersonService)
  16.     {
  17.         $this->tenantPersonService $tenantPersonService;
  18.     }
  19.     /**
  20.      * {@inheritDoc}
  21.      */
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             // The second value of the array defines priority of the subscriber.
  26.             KernelEvents::RESPONSE => ['onKernelResponse'200],
  27.         ];
  28.     }
  29.     /**
  30.      * @throws InvalidArgumentException
  31.      */
  32.     public function onKernelResponse(ResponseEvent $event)
  33.     {
  34.         if (!$this->tenantPersonService->isInitialized()) {
  35.             return;
  36.         }
  37.         $response $event->getResponse();
  38.         if (!$response->isSuccessful()) {
  39.             return;
  40.         }
  41.         $contentType $response->headers->get('Content-Type');
  42.         if (false === strpos($contentType'json')) {
  43.             return;
  44.         }
  45.         $content json_decode($response->getContent(), true);
  46.         if (\JSON_ERROR_NONE !== json_last_error()) {
  47.             return;
  48.         }
  49.         if (null === $content) {
  50.             return;
  51.         }
  52.         if ('' === $content) {
  53.             return;
  54.         }
  55.         $timezone $this->tenantPersonService->getProspectTenantTimezone();
  56.         array_walk_recursive($content, function (&$value$key) use ($timezone) {
  57.             // If the string ends with 'At' then it's datetime field.
  58.             // Convert to snake case for simplicity.
  59.             $snakeCasedKey Converter::toSnakeCase($key);
  60.             if ('_at' === substr($snakeCasedKey, -3)) {
  61.                 try {
  62.                     if (null !== $value) {
  63.                         $value Carbon::parse($value)
  64.                             ->setTimezone($timezone)
  65.                             ->toISOString(true)
  66.                         ;
  67.                     }
  68.                 } catch (InvalidFormatException $e) {
  69.                     return;
  70.                 }
  71.             }
  72.         });
  73.         $jsonResponse = new JsonResponse($content$response->getStatusCode(), $response->headers->all());
  74.         $event->setResponse($jsonResponse);
  75.     }
  76. }