vendor/sentry/sentry-symfony/src/Tracing/HttpClient/AbstractTraceableResponse.php line 116

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\Tracing\HttpClient;
  4. use Sentry\Tracing\Span;
  5. use Symfony\Contracts\HttpClient\ChunkInterface;
  6. use Symfony\Contracts\HttpClient\HttpClientInterface;
  7. use Symfony\Contracts\HttpClient\ResponseInterface;
  8. /**
  9.  * @internal
  10.  */
  11. abstract class AbstractTraceableResponse implements ResponseInterface
  12. {
  13.     /**
  14.      * @var ResponseInterface
  15.      */
  16.     protected $response;
  17.     /**
  18.      * @var HttpClientInterface
  19.      */
  20.     protected $client;
  21.     /**
  22.      * @var Span|null
  23.      */
  24.     protected $span;
  25.     public function __construct(HttpClientInterface $clientResponseInterface $response, ?Span $span)
  26.     {
  27.         $this->client $client;
  28.         $this->response $response;
  29.         $this->span $span;
  30.     }
  31.     public function __destruct()
  32.     {
  33.         try {
  34.             if (method_exists($this->response'__destruct')) {
  35.                 $this->response->__destruct();
  36.             }
  37.         } finally {
  38.             $this->finishSpan();
  39.         }
  40.     }
  41.     public function __sleep(): array
  42.     {
  43.         throw new \BadMethodCallException('Serializing instances of this class is forbidden.');
  44.     }
  45.     public function __wakeup()
  46.     {
  47.         throw new \BadMethodCallException('Unserializing instances of this class is forbidden.');
  48.     }
  49.     public function getStatusCode(): int
  50.     {
  51.         return $this->response->getStatusCode();
  52.     }
  53.     public function getHeaders(bool $throw true): array
  54.     {
  55.         return $this->response->getHeaders($throw);
  56.     }
  57.     public function getContent(bool $throw true): string
  58.     {
  59.         try {
  60.             return $this->response->getContent($throw);
  61.         } finally {
  62.             $this->finishSpan();
  63.         }
  64.     }
  65.     public function toArray(bool $throw true): array
  66.     {
  67.         try {
  68.             return $this->response->toArray($throw);
  69.         } finally {
  70.             $this->finishSpan();
  71.         }
  72.     }
  73.     public function cancel(): void
  74.     {
  75.         $this->response->cancel();
  76.         $this->finishSpan();
  77.     }
  78.     /**
  79.      * @internal
  80.      *
  81.      * @param iterable<AbstractTraceableResponse> $responses
  82.      *
  83.      * @return \Generator<AbstractTraceableResponse, ChunkInterface>
  84.      */
  85.     public static function stream(HttpClientInterface $clientiterable $responses, ?float $timeout): \Generator
  86.     {
  87.         /** @var \SplObjectStorage<ResponseInterface, AbstractTraceableResponse> $traceableMap */
  88.         $traceableMap = new \SplObjectStorage();
  89.         $wrappedResponses = [];
  90.         foreach ($responses as $response) {
  91.             if (!$response instanceof self) {
  92.                 throw new \TypeError(sprintf('"%s::stream()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.'TraceableHttpClient::class, get_debug_type($response)));
  93.             }
  94.             $traceableMap[$response->response] = $response;
  95.             $wrappedResponses[] = $response->response;
  96.         }
  97.         foreach ($client->stream($wrappedResponses$timeout) as $response => $chunk) {
  98.             $traceableResponse $traceableMap[$response];
  99.             $traceableResponse->finishSpan();
  100.             yield $traceableResponse => $chunk;
  101.         }
  102.     }
  103.     private function finishSpan(): void
  104.     {
  105.         if (null !== $this->span) {
  106.             $this->span->finish();
  107.             $this->span null;
  108.         }
  109.     }
  110. }