PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Handler / MockHandler.php

MockHandler.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/Handler/MockHandler.php

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