← All changes
|
vendor_prefixed/guzzlehttp/guzzle/src/Handler/MockHandler.php
+0
-157
18.6
→
trunk
View file →
| @@ -1,157 +1,0 @@ | ||
| 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\PromiseInterface; | |
| 8 | -use YoastSEO_Vendor\GuzzleHttp\Promise\RejectedPromise; | |
| 9 | -use YoastSEO_Vendor\GuzzleHttp\TransferStats; | |
| 10 | -use YoastSEO_Vendor\Psr\Http\Message\RequestInterface; | |
| 11 | -use YoastSEO_Vendor\Psr\Http\Message\ResponseInterface; | |
| 12 | -/** | |
| 13 | - * Handler that returns responses or throw exceptions from a queue. | |
| 14 | - */ | |
| 15 | -class MockHandler implements \Countable | |
| 16 | -{ | |
| 17 | - private $queue = []; | |
| 18 | - private $lastRequest; | |
| 19 | - private $lastOptions; | |
| 20 | - private $onFulfilled; | |
| 21 | - private $onRejected; | |
| 22 | - /** | |
| 23 | - * Creates a new MockHandler that uses the default handler stack list of | |
| 24 | - * middlewares. | |
| 25 | - * | |
| 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 | |
| 31 | - */ | |
| 32 | - public static function createWithMiddleware(array $queue = null, callable $onFulfilled = null, callable $onRejected = null) | |
| 33 | - { | |
| 34 | - return \YoastSEO_Vendor\GuzzleHttp\HandlerStack::create(new self($queue, $onFulfilled, $onRejected)); | |
| 35 | - } | |
| 36 | - /** | |
| 37 | - * The passed in value must be an array of | |
| 38 | - * {@see Psr7\Http\Message\ResponseInterface} objects, Exceptions, | |
| 39 | - * callables, or Promises. | |
| 40 | - * | |
| 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. | |
| 44 | - */ | |
| 45 | - public function __construct(array $queue = null, callable $onFulfilled = null, callable $onRejected = null) | |
| 46 | - { | |
| 47 | - $this->onFulfilled = $onFulfilled; | |
| 48 | - $this->onRejected = $onRejected; | |
| 49 | - if ($queue) { | |
| 50 | - \call_user_func_array([$this, 'append'], $queue); | |
| 51 | - } | |
| 52 | - } | |
| 53 | - public function __invoke(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) | |
| 54 | - { | |
| 55 | - if (!$this->queue) { | |
| 56 | - throw new \OutOfBoundsException('Mock queue is empty'); | |
| 57 | - } | |
| 58 | - if (isset($options['delay']) && \is_numeric($options['delay'])) { | |
| 59 | - \usleep($options['delay'] * 1000); | |
| 60 | - } | |
| 61 | - $this->lastRequest = $request; | |
| 62 | - $this->lastOptions = $options; | |
| 63 | - $response = \array_shift($this->queue); | |
| 64 | - if (isset($options['on_headers'])) { | |
| 65 | - if (!\is_callable($options['on_headers'])) { | |
| 66 | - throw new \InvalidArgumentException('on_headers must be callable'); | |
| 67 | - } | |
| 68 | - try { | |
| 69 | - $options['on_headers']($response); | |
| 70 | - } catch (\Exception $e) { | |
| 71 | - $msg = 'An error was encountered during the on_headers event'; | |
| 72 | - $response = new \YoastSEO_Vendor\GuzzleHttp\Exception\RequestException($msg, $request, $response, $e); | |
| 73 | - } | |
| 74 | - } | |
| 75 | - if (\is_callable($response)) { | |
| 76 | - $response = \call_user_func($response, $request, $options); | |
| 77 | - } | |
| 78 | - $response = $response instanceof \Exception ? \YoastSEO_Vendor\GuzzleHttp\Promise\rejection_for($response) : \YoastSEO_Vendor\GuzzleHttp\Promise\promise_for($response); | |
| 79 | - return $response->then(function ($value) use($request, $options) { | |
| 80 | - $this->invokeStats($request, $options, $value); | |
| 81 | - if ($this->onFulfilled) { | |
| 82 | - \call_user_func($this->onFulfilled, $value); | |
| 83 | - } | |
| 84 | - if (isset($options['sink'])) { | |
| 85 | - $contents = (string) $value->getBody(); | |
| 86 | - $sink = $options['sink']; | |
| 87 | - if (\is_resource($sink)) { | |
| 88 | - \fwrite($sink, $contents); | |
| 89 | - } elseif (\is_string($sink)) { | |
| 90 | - \file_put_contents($sink, $contents); | |
| 91 | - } elseif ($sink instanceof \YoastSEO_Vendor\Psr\Http\Message\StreamInterface) { | |
| 92 | - $sink->write($contents); | |
| 93 | - } | |
| 94 | - } | |
| 95 | - return $value; | |
| 96 | - }, function ($reason) use($request, $options) { | |
| 97 | - $this->invokeStats($request, $options, null, $reason); | |
| 98 | - if ($this->onRejected) { | |
| 99 | - \call_user_func($this->onRejected, $reason); | |
| 100 | - } | |
| 101 | - return \YoastSEO_Vendor\GuzzleHttp\Promise\rejection_for($reason); | |
| 102 | - }); | |
| 103 | - } | |
| 104 | - /** | |
| 105 | - * Adds one or more variadic requests, exceptions, callables, or promises | |
| 106 | - * to the queue. | |
| 107 | - */ | |
| 108 | - public function append() | |
| 109 | - { | |
| 110 | - foreach (\func_get_args() as $value) { | |
| 111 | - if ($value instanceof \YoastSEO_Vendor\Psr\Http\Message\ResponseInterface || $value instanceof \Exception || $value instanceof \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface || \is_callable($value)) { | |
| 112 | - $this->queue[] = $value; | |
| 113 | - } else { | |
| 114 | - throw new \InvalidArgumentException('Expected a response or ' . 'exception. Found ' . \YoastSEO_Vendor\GuzzleHttp\describe_type($value)); | |
| 115 | - } | |
| 116 | - } | |
| 117 | - } | |
| 118 | - /** | |
| 119 | - * Get the last received request. | |
| 120 | - * | |
| 121 | - * @return RequestInterface | |
| 122 | - */ | |
| 123 | - public function getLastRequest() | |
| 124 | - { | |
| 125 | - return $this->lastRequest; | |
| 126 | - } | |
| 127 | - /** | |
| 128 | - * Get the last received request options. | |
| 129 | - * | |
| 130 | - * @return array | |
| 131 | - */ | |
| 132 | - public function getLastOptions() | |
| 133 | - { | |
| 134 | - return $this->lastOptions; | |
| 135 | - } | |
| 136 | - /** | |
| 137 | - * Returns the number of remaining items in the queue. | |
| 138 | - * | |
| 139 | - * @return int | |
| 140 | - */ | |
| 141 | - public function count() | |
| 142 | - { | |
| 143 | - return \count($this->queue); | |
| 144 | - } | |
| 145 | - public function reset() | |
| 146 | - { | |
| 147 | - $this->queue = []; | |
| 148 | - } | |
| 149 | - private function invokeStats(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options, \YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response = null, $reason = null) | |
| 150 | - { | |
| 151 | - if (isset($options['on_stats'])) { | |
| 152 | - $transferTime = isset($options['transfer_time']) ? $options['transfer_time'] : 0; | |
| 153 | - $stats = new \YoastSEO_Vendor\GuzzleHttp\TransferStats($request, $response, $transferTime, $reason); | |
| 154 | - \call_user_func($options['on_stats'], $stats); | |
| 155 | - } | |
| 156 | - } | |
| 157 | -} | |