Skip to content

Commit 03915a3

Browse files
author
Unay Santisteban
committed
Hotfix for data model and array method, new method arrayize() available, process() method has been split in process() and dispatch() methods.
1 parent 113558e commit 03915a3

File tree

2 files changed

+91
-61
lines changed

2 files changed

+91
-61
lines changed

src/FulfillmentAutomation.php

Lines changed: 58 additions & 39 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-
}
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+
}
157142

158-
$this->logger->info("Finished processing of request ID=" . $request->id . " result=" . $processingResult);
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,7 +195,14 @@ 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
/**

src/Model.php

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +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);
224+
$tmp = self::modelize(is_int($index) ? $key : $index, $item);
225225
if (isset($tmp->id)) {
226226
$array[$tmp->id] = $tmp;
227227

@@ -245,47 +245,38 @@ public static function modelize($key, $value)
245245
}
246246

247247
/**
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
248+
* Transform the given structure into array
260249
* @param mixed $value
261250
* @return array
262251
*/
263-
public function toArray($value = null)
252+
public static function arrayize($value)
264253
{
265-
if (!isset($value)) {
266-
$value = $this;
267-
}
268-
269254
$forbidden = [];
270255
if ($value instanceof \Connect\Model) {
271256
$forbidden = $value->getHidden();
272257
}
273258

274259
$array = [];
275260
foreach ($value as $key => $item) {
276-
if (!in_array($key, $forbidden)) {
261+
262+
if (stripos($key, '_hidden') !== false) {
263+
continue;
264+
}
265+
266+
if (!in_array(trim($key), $forbidden)) {
267+
277268
switch (gettype($item)) {
278269
case 'object':
279270
case 'array':
280-
$buffer = $this->toArray($item);
271+
$buffer = self::arrayize($item);
281272
if (!empty($buffer)) {
282-
$array[$key] = $buffer;
273+
$array[trim($key)] = $buffer;
283274
}
284275

285276
break;
286277
default:
287278
if (isset($item)) {
288-
$array[$key] = $item;
279+
$array[trim($key)] = $item;
289280
}
290281
}
291282
}
@@ -294,6 +285,26 @@ public function toArray($value = null)
294285
return $array;
295286
}
296287

288+
/**
289+
* Check if the required properties exists
290+
* @return array List of missing properties
291+
*/
292+
public function validate()
293+
{
294+
return array_values(array_filter($this->_required, function ($required) {
295+
return empty($this->{$required});
296+
}));
297+
}
298+
299+
/**
300+
* Transform the \Connect\Model into array
301+
* @return array
302+
*/
303+
public function toArray()
304+
{
305+
return self::arrayize($this);
306+
}
307+
297308
/**
298309
* Return the object in json format
299310
* @param boolean $pretty

0 commit comments

Comments
 (0)