Skip to content

Commit 9af21c9

Browse files
authored
Merge pull request #488 from ptmcnally/5374-missing-prices
5374 missing prices
2 parents 9e78da6 + 800fa0a commit 9af21c9

File tree

4 files changed

+43
-92
lines changed

4 files changed

+43
-92
lines changed

src/Traits/PageItems.php

+32
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,36 @@ public function updateEntity($entity)
139139
return $this->loadEntity($responseBody->data);
140140
});
141141
}
142+
143+
/**
144+
* Get child resources for a resource e.g GET resource/:id/$resourceName
145+
* @param $id string ID of the resource to get child resources for
146+
* @param $resourceName string name of the associated resource as specified in the route path
147+
* @param $serializer \callback serializer to use for the child resource
148+
* @param $filters
149+
* @return array
150+
* @throws \GuzzleHttp\Exception\GuzzleException
151+
*/
152+
public function getChildResources($id, $resourceName, $serializer, $filters = [])
153+
{
154+
$pageNumber = 0;
155+
$items = [];
156+
do {
157+
$pageNumber++;
158+
$page = $this->paginatedRequest(
159+
$this->collectionPath . '/' . $id . '/' . $resourceName,
160+
$pageNumber,
161+
15,
162+
$filters
163+
);
164+
165+
$page->serializeWith($serializer);
166+
$items = array_merge(
167+
$items,
168+
$page->getItems()
169+
);
170+
} while ($pageNumber < $page->totalPages());
171+
172+
return $items;
173+
}
142174
}

src/eCloud/AffinityRuleClient.php

+7-26
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,16 @@ public function loadEntity($data)
2525
);
2626
}
2727

28+
/**
29+
* @param $id
30+
* @param $filters
31+
* @return array
32+
*/
2833
public function getMembers($id, $filters = [])
2934
{
30-
$page = $this->paginatedRequest($this->collectionPath . '/' . $id . '/members', 1, 15, $filters);
31-
32-
if ($page->totalItems() == 0) {
33-
return [];
34-
}
35-
36-
$loadEntity = function ($data) {
35+
return $this->getChildResources($id, 'members', function ($data) {
3736
return new AffinityRuleMember($this->apiToFriendly($data, AffinityRuleMember::$entityMap));
38-
};
39-
40-
$page->serializeWith($loadEntity);
41-
42-
$items = $page->getItems();
43-
if ($page->totalPages() == 1) {
44-
return $items;
45-
}
46-
47-
while ($page->pageNumber() < $page->totalPages()) {
48-
$page = $this->getPage($page->pageNumber() + 1, 15, $filters);
49-
50-
$page->serializeWith($loadEntity);
51-
$items = array_merge(
52-
$items,
53-
$page->getItems()
54-
);
55-
}
56-
return $items;
37+
}, $filters);
5738
}
5839

5940
/**

src/eCloud/AvailabilityZoneClient.php

+2-33
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,10 @@ public function getEntityMap()
3030
];
3131
}
3232

33-
/**
34-
* Get an array of all products and prices for the availability zone
35-
*
36-
* @param int $id Region ID
37-
* @param array $filters
38-
* @return array
39-
*/
4033
public function getProducts($id, $filters = [])
4134
{
42-
$page = $this->paginatedRequest($this->collectionPath . '/' . $id . '/prices', 1, 15, $filters);
43-
44-
if ($page->totalItems() == 0) {
45-
return [];
46-
}
47-
48-
$loadEntity = function ($data) {
35+
return $this->getChildResources($id, 'prices', function ($data) {
4936
return new Product($this->apiToFriendly($data, Product::$entityMap));
50-
};
51-
52-
$page->serializeWith($loadEntity);
53-
54-
$items = $page->getItems();
55-
if ($page->totalPages() == 1) {
56-
return $items;
57-
}
58-
59-
while ($page->pageNumber() < $page->totalPages()) {
60-
$page = $this->getPage($page->pageNumber() + 1, 15, $filters);
61-
62-
$page->serializeWith($loadEntity);
63-
$items = array_merge(
64-
$items,
65-
$page->getItems()
66-
);
67-
}
68-
return $items;
37+
}, $filters);
6938
}
7039
}

src/eCloud/RegionClient.php

+2-33
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,10 @@ public function getEntityMap()
2828
];
2929
}
3030

31-
/**
32-
* Get an array of all products and prices for the region
33-
*
34-
* @param int $id Region ID
35-
* @param array $filters
36-
* @return array
37-
*/
3831
public function getProducts($id, $filters = [])
3932
{
40-
$page = $this->paginatedRequest($this->collectionPath . '/' . $id . '/prices', 1, 15, $filters);
41-
42-
if ($page->totalItems() == 0) {
43-
return [];
44-
}
45-
46-
$loadEntity = function ($data) {
33+
return $this->getChildResources($id, 'prices', function ($data) {
4734
return new Product($this->apiToFriendly($data, Product::$entityMap));
48-
};
49-
50-
$page->serializeWith($loadEntity);
51-
52-
$items = $page->getItems();
53-
if ($page->totalPages() == 1) {
54-
return $items;
55-
}
56-
57-
while ($page->pageNumber() < $page->totalPages()) {
58-
$page = $this->getPage($page->pageNumber() + 1, 15, $filters);
59-
60-
$page->serializeWith($loadEntity);
61-
$items = array_merge(
62-
$items,
63-
$page->getItems()
64-
);
65-
}
66-
return $items;
35+
}, $filters);
6736
}
6837
}

0 commit comments

Comments
 (0)