vendor/symfony/http-client/Chunk/ErrorChunk.php line 56

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpClient\Chunk;
  11. use Symfony\Component\HttpClient\Exception\TimeoutException;
  12. use Symfony\Component\HttpClient\Exception\TransportException;
  13. use Symfony\Contracts\HttpClient\ChunkInterface;
  14. /**
  15.  * @author Nicolas Grekas <p@tchwork.com>
  16.  *
  17.  * @internal
  18.  */
  19. class ErrorChunk implements ChunkInterface
  20. {
  21.     private bool $didThrow false;
  22.     private int $offset;
  23.     private string $errorMessage;
  24.     private ?\Throwable $error null;
  25.     public function __construct(int $offset\Throwable|string $error)
  26.     {
  27.         $this->offset $offset;
  28.         if (\is_string($error)) {
  29.             $this->errorMessage $error;
  30.         } else {
  31.             $this->error $error;
  32.             $this->errorMessage $error->getMessage();
  33.         }
  34.     }
  35.     public function isTimeout(): bool
  36.     {
  37.         $this->didThrow true;
  38.         if (null !== $this->error) {
  39.             throw new TransportException($this->errorMessage0$this->error);
  40.         }
  41.         return true;
  42.     }
  43.     public function isFirst(): bool
  44.     {
  45.         $this->didThrow true;
  46.         throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  47.     }
  48.     public function isLast(): bool
  49.     {
  50.         $this->didThrow true;
  51.         throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  52.     }
  53.     public function getInformationalStatus(): ?array
  54.     {
  55.         $this->didThrow true;
  56.         throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  57.     }
  58.     public function getContent(): string
  59.     {
  60.         $this->didThrow true;
  61.         throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  62.     }
  63.     public function getOffset(): int
  64.     {
  65.         return $this->offset;
  66.     }
  67.     public function getError(): ?string
  68.     {
  69.         return $this->errorMessage;
  70.     }
  71.     public function didThrow(bool $didThrow null): bool
  72.     {
  73.         if (null !== $didThrow && $this->didThrow !== $didThrow) {
  74.             return !$this->didThrow $didThrow;
  75.         }
  76.         return $this->didThrow;
  77.     }
  78.     public function __sleep(): array
  79.     {
  80.         throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  81.     }
  82.     public function __wakeup()
  83.     {
  84.         throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  85.     }
  86.     public function __destruct()
  87.     {
  88.         if (!$this->didThrow) {
  89.             $this->didThrow true;
  90.             throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  91.         }
  92.     }
  93. }