Exception
3 years ago
HttpClientPool
3 years ago
Plugin
3 years ago
BatchClient.php
3 years ago
BatchResult.php
3 years ago
Deferred.php
3 years ago
EmulatedHttpAsyncClient.php
3 years ago
EmulatedHttpClient.php
3 years ago
FlexibleHttpClient.php
3 years ago
HttpAsyncClientDecorator.php
3 years ago
HttpAsyncClientEmulator.php
3 years ago
HttpClientDecorator.php
3 years ago
HttpClientEmulator.php
3 years ago
HttpClientPool.php
3 years ago
HttpClientPoolItem.php
3 years ago
HttpClientRouter.php
3 years ago
HttpMethodsClient.php
3 years ago
Plugin.php
3 years ago
PluginClient.php
3 years ago
PluginClientFactory.php
3 years ago
VersionBridgeClient.php
3 years ago
BatchResult.php
182 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaHttp\Client\Common; |
| 4 | |
| 5 | use AmeliaHttp\Client\Exception; |
| 6 | use AmeliaPsr\Http\Message\RequestInterface; |
| 7 | use AmeliaPsr\Http\Message\ResponseInterface; |
| 8 | |
| 9 | /** |
| 10 | * Responses and exceptions returned from parallel request execution. |
| 11 | * |
| 12 | * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> |
| 13 | */ |
| 14 | final class BatchResult |
| 15 | { |
| 16 | /** |
| 17 | * @var \SplObjectStorage |
| 18 | */ |
| 19 | private $responses; |
| 20 | |
| 21 | /** |
| 22 | * @var \SplObjectStorage |
| 23 | */ |
| 24 | private $exceptions; |
| 25 | |
| 26 | public function __construct() |
| 27 | { |
| 28 | $this->responses = new \SplObjectStorage(); |
| 29 | $this->exceptions = new \SplObjectStorage(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Checks if there are any successful responses at all. |
| 34 | * |
| 35 | * @return bool |
| 36 | */ |
| 37 | public function hasResponses() |
| 38 | { |
| 39 | return $this->responses->count() > 0; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns all successful responses. |
| 44 | * |
| 45 | * @return ResponseInterface[] |
| 46 | */ |
| 47 | public function getResponses() |
| 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 | * @param RequestInterface $request |
| 62 | * |
| 63 | * @return bool |
| 64 | */ |
| 65 | public function isSuccessful(RequestInterface $request) |
| 66 | { |
| 67 | return $this->responses->contains($request); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the response for a successful request. |
| 72 | * |
| 73 | * @param RequestInterface $request |
| 74 | * |
| 75 | * @return ResponseInterface |
| 76 | * |
| 77 | * @throws \UnexpectedValueException If request was not part of the batch or failed |
| 78 | */ |
| 79 | public function getResponseFor(RequestInterface $request) |
| 80 | { |
| 81 | try { |
| 82 | return $this->responses[$request]; |
| 83 | } catch (\UnexpectedValueException $e) { |
| 84 | throw new \UnexpectedValueException('Request not found', $e->getCode(), $e); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Adds a response in an immutable way. |
| 90 | * |
| 91 | * @param RequestInterface $request |
| 92 | * @param ResponseInterface $response |
| 93 | * |
| 94 | * @return BatchResult the new BatchResult with this request-response pair added to it |
| 95 | */ |
| 96 | public function addResponse(RequestInterface $request, ResponseInterface $response) |
| 97 | { |
| 98 | $new = clone $this; |
| 99 | $new->responses->attach($request, $response); |
| 100 | |
| 101 | return $new; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Checks if there are any unsuccessful requests at all. |
| 106 | * |
| 107 | * @return bool |
| 108 | */ |
| 109 | public function hasExceptions() |
| 110 | { |
| 111 | return $this->exceptions->count() > 0; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns all exceptions for the unsuccessful requests. |
| 116 | * |
| 117 | * @return Exception[] |
| 118 | */ |
| 119 | public function getExceptions() |
| 120 | { |
| 121 | $exceptions = []; |
| 122 | |
| 123 | foreach ($this->exceptions as $request) { |
| 124 | $exceptions[] = $this->exceptions[$request]; |
| 125 | } |
| 126 | |
| 127 | return $exceptions; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Checks if there is an exception for a request, meaning the request failed. |
| 132 | * |
| 133 | * @param RequestInterface $request |
| 134 | * |
| 135 | * @return bool |
| 136 | */ |
| 137 | public function isFailed(RequestInterface $request) |
| 138 | { |
| 139 | return $this->exceptions->contains($request); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns the exception for a failed request. |
| 144 | * |
| 145 | * @param RequestInterface $request |
| 146 | * |
| 147 | * @return Exception |
| 148 | * |
| 149 | * @throws \UnexpectedValueException If request was not part of the batch or was successful |
| 150 | */ |
| 151 | public function getExceptionFor(RequestInterface $request) |
| 152 | { |
| 153 | try { |
| 154 | return $this->exceptions[$request]; |
| 155 | } catch (\UnexpectedValueException $e) { |
| 156 | throw new \UnexpectedValueException('Request not found', $e->getCode(), $e); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Adds an exception in an immutable way. |
| 162 | * |
| 163 | * @param RequestInterface $request |
| 164 | * @param Exception $exception |
| 165 | * |
| 166 | * @return BatchResult the new BatchResult with this request-exception pair added to it |
| 167 | */ |
| 168 | public function addException(RequestInterface $request, Exception $exception) |
| 169 | { |
| 170 | $new = clone $this; |
| 171 | $new->exceptions->attach($request, $exception); |
| 172 | |
| 173 | return $new; |
| 174 | } |
| 175 | |
| 176 | public function __clone() |
| 177 | { |
| 178 | $this->responses = clone $this->responses; |
| 179 | $this->exceptions = clone $this->exceptions; |
| 180 | } |
| 181 | } |
| 182 |