← All changes
|
includes/sdk/s3/GuzzleHttp/Handler/MockHandler.php
+58
-41
1.0.2
→
1.4.1
View file →
| @@ -3,61 +3,79 @@ | ||
| 3 | 3 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Handler; |
| 4 | 4 | |
| 5 | 5 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Exception\RequestException; |
| 6 | 6 | use Dudlewebs\WPMCS\s3\GuzzleHttp\HandlerStack; |
| 7 | +use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise as P; | |
| 7 | 8 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface; |
| 8 | -use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\RejectedPromise; | |
| 9 | 9 | use Dudlewebs\WPMCS\s3\GuzzleHttp\TransferStats; |
| 10 | +use Dudlewebs\WPMCS\s3\GuzzleHttp\Utils; | |
| 10 | 11 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 11 | 12 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface; |
| 13 | +use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; | |
| 12 | 14 | /** |
| 13 | 15 | * Handler that returns responses or throw exceptions from a queue. |
| 16 | + * | |
| 17 | + * @final | |
| 14 | 18 | */ |
| 15 | 19 | class MockHandler implements \Countable |
| 16 | 20 | { |
| 21 | + /** | |
| 22 | + * @var array | |
| 23 | + */ | |
| 17 | 24 | private $queue = []; |
| 25 | + /** | |
| 26 | + * @var RequestInterface|null | |
| 27 | + */ | |
| 18 | 28 | private $lastRequest; |
| 19 | - private $lastOptions; | |
| 29 | + /** | |
| 30 | + * @var array | |
| 31 | + */ | |
| 32 | + private $lastOptions = []; | |
| 33 | + /** | |
| 34 | + * @var callable|null | |
| 35 | + */ | |
| 20 | 36 | private $onFulfilled; |
| 37 | + /** | |
| 38 | + * @var callable|null | |
| 39 | + */ | |
| 21 | 40 | private $onRejected; |
| 22 | 41 | /** |
| 23 | 42 | * Creates a new MockHandler that uses the default handler stack list of |
| 24 | 43 | * middlewares. |
| 25 | 44 | * |
| 26 | - * @param array $queue Array of responses, callables, or exceptions. | |
| 27 | - * @param callable $onFulfilled Callback to invoke when the return value is fulfilled. | |
| 28 | - * @param callable $onRejected Callback to invoke when the return value is rejected. | |
| 29 | - * | |
| 30 | - * @return HandlerStack | |
| 45 | + * @param array|null $queue Array of responses, callables, or exceptions. | |
| 46 | + * @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled. | |
| 47 | + * @param callable|null $onRejected Callback to invoke when the return value is rejected. | |
| 31 | 48 | */ |
| 32 | - public static function createWithMiddleware(array $queue = null, callable $onFulfilled = null, callable $onRejected = null) | |
| 49 | + public static function createWithMiddleware(?array $queue = null, ?callable $onFulfilled = null, ?callable $onRejected = null) : HandlerStack | |
| 33 | 50 | { |
| 34 | 51 | return HandlerStack::create(new self($queue, $onFulfilled, $onRejected)); |
| 35 | 52 | } |
| 36 | 53 | /** |
| 37 | 54 | * The passed in value must be an array of |
| 38 | - * {@see Psr7\Http\Message\ResponseInterface} objects, Exceptions, | |
| 55 | + * {@see ResponseInterface} objects, Exceptions, | |
| 39 | 56 | * callables, or Promises. |
| 40 | 57 | * |
| 41 | - * @param array $queue | |
| 42 | - * @param callable $onFulfilled Callback to invoke when the return value is fulfilled. | |
| 43 | - * @param callable $onRejected Callback to invoke when the return value is rejected. | |
| 58 | + * @param array<int, mixed>|null $queue The parameters to be passed to the append function, as an indexed array. | |
| 59 | + * @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled. | |
| 60 | + * @param callable|null $onRejected Callback to invoke when the return value is rejected. | |
| 44 | 61 | */ |
| 45 | - public function __construct(array $queue = null, callable $onFulfilled = null, callable $onRejected = null) | |
| 62 | + public function __construct(?array $queue = null, ?callable $onFulfilled = null, ?callable $onRejected = null) | |
| 46 | 63 | { |
| 47 | 64 | $this->onFulfilled = $onFulfilled; |
| 48 | 65 | $this->onRejected = $onRejected; |
| 49 | 66 | if ($queue) { |
| 50 | - \call_user_func_array([$this, 'append'], $queue); | |
| 67 | + // array_values included for BC | |
| 68 | + $this->append(...\array_values($queue)); | |
| 51 | 69 | } |
| 52 | 70 | } |
| 53 | - public function __invoke(RequestInterface $request, array $options) | |
| 71 | + public function __invoke(RequestInterface $request, array $options) : PromiseInterface | |
| 54 | 72 | { |
| 55 | 73 | if (!$this->queue) { |
| 56 | 74 | throw new \OutOfBoundsException('Mock queue is empty'); |
| 57 | 75 | } |
| 58 | 76 | if (isset($options['delay']) && \is_numeric($options['delay'])) { |
| 59 | - \usleep($options['delay'] * 1000); | |
| 77 | + \usleep((int) $options['delay'] * 1000); | |
| 60 | 78 | } |
| 61 | 79 | $this->lastRequest = $request; |
| 62 | 80 | $this->lastOptions = $options; |
| 63 | 81 | $response = \array_shift($this->queue); |
| @@ -72,17 +90,17 @@ | ||
| 72 | 90 | $response = new RequestException($msg, $request, $response, $e); |
| 73 | 91 | } |
| 74 | 92 | } |
| 75 | 93 | if (\is_callable($response)) { |
| 76 | - $response = \call_user_func($response, $request, $options); | |
| 94 | + $response = $response($request, $options); | |
| 77 | 95 | } |
| 78 | - $response = $response instanceof \Exception ? \Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\rejection_for($response) : \Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\promise_for($response); | |
| 79 | - return $response->then(function ($value) use($request, $options) { | |
| 96 | + $response = $response instanceof \Throwable ? P\Create::rejectionFor($response) : P\Create::promiseFor($response); | |
| 97 | + return $response->then(function (?ResponseInterface $value) use($request, $options) { | |
| 80 | 98 | $this->invokeStats($request, $options, $value); |
| 81 | 99 | if ($this->onFulfilled) { |
| 82 | - \call_user_func($this->onFulfilled, $value); | |
| 100 | + ($this->onFulfilled)($value); | |
| 83 | 101 | } |
| 84 | - if (isset($options['sink'])) { | |
| 102 | + if ($value !== null && isset($options['sink'])) { | |
| 85 | 103 | $contents = (string) $value->getBody(); |
| 86 | 104 | $sink = $options['sink']; |
| 87 | 105 | if (\is_resource($sink)) { |
| 88 | 106 | \fwrite($sink, $contents); |
| @@ -87,9 +105,9 @@ | ||
| 87 | 105 | if (\is_resource($sink)) { |
| 88 | 106 | \fwrite($sink, $contents); |
| 89 | 107 | } elseif (\is_string($sink)) { |
| 90 | 108 | \file_put_contents($sink, $contents); |
| 91 | - } elseif ($sink instanceof \Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface) { | |
| 109 | + } elseif ($sink instanceof StreamInterface) { | |
| 92 | 110 | $sink->write($contents); |
| 93 | 111 | } |
| 94 | 112 | } |
| 95 | 113 | return $value; |
| @@ -95,63 +113,62 @@ | ||
| 95 | 113 | return $value; |
| 96 | 114 | }, function ($reason) use($request, $options) { |
| 97 | 115 | $this->invokeStats($request, $options, null, $reason); |
| 98 | 116 | if ($this->onRejected) { |
| 99 | - \call_user_func($this->onRejected, $reason); | |
| 117 | + ($this->onRejected)($reason); | |
| 100 | 118 | } |
| 101 | - return \Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\rejection_for($reason); | |
| 119 | + return P\Create::rejectionFor($reason); | |
| 102 | 120 | }); |
| 103 | 121 | } |
| 104 | 122 | /** |
| 105 | 123 | * Adds one or more variadic requests, exceptions, callables, or promises |
| 106 | 124 | * to the queue. |
| 125 | + * | |
| 126 | + * @param mixed ...$values | |
| 107 | 127 | */ |
| 108 | - public function append() | |
| 128 | + public function append(...$values) : void | |
| 109 | 129 | { |
| 110 | - foreach (\func_get_args() as $value) { | |
| 111 | - if ($value instanceof ResponseInterface || $value instanceof \Exception || $value instanceof PromiseInterface || \is_callable($value)) { | |
| 130 | + foreach ($values as $value) { | |
| 131 | + if ($value instanceof ResponseInterface || $value instanceof \Throwable || $value instanceof PromiseInterface || \is_callable($value)) { | |
| 112 | 132 | $this->queue[] = $value; |
| 113 | 133 | } else { |
| 114 | - throw new \InvalidArgumentException('Expected a response or ' . 'exception. Found ' . \Dudlewebs\WPMCS\s3\GuzzleHttp\describe_type($value)); | |
| 134 | + throw new \TypeError('Expected a Response, Promise, Throwable or callable. Found ' . Utils::describeType($value)); | |
| 115 | 135 | } |
| 116 | 136 | } |
| 117 | 137 | } |
| 118 | 138 | /** |
| 119 | 139 | * Get the last received request. |
| 120 | - * | |
| 121 | - * @return RequestInterface | |
| 122 | 140 | */ |
| 123 | - public function getLastRequest() | |
| 141 | + public function getLastRequest() : ?RequestInterface | |
| 124 | 142 | { |
| 125 | 143 | return $this->lastRequest; |
| 126 | 144 | } |
| 127 | 145 | /** |
| 128 | 146 | * Get the last received request options. |
| 129 | - * | |
| 130 | - * @return array | |
| 131 | 147 | */ |
| 132 | - public function getLastOptions() | |
| 148 | + public function getLastOptions() : array | |
| 133 | 149 | { |
| 134 | 150 | return $this->lastOptions; |
| 135 | 151 | } |
| 136 | 152 | /** |
| 137 | 153 | * Returns the number of remaining items in the queue. |
| 138 | - * | |
| 139 | - * @return int | |
| 140 | 154 | */ |
| 141 | - public function count() | |
| 155 | + public function count() : int | |
| 142 | 156 | { |
| 143 | 157 | return \count($this->queue); |
| 144 | 158 | } |
| 145 | - public function reset() | |
| 159 | + public function reset() : void | |
| 146 | 160 | { |
| 147 | 161 | $this->queue = []; |
| 148 | 162 | } |
| 149 | - private function invokeStats(RequestInterface $request, array $options, ResponseInterface $response = null, $reason = null) | |
| 163 | + /** | |
| 164 | + * @param mixed $reason Promise or reason. | |
| 165 | + */ | |
| 166 | + private function invokeStats(RequestInterface $request, array $options, ?ResponseInterface $response = null, $reason = null) : void | |
| 150 | 167 | { |
| 151 | 168 | if (isset($options['on_stats'])) { |
| 152 | - $transferTime = isset($options['transfer_time']) ? $options['transfer_time'] : 0; | |
| 169 | + $transferTime = $options['transfer_time'] ?? 0; | |
| 153 | 170 | $stats = new TransferStats($request, $response, $transferTime, $reason); |
| 154 | - \call_user_func($options['on_stats'], $stats); | |
| 171 | + $options['on_stats']($stats); | |
| 155 | 172 | } |
| 156 | 173 | } |
| 157 | 174 | } |