Skip to content

Commit bb1c90f

Browse files
authored
Merge pull request #15 from othercodes/master
Hotfix for data model and toArray() method
2 parents 113558e + 79f2eb7 commit bb1c90f

File tree

5 files changed

+207
-91
lines changed

5 files changed

+207
-91
lines changed

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<testsuite name="Models">
5454
<file>tests/Unit/ModelTest.php</file>
5555
<file>tests/Unit/ParamTest.php</file>
56+
<file>tests/Unit/AssetTest.php</file>
5657
</testsuite>
5758
</testsuites>
5859
</phpunit>

src/Asset.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,32 @@ class Asset extends Model
4141
* @var Tiers
4242
*/
4343
public $tiers;
44+
45+
/**
46+
* Return a Param by ID
47+
* @param $id
48+
* @return Param
49+
*/
50+
public function getParameterByID($id)
51+
{
52+
$param = current(array_filter($this->params, function (Param $param) use ($id) {
53+
return ($param->id === $id);
54+
}));
55+
56+
return ($param) ? $param : null;
57+
}
58+
59+
/**
60+
* Return a Param by ID
61+
* @param $id
62+
* @return Param
63+
*/
64+
public function getItemByID($id)
65+
{
66+
$item = current(array_filter($this->items, function (Item $item) use ($id) {
67+
return ($item->id === $id);
68+
}));
69+
70+
return ($item) ? $item : null;
71+
}
4472
}

src/FulfillmentAutomation.php

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -112,52 +112,64 @@ private function sendRequest($verb, $path, $body = null)
112112
*/
113113
public function process()
114114
{
115-
$requests = $this->listRequests(['status' => 'pending']);
115+
foreach ($this->listRequests(['status' => 'pending']) as $request) {
116+
$this->dispatch($request);
117+
}
118+
}
116119

