src/Subscriber/HttpClientSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Subscriber;
  4. use App\Event\HttpClient\Events;
  5. use App\Event\HttpClient\HttpClientRequestEvent;
  6. use App\Event\HttpClient\HttpClientResponseEvent;
  7. use App\Payment\Provider\Basys\BasysClient;
  8. use App\Service\Graph\ContainerTraversablePathAccess;
  9. use App\Service\Request\ContextInterface;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class HttpClientSubscriber implements EventSubscriberInterface
  13. {
  14.     const HTTP_ERROR_CODES_REGEX '/(^5\\d{2}$)|(^4\\d{2}$)/';
  15.     public function __construct(
  16.         private readonly ContextInterface $context,
  17.         private readonly LoggerInterface $logger,
  18.     ) {}
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             Events::HTTP_CALL_OUTGOING => 'onRequest',
  23.             Events::HTTP_CALL_INCOMING => 'onResponse',
  24.         ];
  25.     }
  26.     public function onRequest(HttpClientRequestEvent $requestEvent): void
  27.     {
  28.         $request $requestEvent->getRequest();
  29.         $data $this->getMaskedBody($requestEvent);
  30.         $this->logger->info(\sprintf('[HTTP: REQUEST] [%s] %s %s with query parameters %s and body %s and headers %s',
  31.             $requestEvent->getRequestIdentifier(),
  32.             $requestEvent->getMethod(),
  33.             $request->getUrl(),
  34.             \http_build_query($request->getParameters() ?? []),
  35.             \is_array($data) ? \json_encode($data) : $data,
  36.             \json_encode($request->getHeaders())
  37.         ),
  38.             [
  39.                 'request-id' => $this->context->getRequestId()?->toString(),
  40.                 'correlation-id' => $this->context->getCorrelationId()?->toString()
  41.             ]
  42.         );
  43.     }
  44.     private function getMaskedBody(HttpClientRequestEvent $requestEvent)
  45.     {
  46.         if ($requestEvent->getContext() == BasysClient::PROVIDER_NAME) {
  47.             $data $requestEvent->getRequest()->getBody();
  48.             if (empty($data)) {
  49.                 return $data;
  50.             }
  51.             $containerTraversablePathAccess = new ContainerTraversablePathAccess(
  52.                 is_array($data) ? $data json_decode($datatrue)
  53.                 , false);
  54.             if (
  55.                 $containerTraversablePathAccess->getByPath('payment_method.card.cvc'false) !== false
  56.                 || $containerTraversablePathAccess->getByPath('payment_method.card.expiration_date'false) !== false
  57.                 || $containerTraversablePathAccess->getByPath('payment_method.card.number'false) !== false
  58.             ) {
  59.                 $data is_array($data) ? $data json_decode($datatrue);
  60.                 $data['payment_method']['card']['number'] = '***HIDE*FOR*LOGS***';
  61.                 $data['payment_method']['card']['cvc'] = '***HIDE*FOR*LOGS***';
  62.                 $data['payment_method']['card']['expiration_date'] = '***HIDE*FOR*LOGS***';
  63.             }
  64.             return $data;
  65.         }
  66.         return $requestEvent->getRequest()->getBody();
  67.     }
  68.     public function onResponse(HttpClientResponseEvent $responseEvent): void
  69.     {
  70.         $response $responseEvent->getResponse();
  71.         if (preg_match(self::HTTP_ERROR_CODES_REGEX, (string)$response->getStatusCode())) {
  72.             $this->logger->error(\sprintf('[HTTP: RESPONSE] [%s] Response with status code \'%s\' and body \'%s\'',
  73.                 $responseEvent->getRequestIdentifier(),
  74.                 $response->getStatusCode(),
  75.                 $response->getRawBody()
  76.             ),
  77.                 [
  78.                     'request-id' => $this->context->getRequestId()?->toString(),
  79.                     'correlation-id' => $this->context->getCorrelationId()?->toString()
  80.                 ]
  81.             );
  82.         } else {
  83.             $this->logger->info(\sprintf('[HTTP: RESPONSE] [%s] Response with status code \'%s\' and body \'%s\'',
  84.                 $responseEvent->getRequestIdentifier(),
  85.                 $response->getStatusCode(),
  86.                 $response->getRawBody()
  87.             ),
  88.                 [
  89.                     'request-id' => $this->context->getRequestId()?->toString(),
  90.                     'correlation-id' => $this->context->getCorrelationId()?->toString()
  91.                 ]
  92.             );
  93.         }
  94.     }
  95. }