PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
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 / Aws / MockHandler.php

MockHandler.php in Media Cloud Sync 1.2.12, at includes/sdk/s3/Aws/MockHandler.php

123 lines 4.0 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\Aws;
4
5 use Dudlewebs\WPMCS\s3\Aws\Exception\AwsException;
6 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise;
7 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\RejectedPromise;
8 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
9 use Exception;
10 /**
11 * Returns promises that are rejected or fulfilled using a queue of
12 * Aws\ResultInterface and Aws\Exception\AwsException objects.
13 */
14 class MockHandler implements \Countable
15 {
16 private $queue;
17 private $lastCommand;
18 private $lastRequest;
19 private $onFulfilled;
20 private $onRejected;
21 /**
22 * The passed in value must be an array of {@see Aws\ResultInterface} or
23 * {@see AwsException} objects that acts as a queue of results or
24 * exceptions to return each time the handler is invoked.
25 *
26 * @param array $resultOrQueue
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 public function __construct(array $resultOrQueue = [], callable $onFulfilled = null, callable $onRejected = null)
31 {
32 $this->onFulfilled = $onFulfilled;
33 $this->onRejected = $onRejected;
34 if ($resultOrQueue) {
35 \call_user_func_array([$this, 'append'], \array_values($resultOrQueue));
36 }
37 }
38 /**
39 * Adds one or more variadic ResultInterface or AwsException objects to the
40 * queue.
41 */
42 public function append()
43 {
44 foreach (\func_get_args() as $value) {
45 if ($value instanceof ResultInterface || $value instanceof Exception || \is_callable($value)) {
46 $this->queue[] = $value;
47 } else {
48 throw new \InvalidArgumentException('Expected an Aws\\ResultInterface or Exception.');
49 }
50 }
51 }
52 /**
53 * Adds one or more \Exception or \Throwable to the queue
54 */
55 public function appendException()
56 {
57 foreach (\func_get_args() as $value) {
58 if ($value instanceof \Exception || $value instanceof \Throwable) {
59 $this->queue[] = $value;
60 } else {
61 throw new \InvalidArgumentException('Expected an \\Exception or \\Throwable.');
62 }
63 }
64 }
65 public function __invoke(CommandInterface $command, RequestInterface $request)
66 {
67 if (!$this->queue) {
68 $last = $this->lastCommand ? ' The last command sent was ' . $this->lastCommand->getName() . '.' : '';
69 throw new \RuntimeException('Mock queue is empty. Trying to send a ' . $command->getName() . ' command failed.' . $last);
70 }
71 $this->lastCommand = $command;
72 $this->lastRequest = $request;
73 $result = \array_shift($this->queue);
74 if (\is_callable($result)) {
75 $result = $result($command, $request);
76 }
77 if ($result instanceof \Exception) {
78 $result = new RejectedPromise($result);
79 } else {
80 // Add an effective URI and statusCode if not present.
81 $meta = $result['@metadata'];
82 if (!isset($meta['effectiveUri'])) {
83 $meta['effectiveUri'] = (string) $request->getUri();
84 }
85 if (!isset($meta['statusCode'])) {
86 $meta['statusCode'] = 200;
87 }
88 $result['@metadata'] = $meta;
89 $result = Promise\Create::promiseFor($result);
90 }
91 $result->then($this->onFulfilled, $this->onRejected);
92 return $result;
93 }
94 /**
95 * Get the last received request.
96 *
97 * @return RequestInterface
98 */
99 public function getLastRequest()
100 {
101 return $this->lastRequest;
102 }
103 /**
104 * Get the last received command.
105 *
106 * @return CommandInterface
107 */
108 public function getLastCommand()
109 {
110 return $this->lastCommand;
111 }
112 /**
113 * Returns the number of remaining items in the queue.
114 *
115 * @return int
116 */
117 #[\ReturnTypeWillChange]
118 public function count()
119 {
120 return \count($this->queue);
121 }
122 }
123