PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / http / client-common / BatchResult.php

BatchResult.php in DecaLog 4.4.0, at includes/libraries/http/client-common/BatchResult.php

158 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Http\Client\Common;
6
7 use Psr\Http\Client\ClientExceptionInterface;
8 use Psr\Http\Message\RequestInterface;
9 use Psr\Http\Message\ResponseInterface;
10
11 /**
12 * Responses and exceptions returned from parallel request execution.
13 *
14 * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
15 */
16 final class BatchResult
17 {
18 /**
19 * @var \SplObjectStorage<RequestInterface, ResponseInterface>
20 */
21 private $responses;
22
23 /**
24 * @var \SplObjectStorage<RequestInterface, ClientExceptionInterface>
25 */
26 private $exceptions;
27
28 public function __construct()
29 {
30 $this->responses = new \SplObjectStorage();
31 $this->exceptions = new \SplObjectStorage();
32 }
33
34 /**
35 * Checks if there are any successful responses at all.
36 */
37 public function hasResponses(): bool
38 {
39 return $this->responses->count() > 0;
40 }
41
42 /**
43 * Returns all successful responses.
44 *
45 * @return ResponseInterface[]
46 */
47 public function getResponses(): array
48 {
49 $responses = [];
50
51 foreach ($this->responses as $request) {
52 $responses[] = $this->responses[$request];
53 }
54
55 return $responses;
56 }
57
58 /**
59 * Checks if there is a successful response for a request.
60 */
61 public function isSuccessful(RequestInterface $request): bool
62 {
63 return $this->responses->contains($request);
64 }
65
66 /**
67 * Returns the response for a successful request.
68 *
69 * @throws \UnexpectedValueException If request was not part of the batch or failed
70 */
71 public function getResponseFor(RequestInterface $request): ResponseInterface
72 {
73 try {
74 return $this->responses[$request];
75 } catch (\UnexpectedValueException $e) {
76 throw new \UnexpectedValueException('Request not found', $e->getCode(), $e);
77 }
78 }
79
80 /**
81 * Adds a response in an immutable way.
82 *
83 * @return BatchResult the new BatchResult with this request-response pair added to it
84 */
85 public function addResponse(RequestInterface $request, ResponseInterface $response): self
86 {
87 $new = clone $this;
88 $new->responses->attach($request, $response);
89
90 return $new;
91 }
92
93 /**
94 * Checks if there are any unsuccessful requests at all.
95 */
96 public function hasExceptions(): bool
97 {
98 return $this->exceptions->count() > 0;
99 }
100
101 /**
102 * Returns all exceptions for the unsuccessful requests.
103 *
104 * @return ClientExceptionInterface[]
105 */
106 public function getExceptions(): array
107 {
108 $exceptions = [];
109
110 foreach ($this->exceptions as $request) {
111 $exceptions[] = $this->exceptions[$request];
112 }
113
114 return $exceptions;
115 }
116
117 /**
118 * Checks if there is an exception for a request, meaning the request failed.
119 */
120 public function isFailed(RequestInterface $request): bool
121 {
122 return $this->exceptions->contains($request);
123 }
124
125 /**
126 * Returns the exception for a failed request.
127 *
128 * @throws \UnexpectedValueException If request was not part of the batch or was successful
129 */
130 public function getExceptionFor(RequestInterface $request): ClientExceptionInterface
131 {
132 try {
133 return $this->exceptions[$request];
134 } catch (\UnexpectedValueException $e) {
135 throw new \UnexpectedValueException('Request not found', $e->getCode(), $e);
136 }
137 }
138
139 /**
140 * Adds an exception in an immutable way.
141 *
142 * @return BatchResult the new BatchResult with this request-exception pair added to it
143 */
144 public function addException(RequestInterface $request, ClientExceptionInterface $exception): self
145 {
146 $new = clone $this;
147 $new->exceptions->attach($request, $exception);
148
149 return $new;
150 }
151
152 public function __clone()
153 {
154 $this->responses = clone $this->responses;
155 $this->exceptions = clone $this->exceptions;
156 }
157 }
158