117-
foreach ($requests as $request) {
120+
/**
121+
* @param Request $request
122+
* @return string
123+
* @throws \GuzzleHttp\Exception\GuzzleException
124+
*/
125+
protected function dispatch($request)
126+
{
127+
try {
118128

119129
if ($this->config->products && !in_array($request->asset->product->id, $this->config->products)) {
120-
continue;
130+
return 'Invalid product';
121131
}
122132

123-
if ($request->status == 'pending') { // actually default filter is pending
124-
$processingResult = 'unknown';
125-
try {
126-
$this->logger->info("Starting processing of request ID=" . $request->id);
127-
128-
/** @noinspection PhpVoidFunctionResultUsedInspection */
129-
$msg = $this->processRequest($request);
130-
if (!$msg || is_string($msg)) {
131-
$msg = new ActivationTileResponse($msg);
132-
}
133-
134-
if ($msg instanceof ActivationTemplateResponse) {
135-
$this->sendRequest('POST', '/requests/' . $request->id . '/approve',
136-
'{"template_id": "' . $msg->templateid . '"}');
137-
$processingResult = 'succeed (Activated using template ' . $msg->templateid . ')';
138-
} else {
139-
$this->sendRequest('POST', '/requests/' . $request->id . '/approve',
140-
'{"activation_tile": "' . $msg->activationTile . '"}');
141-
$processingResult = 'succeed (' . $msg->activationTile . ')';
142-
}
143-
144-
} catch (Inquire $e) {
145-
// update parameters and move to inquire
146-
$this->updateParameters($request, $e->params);
147-
$this->sendRequest('POST', '/requests/' . $request->id . '/inquire', '{}');
148-
$processingResult = 'inquire';
149-
} catch (Fail $e) {
150-
// fail request
151-
$this->sendRequest('POST', '/requests/' . $request->id . '/fail',
152-
'{"reason": "' . $e->getMessage() . '"}');
153-
$processingResult = 'fail';
154-
} catch (Skip $e) {
155-
$processingResult = 'skip';
156-
}
157-
158-
$this->logger->info("Finished processing of request ID=" . $request->id . " result=" . $processingResult);
133+
$processingResult = 'unknown';
134+
135+
$this->logger->info("Starting processing of request ID=" . $request->id);
136+
137+
/** @noinspection PhpVoidFunctionResultUsedInspection */
138+
$msg = $this->processRequest($request);
139+
if (!$msg || is_string($msg)) {
140+
$msg = new ActivationTileResponse($msg);
141+
}
142+
143+
if ($msg instanceof ActivationTemplateResponse) {
144+
$this->sendRequest('POST', '/requests/' . $request->id . '/approve',
145+
'{"template_id": "' . $msg->templateid . '"}');
146+
$processingResult = 'succeed (Activated using template ' . $msg->templateid . ')';
147+
} else {
148+
$this->sendRequest('POST', '/requests/' . $request->id . '/approve',
149+
'{"activation_tile": "' . $msg->activationTile . '"}');
150+
$processingResult = 'succeed (' . $msg->activationTile . ')';
159151
}
152+
153+
} catch (Inquire $e) {
154+
// update parameters and move to inquire
155+
$this->updateParameters($request, $e->params);
156+
$this->sendRequest('POST', '/requests/' . $request->id . '/inquire', '{}');
157+
$processingResult = 'inquire';
158+
159+
} catch (Fail $e) {
160+
// fail request
161+
$this->sendRequest('POST', '/requests/' . $request->id . '/fail',
162+
'{"reason": "' . $e->getMessage() . '"}');
163+
$processingResult = 'fail';
164+
165+
} catch (Skip $e) {
166+
$processingResult = 'skip';
167+
160168
}
169+
170+
$this->logger->info("Finished processing of request ID=" . $request->id . " result=" . $processingResult);
171+
172+
return $processingResult;
161173
}
162174

163175
/**
@@ -183,43 +195,31 @@ public function listRequests(array $filters = null)
183195
}
184196

185197
$body = $this->sendRequest('GET', '/requests' . $query);
186-
return Model::modelize('requests', json_decode($body));
198+
199+
/** @var Request[] $models */
200+
$models = Model::modelize('requests', json_decode($body));
201+
foreach ($models as $index => $model) {
202+
$models[$index]->requestProcessor = $this;
203+
}
204+
205+
return $models;
187206
}
188207

189208
/**
190209
* Update request parameters
191210
* @param Request $request - request being updated
192-
* @param Param[] $parray - array of parameters
211+
* @param Param[] $params - array of parameters
193212
* Example:
194213
* array(
195214
* $request->asset->params['param_a']->error('Unknown activation ID was provided'),
196215
* $request->asset->params['param_b']->value('true'),
197-
* new \Connect\Param('param_c', 'newValue')
216+
* new \Connect\Param(['id' => 'param_c', 'newValue'])
198217
* )
199218
* @throws \GuzzleHttp\Exception\GuzzleException
200219
*/
201-
public function updateParameters(Request $request, array $parray)
220+
public function updateParameters(Request $request, array $params)
202221
{
203-
$plist = array();
204-
foreach ($parray as $p) {
205-
$parr = (array)$p;
206-
207-
unset($parr['value_choices']);
208-
209-
foreach ($parr as $k => $v) {
210-
if (!$v) {
211-
unset($parr[$k]);
212-
}
213-
214-
if ($k == 'value' && !$v) {
215-
$parr[$k] = '';
216-
}
217-
}
218-
219-
$plist[] = $parr;
220-
}
221-
222-
$body = new \Connect\Request(['asset' => ['params' => $plist]]);
222+
$body = new \Connect\Request(['asset' => ['params' => $params]]);
223223
$this->sendRequest('PUT', '/requests/' . $request->id, $body);
224224
}
225225

src/Model.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,7 @@ public static function modelize($key, $value)
221221
* of original array.
222222
*/
223223
foreach ($value as $index => $item) {
224-
$tmp = self::modelize($key, $item);
225-
if (isset($tmp->id)) {
226-
$array[$tmp->id] = $tmp;
227-
228-
} else {
229-
$array[$index] = $tmp;
230-
}
224+
$array[$index] = self::modelize(is_int($index) ? $key : $index, $item);
231225
}
232226

233227
return $array;
@@ -245,47 +239,32 @@ public static function modelize($key, $value)
245239
}
246240

