|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Resource; |
|
4
|
|
|
|
|
5
|
|
|
use OpenStack\Common\Transport\Utils; |
|
6
|
|
|
|
|
7
|
|
|
class Iterator |
|
8
|
|
|
{ |
|
9
|
|
|
private $requestFn; |
|
10
|
|
|
private $resourceFn; |
|
11
|
|
|
private $limit; |
|
12
|
|
|
private $count; |
|
13
|
|
|
private $resourcesKey; |
|
14
|
|
|
private $markerKey; |
|
15
|
|
|
private $mapFn; |
|
16
|
|
|
private $currentMarker; |
|
17
|
|
|
|
|
18
|
33 |
|
public function __construct(array $options, callable $requestFn, callable $resourceFn) |
|
19
|
|
|
{ |
|
20
|
33 |
|
$this->limit = isset($options['limit']) ? $options['limit'] : false; |
|
21
|
33 |
|
$this->count = 0; |
|
22
|
|
|
|
|
23
|
33 |
|
if (isset($options['resourcesKey'])) { |
|
24
|
33 |
|
$this->resourcesKey = $options['resourcesKey']; |
|
25
|
33 |
|
} |
|
26
|
|
|
|
|
27
|
33 |
|
if (isset($options['markerKey'])) { |
|
28
|
8 |
|
$this->markerKey = $options['markerKey']; |
|
29
|
8 |
|
} |
|
30
|
|
|
|
|
31
|
33 |
|
if (isset($options['mapFn']) && is_callable($options['mapFn'])) { |
|
32
|
1 |
|
$this->mapFn = $options['mapFn']; |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
33 |
|
$this->requestFn = $requestFn; |
|
36
|
33 |
|
$this->resourceFn = $resourceFn; |
|
37
|
33 |
|
} |
|
38
|
|
|
|
|
39
|
33 |
|
private function fetchResources() |
|
40
|
|
|
{ |
|
41
|
33 |
|
if ($this->shouldNotSendAnotherRequest()) { |
|
42
|
29 |
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
33 |
|
$response = call_user_func($this->requestFn, $this->currentMarker); |
|
46
|
|
|
|
|
47
|
33 |
|
$json = Utils::flattenJson(Utils::jsonDecode($response), $this->resourcesKey); |
|
48
|
|
|
|
|
49
|
33 |
|
if ($response->getStatusCode() === 204 || empty($json)) { |
|
50
|
4 |
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
33 |
|
return $json; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
33 |
|
private function assembleResource(array $data) |
|
57
|
|
|
{ |
|
58
|
33 |
|
$resource = call_user_func($this->resourceFn, $data); |
|
59
|
|
|
|
|
60
|
|
|
// Invoke user-provided fn if provided |
|
61
|
33 |
|
if ($this->mapFn) { |
|
62
|
1 |
|
call_user_func_array($this->mapFn, [&$resource]); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Update marker if operation supports it |
|
66
|
33 |
|
if ($this->markerKey) { |
|
67
|
8 |
|
$this->currentMarker = $resource->{$this->markerKey}; |
|
68
|
8 |
|
} |
|
69
|
|
|
|
|
70
|
33 |
|
return $resource; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
33 |
|
private function totalReached() |
|
74
|
|
|
{ |
|
75
|
33 |
|
return $this->limit && $this->count >= $this->limit; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
33 |
|
private function shouldNotSendAnotherRequest() |
|
79
|
|
|
{ |
|
80
|
33 |
|
return $this->totalReached() || ($this->count > 0 && !$this->markerKey); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
33 |
|
public function __invoke() |
|
84
|
|
|
{ |
|
85
|
33 |
|
while ($resources = $this->fetchResources()) { |
|
86
|
33 |
|
foreach ($resources as $resourceData) { |
|
87
|
33 |
|
if ($this->totalReached()) { |
|
88
|
2 |
|
break; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
33 |
|
$this->count++; |
|
92
|
|
|
|
|
93
|
33 |
|
yield $this->assembleResource($resourceData); |
|
94
|
33 |
|
} |
|
95
|
33 |
|
} |
|
96
|
33 |
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|