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

Open in your IDE?
  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 $didThrow false;
  22.     private $offset;
  23.     private $errorMessage;
  24.     private $error;
  25.     /**
  26.      * @param \Throwable|string $error
  27.      */
  28.     public function __construct(int $offset$error)
  29.     {
  30.         $this->offset $offset;
  31.         if (\is_string($error)) {
  32.             $this->errorMessage $error;
  33.         } else {
  34.             $this->error $error;
  35.             $this->errorMessage $error->getMessage();
  36.         }
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function isTimeout(): bool
  42.     {
  43.         $this->didThrow true;
  44.         if (null !== $this->error) {
  45.             throw new TransportException($this->errorMessage0$this->error);
  46.         }
  47.         return true;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function isFirst(): bool
  53.     {
  54.         $this->didThrow true;
  55.         throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function isLast(): bool
  61.     {
  62.         $this->didThrow true;
  63.         throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function getInformationalStatus(): ?array
  69.     {
  70.         $this->didThrow true;
  71.         throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  72.     }
  73.     /**
  74.      * {@inheritdoc}
  75.      */
  76.     public function getContent(): string
  77.     {
  78.         $this->didThrow true;
  79.         throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function getOffset(): int
  85.     {
  86.         return $this->offset;
  87.     }
  88.     /**
  89.      * {@inheritdoc}
  90.      */
  91.     public function getError(): ?string
  92.     {
  93.         return $this->errorMessage;
  94.     }
  95.     /**
  96.      * @return bool Whether the wrapped error has been thrown or not
  97.      */
  98.     public function didThrow(bool $didThrow null): bool
  99.     {
  100.         if (null !== $didThrow && $this->didThrow !== $didThrow) {
  101.             return !$this->didThrow $didThrow;
  102.         }
  103.         return $this->didThrow;
  104.     }
  105.     public function __sleep(): array
  106.     {
  107.         throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  108.     }
  109.     public function __wakeup()
  110.     {
  111.         throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  112.     }
  113.     public function __destruct()
  114.     {
  115.         if (!$this->didThrow) {
  116.             $this->didThrow true;
  117.             throw null !== $this->error ? new TransportException($this->errorMessage0$this->error) : new TimeoutException($this->errorMessage);
  118.         }
  119.     }
  120. }