Debian Codegraph @

lib/3rdParty/composer/symfony/http-client/Chunk/ErrorChunk.php

raw symbols SCIP
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 bool $didThrow = false;
26 private string $errorMessage;
27 private ?\Throwable $error = null;
28
29 public function __construct(
30 private int $offset,
31 \Throwable|string $error,
32 ) {
33 if (\is_string($error)) {
34 $this->errorMessage = $error;
35 } else {
36 $this->error = $error;
37 $this->errorMessage = $error->getMessage();
38 }
39 }
40
41 public function isTimeout(): bool
42 {
43 $this->didThrow = true;
44
45 if (null !== $this->error) {
46 throw new TransportException($this->errorMessage, 0, $this->error);
47 }
48
49 return true;
50 }
51
52 public function isFirst(): bool
53 {
54 $this->didThrow = true;
55 throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
56 }
57
58 public function isLast(): bool
59 {
60 $this->didThrow = true;
61 throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
62 }
63
64 public function getInformationalStatus(): ?array
65 {
66 $this->didThrow = true;
67 throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
68 }
69
70 public function getContent(): string
71 {
72 $this->didThrow = true;
73 throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
74 }
75
76 public function getOffset(): int
77 {
78 return $this->offset;
79 }
80
81 public function getError(): ?string
82 {
83 return $this->errorMessage;
84 }
85
86 public function didThrow(?bool $didThrow = null): bool
87 {
88 if (null !== $didThrow && $this->didThrow !== $didThrow) {
89 return !$this->didThrow = $didThrow;
90 }
91
92 return $this->didThrow;
93 }
94
95 public function __serialize(): array
96 {
97 throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
98 }
99
100 public function __unserialize(array $data): void
101 {
102 throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
103 }
104
105 public function __destruct()
106 {
107 if (!$this->didThrow) {
108 $this->didThrow = true;
109 throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
110 }
111 }
112 }