247241
/**
248-
* Check if the required properties exists
249-
* @return array List of missing properties
250-
*/
251-
public function validate()
252-
{
253-
return array_values(array_filter($this->_required, function ($required) {
254-
return empty($this->{$required});
255-
}));
256-
}
257-
258-
/**
259-
* Return all the filled properties/arrays and Model objects
242+
* Transform the given structure into array
260243
* @param mixed $value
261244
* @return array
262245
*/
263-
public function toArray($value = null)
246+
public static function arrayize($value)
264247
{
265-
if (!isset($value)) {
266-
$value = $this;
267-
}
268-
269248
$forbidden = [];
270249
if ($value instanceof \Connect\Model) {
271250
$forbidden = $value->getHidden();
272251
}
273252

274253
$array = [];
275254
foreach ($value as $key => $item) {
276-
if (!in_array($key, $forbidden)) {
255+
if (!in_array(trim($key), $forbidden)) {
277256
switch (gettype($item)) {
278257
case 'object':
279258
case 'array':
280-
$buffer = $this->toArray($item);
259+
$buffer = self::arrayize($item);
281260
if (!empty($buffer)) {
282-
$array[$key] = $buffer;
261+
$array[trim($key)] = $buffer;
283262
}
284263

285264
break;
286265
default:
287266
if (isset($item)) {
288-
$array[$key] = $item;
267+
$array[trim($key)] = $item;
289268
}
290269
}
291270
}
@@ -294,6 +273,26 @@ public function toArray($value = null)
294273
return $array;
295274
}
296275

276+
/**
277+
* Check if the required properties exists
278+
* @return array List of missing properties
279+
*/
280+
public function validate()
281+
{
282+
return array_values(array_filter($this->_required, function ($required) {
283+
return empty($this->{$required});
284+
}));
285+
}
286+
287+
/**
288+
* Transform the \Connect\Model into array
289+
* @return array
290+
*/
291+
public function toArray()
292+
{
293+
return self::arrayize($this);
294+
}
295+
297296
/**
298297
* Return the object in json format
299298
* @param boolean $pretty

tests/Unit/AssetTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Ingram Micro Cloud Blue Connect SDK.
5+
*
6+
* @copyright (c) 2018. Ingram Micro. All Rights Reserved.
7+
*/
8+
9+
namespace Test\Unit;
10+
11+
12+
use Connect\Asset;
13+
use Connect\Item;
14+
use Connect\Param;
15+
16+
class AssetTest extends \Test\TestCase
17+
{
18+
/**
19+
* @return Asset
20+
*/
21+
public function testInstantiation()
22+
{
23+
$asset = new Asset([
24+
'id' => 111,
25+
'params' => [
26+
new Param([
27+
'id' => 111,
28+
'value' => 'one'
29+
]),
30+
new Param([
31+
'id' => 222,
32+
'value' => 'one'
33+
]),
34+
new Param([
35+
'id' => 333,
36+
'value' => 'one'
37+
])
38+
],
39+
'items' => [
40+
new Item([
41+
'id' => 111,
42+
'quantity' => 1
43+
]),
44+
new Item([
45+
'id' => 222,
46+
'quantity' => 2
47+
]),
48+
new Item([
49+
'id' => 333,
50+
'quantity' => 3
51+
])
52+
],
53+
]);
54+
55+
$this->assertInstanceOf('\Connect\Asset', $asset);
56+
57+
$this->assertEquals(111, $asset->id);
58+
$this->assertCount(3, $asset->params);
59+
$this->assertCount(3, $asset->items);
60+
61+
return $asset;
62+
}
63+
64+
/**
65+
* @param Asset $asset
66+
*
67+
* @depends testInstantiation
68+
*/
69+
public function testGetParameterById(Asset $asset)
70+
{
71+
$this->assertInstanceOf('\Connect\Param', $asset->getParameterByID(222));
72+
$this->assertEquals(222, $asset->getParameterByID(222)->id);
73+
$this->assertNull($asset->getParameterByID(999));
74+
}
75+
76+
/**
77+
* @param Asset $asset
78+
*
79+
* @depends testInstantiation
80+
*/
81+
public function testGetItemById(Asset $asset)
82+
{
83+
$this->assertInstanceOf('\Connect\Item', $asset->getItemByID(333));
84+
$this->assertEquals(333, $asset->getItemByID(333)->id);
85+
$this->assertNull($asset->getItemByID(999));
86+
}
87+
88+
}

0 commit comments

Comments
 (0)