-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterator.php
363 lines (324 loc) · 8.94 KB
/
Iterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
namespace KrystalCode\ApiIterator;
/**
* Default implementation of the API iterator.
*
* @I Support alternative cache backends
* type : improvement
* priority : normal
* labels : cache
* @I Support offset
* type : improvement
* priority : normal
* labels : iterator
* @I Support retries on throwables when calling `list` on the client
* type : feature
* priority : normal
* labels : error-handling, iterator
*/
class Iterator implements IteratorInterface
{
/**
* Whether to use cached results, when available.
*
* Results fetched are stored in the iterator object and do not persist
* across iterators.
*
* @var bool
*/
protected $cache;
/**
* The seconds/nanoseconds to sleep after requesting a page.
*
* Some API have rate limits that could be hit when iterating over a large
* number of pages without a delay between page requests.
*
* For example:
* - API has a limit of 10 requests per second.
* - There's 50 pages for the query.
* - API responds to each request fast e.g. in milliseconds.
* - There's no delay between requesting the next page i.e. processing the
* results is also fast.
*
* In such cases, looping over the iterator will result in hitting the API
* rate limits and some of the pages failing to be fetched.
*
* Providing a delay will instruct the iterator to sleep after fetching a
* page for that amount of time. For example, in the case above the delay
* could be set to 0 seconds and 100000000 nanoseconds (i.e. 0.1 seconds)
* which will ensure that the 10 requests per second will not be exceeded
* regardless of response and processing times.
*
* The delay must be given as an array containing the number of seconds in
* its first element and the number of nanoseconds in its second element.
*
* In the example above that would be [0, 100000000];
*
* @var array
*
* @see time_nanosleep()
*/
protected $delay;
/**
* The client that will be used to make requests to the API.
*
* @var \KrystalCode\ApiIterator\ClientInterface
*/
protected $client;
/**
* Holds the items fetched from the API, indexed by page index.
*
* @var \CachingIterator
*/
protected $pages = [];
/**
* The total number of pages.
*
* @var int
*/
protected $count;
/**
* The current iterator position i.e. the current page index.
*
* @var int
*/
protected $position = 1;
/**
* The number of items to fetch per page.
*
* @var int
*/
protected $limit = 100;
/**
* An associative array containing query parameters to add to the requests.
*
* @var array
*/
protected $query = [];
/**
* Constructs a new Iterator object.
*
* @param \KrystalCode\ApiIterator\ClientInterface $client
* The client that will be used to make requests to the API.
* @param int $pageIndex
* The index of the page to start the iterator at.
* @param int $limit
* The number of items to fetch per page.
* @param array $query
* An associative array containing additional query parameters to add to
* the requests.
* @param bool $cache
* Whether to reuse cached results or not.
* @param array $delay
* A pair of seconds/nanoseconds that will determine the delay after
* fetching a page.
*/
public function __construct(
ClientInterface $client,
int $pageIndex = null,
int $limit = null,
array $query = [],
bool $cache = true,
array $delay = null
) {
$this->client = $client;
if ($pageIndex) {
$this->position = $pageIndex;
}
if ($limit) {
$this->limit = $limit;
}
$this->query = $query;
$this->cache = $cache;
if ($delay) {
$this->delay = $delay;
$this->validateDelay();
}
}
/**
* {@inheritdoc}
*
* @throws \InvalidArgumentException
* If the current page index is an invalid position.
*/
public function current(): \CachingIterator
{
if (!$this->valid()) {
throw new \InvalidArgumentException(
sprintf(
'Page "%s" is either an invalid page index or it exceeds the total number of pages available.',
$pageIndex
)
);
}
if ($this->cache && isset($this->pages[$this->position])) {
$this->pages[$this->position]->rewind();
return $this->pages[$this->position];
}
[
$this->pages[$this->position],
$count,
$this->query
] = $this->client->list(
[
'bypass_iterator' => true,
'page' => $this->position,
'limit' => $this->limit,
],
$this->query
);
// `false` means that we have reached the last page.
if ($count === false) {
$this->count = $this->position;
}
// If we have a number, we must have been given the total number of
// pages or total count in the response. `null` means that we don't
// know yet.
elseif ($count !== null) {
$this->count = $count;
}
$this->pages[$this->position]->rewind();
if ($this->delay !== null) {
time_nanosleep($this->delay[0], $this->delay[1]);
}
return $this->pages[$this->position];
}
/**
* {@inheritdoc}
*/
public function key(): int
{
return $this->position;
}
/**
* {@inheritdoc}
*/
public function next(): void
{
$this->move($this->position + 1);
}
/**
* {@inheritdoc}
*/
public function rewind(): void
{
$this->position = 1;
}
/**
* {@inheritdoc}
*/
public function valid(): bool
{
if ($this->position < 1) {
return false;
}
// We don't always know the total number of pages.
// See the comments in the `count` method.
if ($this->count !== null && $this->position > $this->count) {
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function setKey(int $pageIndex): void
{
$this->position = $pageIndex;
}
/**
* {@inheritdoc}
*/
public function count(): ?int
{
return $this->count;
}
/**
* {@inheritdoc}
*/
public function setCount(int $nbPages): void
{
$this->count = $nbPages;
}
/**
* {@inheritdoc}
*/
public function cache(): bool
{
return $this->cache;
}
/**
* {@inheritdoc}
*/
public function setCache(bool $cache): void
{
$this->cache = $cache;
}
/**
* {@inheritdoc}
*/
public function get(int $pageIndex): \CachingIterator
{
if ($this->position !== $pageIndex) {
$this->move($pageIndex);
}
return $this->current();
}
/**
* {@inheritdoc}
*/
public function move(int $pageIndex): void
{
// If we are not using cached results, let's remove them to reduce
// memory consumption.
// @I Write tests for whether items are removed when cache is disabled
// type : task
// priority : low
// labels : testing
if (!$this->cache && $this->pages[$this->position]) {
$this->pages[$this->position] = null;
}
$this->position = $pageIndex;
}
/**
* {@inheritdoc}
*/
public function getAllItems()
{
$this->rewind();
$items = [];
while ($this->valid()) {
foreach ($this->current() as $item) {
$items[] = $item;
}
$this->next();
}
$this->rewind();
return $items;
}
/**
* Validates that the iterator delay is in the expected format.
*
* If set, it must be an array containing the seconds and nanoseconds as
* integer numbers, as expected by time_nanosleep().
*
* @throws \InvalidArgumentException
* When the delay is set but in an incorrect format.
*/
protected function validateDelay()
{
if (!isset($this->delay)) {
return;
}
if (!isset($this->delay[0]) || !is_int($this->delay[0])) {
throw new \InvalidArgumentException(
'You must provide the seconds of the delay as an integer.'
);
}
if (!isset($this->delay[1]) || !is_int($this->delay[1])) {
throw new \InvalidArgumentException(
'You must provide the nanoseconds of the delay as an integer.'
);
}
}
}