src/DataCollector/HttpClientDataCollector.php line 101

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\DataCollector;
  4. use App\DataCollector\Model\HttpClientFlowCall;
  5. use App\Event\HttpClient\Events;
  6. use App\Event\HttpClient\HttpClientRequestEvent;
  7. use App\Event\HttpClient\HttpClientResponseEvent;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  13. class HttpClientDataCollector extends DataCollector implements EventSubscriberInterface
  14. {
  15.     protected $data = [
  16.         'calls' => null
  17.     ];
  18.     public function __construct()
  19.     {
  20.         $this->data['calls'] = new ArrayCollection();
  21.     }
  22.     public function collect(Request $requestResponse $response\Throwable $exception null)
  23.     {
  24.         //nothing to do here, please look into event's method
  25.     }
  26.     public function getName()
  27.     {
  28.         return 'http_client.collector';
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             Events::HTTP_CALL_OUTGOING => 'onRequest',
  34.             Events::HTTP_CALL_INCOMING => 'onResponse',
  35.         ];
  36.     }
  37.     public function getCalls() : ArrayCollection
  38.     {
  39.         return $this->data['calls'];
  40.     }
  41.     public function reset()
  42.     {
  43.         // TODO: Implement reset() method.
  44.     }
  45.     public function onRequest(HttpClientRequestEvent $requestEvent) : void
  46.     {
  47.         $httpClientFlowCall = new HttpClientFlowCall();
  48.         $httpClientFlowCall->setRequestId($requestEvent->getRequestIdentifier());
  49.         $httpClientFlowCall->setRequestMethod($requestEvent->getMethod());
  50.         $httpClientFlowCall->setRequestUrl($requestEvent->getRequest()->getUrl());
  51.         $httpClientFlowCall->setRequestParameters($requestEvent->getRequest()->getParameters() ?? []);
  52.         $httpClientFlowCall->setRequestBody($requestEvent->getRequest()->getBody());
  53.         $httpClientFlowCall->setRequestHeaders($requestEvent->getRequest()->getHeaders());
  54.         $requestBodyRaw $httpClientFlowCall->getRequestBody();
  55.         $decodedRequestBody null;
  56.         if ($requestBodyRaw !== null){
  57.             try{
  58.                 $decodedRequestBody = @\json_decode($requestBodyRawtrue1024JSON_THROW_ON_ERROR);
  59.             }
  60.             catch(\Exception){
  61.                 $decodedRequestBody null;
  62.             }
  63.         }
  64.         if (\is_array($decodedRequestBody)){
  65.             try{
  66.                 $decodedRequestBody = @\json_encode($decodedRequestBodyJSON_PRETTY_PRINT);
  67.             }
  68.             catch(\Exception){
  69.                 $decodedRequestBody null;
  70.             }
  71.         }
  72.         else{
  73.             $decodedRequestBody null;
  74.         }
  75.         $httpClientFlowCall->setRequestBodyDecoded($decodedRequestBody);
  76.         $collection $this->data['calls'];
  77.         assert($collection instanceof ArrayCollection);
  78.         $collection->add($httpClientFlowCall);
  79.     }
  80.     public function onResponse(HttpClientResponseEvent $responseEvent) : void
  81.     {
  82.         $response $responseEvent->getResponse();
  83.         $collection $this->data['calls'];
  84.         assert($collection instanceof ArrayCollection);
  85.         $filteredCollection $collection->filter(fn (HttpClientFlowCall $call) =>
  86.             $call->getRequestId() === $responseEvent->getRequestIdentifier()
  87.         );
  88.         $httpClientFlowCall $filteredCollection->first() === false null :
  89.             $filteredCollection->first();
  90.         assert($httpClientFlowCall instanceof HttpClientFlowCall || $httpClientFlowCall === null);
  91.         $httpResponseStatusCode = (int) $response->getStatusCode();
  92.         $responseRawBody = (string) $response->getRawBody();
  93.         try{
  94.             $responseDecoded json_decode($responseRawBodytrue1024JSON_THROW_ON_ERROR);
  95.         }
  96.         catch(\Exception){
  97.             $responseDecoded null;
  98.         }
  99.         if (\is_array($responseDecoded)){
  100.             try{
  101.                 $responseDecoded \json_encode($responseDecodedJSON_PRETTY_PRINT);
  102.             }
  103.             catch(\Exception){
  104.                 $responseDecoded null;
  105.             }
  106.         }
  107.         $httpClientFlowCall->setResponseStatusCode($httpResponseStatusCode);
  108.         $httpClientFlowCall->setResponseRawBody($responseRawBody);
  109.         $httpClientFlowCall->setResponseDecoded($responseDecoded);
  110.     }
  111. }