Client.php
6 months ago
CurlPromise.php
6 months ago
MultiRunner.php
3 years ago
PromiseCore.php
6 months ago
ResponseBuilder.php
6 months ago
PromiseCore.php
243 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaHttp\Client\Curl; |
| 4 | |
| 5 | use AmeliaHttp\Client\Exception; |
| 6 | use AmeliaHttp\Promise\Promise; |
| 7 | use AmeliaVendor\Psr\Http\Message\RequestInterface; |
| 8 | use AmeliaVendor\Psr\Http\Message\ResponseInterface; |
| 9 | |
| 10 | /** |
| 11 | * Shared promises core. |
| 12 | * |
| 13 | * @license http://opensource.org/licenses/MIT MIT |
| 14 | * @author Ми� |
| 15 | аил Красильников <m.krasilnikov@yandex.ru> |
| 16 | */ |
| 17 | class PromiseCore |
| 18 | { |
| 19 | /** |
| 20 | * HTTP request. |
| 21 | * |
| 22 | * @var RequestInterface |
| 23 | */ |
| 24 | private $request; |
| 25 | |
| 26 | /** |
| 27 | * cURL handle. |
| 28 | * |
| 29 | * @var resource |
| 30 | */ |
| 31 | private $handle; |
| 32 | |
| 33 | /** |
| 34 | * Response builder. |
| 35 | * |
| 36 | * @var ResponseBuilder |
| 37 | */ |
| 38 | private $responseBuilder; |
| 39 | |
| 40 | /** |
| 41 | * Promise state. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | private $state; |
| 46 | |
| 47 | /** |
| 48 | * Exception. |
| 49 | * |
| 50 | * @var Exception|null |
| 51 | */ |
| 52 | private $exception = null; |
| 53 | |
| 54 | /** |
| 55 | * Functions to call when a response will be available. |
| 56 | * |
| 57 | * @var callable[] |
| 58 | */ |
| 59 | private $onFulfilled = []; |
| 60 | |
| 61 | /** |
| 62 | * Functions to call when an error happens. |
| 63 | * |
| 64 | * @var callable[] |
| 65 | */ |
| 66 | private $onRejected = []; |
| 67 | |
| 68 | /** |
| 69 | * Create shared core. |
| 70 | * |
| 71 | * @param RequestInterface $request HTTP request. |
| 72 | * @param resource $handle cURL handle. |
| 73 | * @param ResponseBuilder $responseBuilder Response builder. |
| 74 | * |
| 75 | * @throws \InvalidArgumentException If $handle is not a cURL resource. |
| 76 | */ |
| 77 | public function __construct( |
| 78 | RequestInterface $request, |
| 79 | $handle, |
| 80 | ResponseBuilder $responseBuilder |
| 81 | ) { |
| 82 | if (!is_resource($handle)) { |
| 83 | throw new \InvalidArgumentException( |
| 84 | sprintf( |
| 85 | 'Parameter $handle expected to be a cURL resource, %s given', |
| 86 | gettype($handle) |
| 87 | ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | if (get_resource_type($handle) !== 'curl') { |
| 92 | throw new \InvalidArgumentException( |
| 93 | sprintf( |
| 94 | 'Parameter $handle expected to be a cURL resource, %s resource given', |
| 95 | get_resource_type($handle) |
| 96 | ) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | $this->request = $request; |
| 101 | $this->handle = $handle; |
| 102 | $this->responseBuilder = $responseBuilder; |
| 103 | $this->state = Promise::PENDING; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Add on fulfilled callback. |
| 108 | * |
| 109 | * @param callable $callback |
| 110 | */ |
| 111 | public function addOnFulfilled(callable $callback) |
| 112 | { |
| 113 | if ($this->getState() === Promise::PENDING) { |
| 114 | $this->onFulfilled[] = $callback; |
| 115 | } elseif ($this->getState() === Promise::FULFILLED) { |
| 116 | $response = call_user_func($callback, $this->responseBuilder->getResponse()); |
| 117 | if ($response instanceof ResponseInterface) { |
| 118 | $this->responseBuilder->setResponse($response); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Add on rejected callback. |
| 125 | * |
| 126 | * @param callable $callback |
| 127 | */ |
| 128 | public function addOnRejected(callable $callback) |
| 129 | { |
| 130 | if ($this->getState() === Promise::PENDING) { |
| 131 | $this->onRejected[] = $callback; |
| 132 | } elseif ($this->getState() === Promise::REJECTED) { |
| 133 | $this->exception = call_user_func($callback, $this->exception); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Return cURL handle. |
| 139 | * |
| 140 | * @return resource |
| 141 | */ |
| 142 | public function getHandle() |
| 143 | { |
| 144 | return $this->handle; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get the state of the promise, one of PENDING, FULFILLED or REJECTED. |
| 149 | * |
| 150 | * @return string |
| 151 | */ |
| 152 | public function getState() |
| 153 | { |
| 154 | return $this->state; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Return request. |
| 159 | * |
| 160 | * @return RequestInterface |
| 161 | */ |
| 162 | public function getRequest() |
| 163 | { |
| 164 | return $this->request; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Return the value of the promise (fulfilled). |
| 169 | * |
| 170 | * @return ResponseInterface Response Object only when the Promise is fulfilled |
| 171 | */ |
| 172 | public function getResponse() |
| 173 | { |
| 174 | return $this->responseBuilder->getResponse(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Get the reason why the promise was rejected. |
| 179 | * |
| 180 | * If the exception is an instance of AmeliaHttp\Client\Exception\HttpException it will contain |
| 181 | * the response object with the status code and the http reason. |
| 182 | * |
| 183 | * @return Exception Exception Object only when the Promise is rejected |
| 184 | * |
| 185 | * @throws \LogicException When the promise is not rejected |
| 186 | */ |
| 187 | public function getException() |
| 188 | { |
| 189 | if (null === $this->exception) { |
| 190 | throw new \LogicException('Promise is not rejected'); |
| 191 | } |
| 192 | |
| 193 | return $this->exception; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Fulfill promise. |
| 198 | */ |
| 199 | public function fulfill() |
| 200 | { |
| 201 | $this->state = Promise::FULFILLED; |
| 202 | $response = $this->responseBuilder->getResponse(); |
| 203 | try { |
| 204 | $response->getBody()->seek(0); |
| 205 | } catch (\RuntimeException $e) { |
| 206 | $exception = new Exception\TransferException($e->getMessage(), $e->getCode(), $e); |
| 207 | $this->reject($exception); |
| 208 | |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | while (count($this->onFulfilled) > 0) { |
| 213 | $callback = array_shift($this->onFulfilled); |
| 214 | $response = call_user_func($callback, $response); |
| 215 | } |
| 216 | |
| 217 | if ($response instanceof ResponseInterface) { |
| 218 | $this->responseBuilder->setResponse($response); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Reject promise. |
| 224 | * |
| 225 | * @param Exception $exception Reject reason |
| 226 | */ |
| 227 | public function reject(Exception $exception) |
| 228 | { |
| 229 | $this->exception = $exception; |
| 230 | $this->state = Promise::REJECTED; |
| 231 | |
| 232 | while (count($this->onRejected) > 0) { |
| 233 | $callback = array_shift($this->onRejected); |
| 234 | try { |
| 235 | $exception = call_user_func($callback, $this->exception); |
| 236 | $this->exception = $exception; |
| 237 | } catch (Exception $exception) { |
| 238 | $this->exception = $exception; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 |