| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Chunk; |
| 13 |
|
| 14 |
use Symfony\Component\HttpClient\Exception\TimeoutException; |
| 15 |
use Symfony\Component\HttpClient\Exception\TransportException; |
| 16 |
use Symfony\Contracts\HttpClient\ChunkInterface; |
| 17 |
|
| 18 |
/** |
| 19 |
* @author Nicolas Grekas <[email protected]> |
| 20 |
* |
| 21 |
* @internal |
| 22 |
*/ |
| 23 |
class ErrorChunk implements ChunkInterface |
| 24 |
{ |
| 25 |
private $didThrow = false; |
| 26 |
private $offset; |
| 27 |
private $errorMessage; |
| 28 |
private $error; |
| 29 |
|
| 30 |
/** |
| 31 |
* @param \Throwable|string $error |
| 32 |
*/ |
| 33 |
public function __construct(int $offset, $error) |
| 34 |
{ |
| 35 |
$this->offset = $offset; |
| 36 |
|
| 37 |
if (\is_string($error)) { |
| 38 |
$this->errorMessage = $error; |
| 39 |
} else { |
| 40 |
$this->error = $error; |
| 41 |
$this->errorMessage = $error->getMessage(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* {@inheritdoc} |
| 47 |
*/ |
| 48 |
public function isTimeout(): bool |
| 49 |
{ |
| 50 |
$this->didThrow = true; |
| 51 |
|
| 52 |
if (null !== $this->error) { |
| 53 |
throw new TransportException($this->errorMessage, 0, $this->error); |
| 54 |
} |
| 55 |
|
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* {@inheritdoc} |
| 61 |
*/ |
| 62 |
public function isFirst(): bool |
| 63 |
{ |
| 64 |
$this->didThrow = true; |
| 65 |
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* {@inheritdoc} |
| 70 |
*/ |
| 71 |
public function isLast(): bool |
| 72 |
{ |
| 73 |
$this->didThrow = true; |
| 74 |
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* {@inheritdoc} |
| 79 |
*/ |
| 80 |
public function getInformationalStatus(): ?array |
| 81 |
{ |
| 82 |
$this->didThrow = true; |
| 83 |
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* {@inheritdoc} |
| 88 |
*/ |
| 89 |
public function getContent(): string |
| 90 |
{ |
| 91 |
$this->didThrow = true; |
| 92 |
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* {@inheritdoc} |
| 97 |
*/ |
| 98 |
public function getOffset(): int |
| 99 |
{ |
| 100 |
return $this->offset; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* {@inheritdoc} |
| 105 |
*/ |
| 106 |
public function getError(): ?string |
| 107 |
{ |
| 108 |
return $this->errorMessage; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @return bool Whether the wrapped error has been thrown or not |
| 113 |
*/ |
| 114 |
public function didThrow(bool $didThrow = null): bool |
| 115 |
{ |
| 116 |
if (null !== $didThrow && $this->didThrow !== $didThrow) { |
| 117 |
return !$this->didThrow = $didThrow; |
| 118 |
} |
| 119 |
|
| 120 |
return $this->didThrow; |
| 121 |
} |
| 122 |
|
| 123 |
public function __sleep(): array |
| 124 |
{ |
| 125 |
throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
| 126 |
} |
| 127 |
|
| 128 |
public function __wakeup() |
| 129 |
{ |
| 130 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
| 131 |
} |
| 132 |
|
| 133 |
public function __destruct() |
| 134 |
{ |
| 135 |
if (!$this->didThrow) { |
| 136 |
$this->didThrow = true; |
| 137 |
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|