PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / Handler / MockHandler.php

MockHandler.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at vendor_prefixed/guzzlehttp/guzzle/src/Handler/MockHandler.php

175 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp\Handler;
4
5 use YoastSEO_Vendor\GuzzleHttp\Exception\RequestException;
6 use YoastSEO_Vendor\GuzzleHttp\HandlerStack;
7 use YoastSEO_Vendor\GuzzleHttp\Promise as P;
8 use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface;
9 use YoastSEO_Vendor\GuzzleHttp\TransferStats;
10 use YoastSEO_Vendor\GuzzleHttp\Utils;
11 use YoastSEO_Vendor\Psr\Http\Message\RequestInterface;
12 use YoastSEO_Vendor\Psr\Http\Message\ResponseInterface;
13 use YoastSEO_Vendor\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) : \YoastSEO_Vendor\GuzzleHttp\HandlerStack
50 {
51 return \YoastSEO_Vendor\GuzzleHttp\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(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) : \YoastSEO_Vendor\GuzzleHttp\Promise\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 \YoastSEO_Vendor\GuzzleHttp\Exception\RequestException($msg, $request, $response, $e);
91 }
92 }
93 if (\is_callable($response)) {
94 $response = $response($request, $options);
95 }
96 $response = $response instanceof \Throwable ? \YoastSEO_Vendor\GuzzleHttp\Promise\Create::rejectionFor($response) : \YoastSEO_Vendor\GuzzleHttp\Promise\Create::promiseFor($response);
97 return $response->then(function (?\YoastSEO_Vendor\Psr\Http\Message\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 \YoastSEO_Vendor\Psr\Http\Message\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 \YoastSEO_Vendor\GuzzleHttp\Promise\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 \YoastSEO_Vendor\Psr\Http\Message\ResponseInterface || $value instanceof \Throwable || $value instanceof \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface || \is_callable($value)) {
132 $this->queue[] = $value;
133 } else {
134 throw new \TypeError('Expected a Response, Promise, Throwable or callable. Found ' . \YoastSEO_Vendor\GuzzleHttp\Utils::describeType($value));
135 }
136 }
137 }
138 /**
139 * Get the last received request.
140 */
141 public function getLastRequest() : ?\YoastSEO_Vendor\Psr\Http\Message\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(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options, ?\YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response = null, $reason = null) : void
167 {
168 if (isset($options['on_stats'])) {
169 $transferTime = $options['transfer_time'] ?? 0;
170 $stats = new \YoastSEO_Vendor\GuzzleHttp\TransferStats($request, $response, $transferTime, $reason);
171 $options['on_stats']($stats);
172 }
173 }
174 }
175