PluginProbe
Media Cloud Sync / 1.2.10
Media Cloud Sync v1.2.10
1.4.1 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 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / ResultPaginator.php

ResultPaginator.php in Media Cloud Sync 1.2.10, at includes/sdk/s3/Aws/ResultPaginator.php

144 lines 5.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\Aws;
4
5 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise;
6 /**
7 * Iterator that yields each page of results of a pageable operation.
8 */
9 class ResultPaginator implements \Iterator
10 {
11 /** @var AwsClientInterface Client performing operations. */
12 private $client;
13 /** @var string Name of the operation being paginated. */
14 private $operation;
15 /** @var array Args for the operation. */
16 private $args;
17 /** @var array Configuration for the paginator. */
18 private $config;
19 /** @var Result Most recent result from the client. */
20 private $result;
21 /** @var string|array Next token to use for pagination. */
22 private $nextToken;
23 /** @var int Number of operations/requests performed. */
24 private $requestCount = 0;
25 /**
26 * @param AwsClientInterface $client
27 * @param string $operation
28 * @param array $args
29 * @param array $config
30 */
31 public function __construct(AwsClientInterface $client, $operation, array $args, array $config)
32 {
33 $this->client = $client;
34 $this->operation = $operation;
35 $this->args = $args;
36 $this->config = $config;
37 }
38 /**
39 * Runs a paginator asynchronously and uses a callback to handle results.
40 *
41 * The callback should have the signature: function (Aws\Result $result).
42 * A non-null return value from the callback will be yielded by the
43 * promise. This means that you can return promises from the callback that
44 * will need to be resolved before continuing iteration over the remaining
45 * items, essentially merging in other promises to the iteration. The last
46 * non-null value returned by the callback will be the result that fulfills
47 * the promise to any downstream promises.
48 *
49 * @param callable $handleResult Callback for handling each page of results.
50 * The callback accepts the result that was
51 * yielded as a single argument. If the
52 * callback returns a promise, the promise
53 * will be merged into the coroutine.
54 *
55 * @return Promise\Promise
56 */
57 public function each(callable $handleResult)
58 {
59 return Promise\Coroutine::of(function () use($handleResult) {
60 $nextToken = null;
61 do {
62 $command = $this->createNextCommand($this->args, $nextToken);
63 $result = (yield $this->client->executeAsync($command));
64 $nextToken = $this->determineNextToken($result);
65 $retVal = $handleResult($result);
66 if ($retVal !== null) {
67 (yield Promise\Create::promiseFor($retVal));
68 }
69 } while ($nextToken);
70 });
71 }
72 /**
73 * Returns an iterator that iterates over the values of applying a JMESPath
74 * search to each result yielded by the iterator as a flat sequence.
75 *
76 * @param string $expression JMESPath expression to apply to each result.
77 *
78 * @return \Iterator
79 */
80 public function search($expression)
81 {
82 // Apply JMESPath expression on each result, but as a flat sequence.
83 return flatmap($this, function (Result $result) use($expression) {
84 return (array) $result->search($expression);
85 });
86 }
87 /**
88 * @return Result
89 */
90 #[\ReturnTypeWillChange]
91 public function current()
92 {
93 return $this->valid() ? $this->result : \false;
94 }
95 #[\ReturnTypeWillChange]
96 public function key()
97 {
98 return $this->valid() ? $this->requestCount - 1 : null;
99 }
100 #[\ReturnTypeWillChange]
101 public function next()
102 {
103 $this->result = null;
104 }
105 #[\ReturnTypeWillChange]
106 public function valid()
107 {
108 if ($this->result) {
109 return \true;
110 }
111 if ($this->nextToken || !$this->requestCount) {
112 $this->result = $this->client->execute($this->createNextCommand($this->args, $this->nextToken));
113 $this->nextToken = $this->determineNextToken($this->result);
114 $this->requestCount++;
115 return \true;
116 }
117 return \false;
118 }
119 #[\ReturnTypeWillChange]
120 public function rewind()
121 {
122 $this->requestCount = 0;
123 $this->nextToken = null;
124 $this->result = null;
125 }
126 private function createNextCommand(array $args, array $nextToken = null)
127 {
128 return $this->client->getCommand($this->operation, \array_merge($args, $nextToken ?: []));
129 }
130 private function determineNextToken(Result $result)
131 {
132 if (!$this->config['output_token']) {
133 return null;
134 }
135 if ($this->config['more_results'] && !$result->search($this->config['more_results'])) {
136 return null;
137 }
138 $nextToken = \is_scalar($this->config['output_token']) ? [$this->config['input_token'] => $this->config['output_token']] : \array_combine($this->config['input_token'], $this->config['output_token']);
139 return \array_filter(\array_map(function ($outputToken) use($result) {
140 return $result->search($outputToken);
141 }, $nextToken));
142 }
143 }
144