1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* API Manager version 3.0 / embedded into Bicore |
5
|
|
|
* This service provides methods that help to manage API Rest communications. |
6
|
|
|
* Each API client can use it to reduce time to interact with API services |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cdf\BiCoreBundle\Service\Api; |
10
|
|
|
|
11
|
|
|
use Cdf\BiCoreBundle\Utils\Api\ApiUtils; |
12
|
|
|
use Cdf\BiCoreBundle\Utils\Api\ApiManagerUtil; |
13
|
|
|
use Cdf\BiCoreBundle\Utils\String\StringUtils; |
14
|
|
|
use Cdf\BiCoreBundle\Service\Api\Oauth2TokenService; |
15
|
|
|
use \Exception; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* It provides services to use API Rest. |
19
|
|
|
*/ |
20
|
|
|
class ApiManager |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
private string $project; |
24
|
|
|
private Oauth2TokenService $oauth2Service; |
25
|
|
|
private bool $oauth2Enabled; |
26
|
|
|
private mixed $map; |
27
|
|
|
private object $headerSelector; |
28
|
|
|
private object $config; |
29
|
|
|
|
30
|
|
|
private ApiManagerUtil $apiManUtil; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Build an ApiManager instance |
34
|
|
|
*/ |
35
|
|
|
public function __construct(String $oauth2Parameter, Oauth2TokenService $oauth2Service) |
36
|
|
|
{ |
37
|
|
|
$this->map = array(); |
38
|
|
|
$this->oauth2Service = $oauth2Service; |
39
|
|
|
$this->oauth2Enabled = false; |
40
|
|
|
if ($oauth2Parameter == 1) { |
41
|
|
|
$this->oauth2Enabled = true; |
42
|
|
|
} |
43
|
|
|
$this->apiManUtil = new ApiManagerUtil(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
//Set the project name of client api |
47
|
|
|
public function setProjectName(string $projectName): void |
48
|
|
|
{ |
49
|
|
|
$this->project = $projectName; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
//Get the project name of client api |
53
|
|
|
public function getProjectName(): string |
54
|
|
|
{ |
55
|
|
|
return $this->project; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
//Allow to set a config and an headerselector for the client api |
59
|
|
|
public function setApiClientConfigs(object $headerSelector, object $config): void |
60
|
|
|
{ |
61
|
|
|
$this->headerSelector = $headerSelector; |
62
|
|
|
$this->config = $config; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function isApiClientConfigs(): void |
66
|
|
|
{ |
67
|
|
|
if (!isset($this->headerSelector) || !isset($this->config)) { |
68
|
|
|
throw new Exception("Header selector not set or Config not set. Set them using setApiClientConfigs() method"); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
//Return true if oauth2 is enabled |
73
|
|
|
private function isOauth2(): bool |
74
|
|
|
{ |
75
|
|
|
return $this->oauth2Enabled; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
//generic setup |
80
|
|
|
public function setupCollection(string $collectionString): void |
81
|
|
|
{ |
82
|
|
|
$this->setApiController($collectionString); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Assure that exist an item mapped into ApiManager having the given collection |
88
|
|
|
* and project as key. |
89
|
|
|
*/ |
90
|
|
|
public function setApiController(String $collection, String $subcollection = ''): void |
91
|
|
|
{ |
92
|
|
|
//if not set it throws a new exception |
93
|
|
|
$this->isApiClientConfigs(); |
94
|
|
|
|
95
|
|
|
$apiUtils = new ApiUtils($collection); |
96
|
|
|
$parametri = array('str' => $collection, 'primamaiuscola' => true); |
97
|
|
|
$outcome = StringUtils::toCamelCase($parametri); |
98
|
|
|
|
99
|
|
|
$subcollectionValue = ''; |
100
|
|
|
if (!empty($subcollection)) { |
101
|
|
|
$parametri2 = array('str' => $subcollection, 'primamaiuscola' => true); |
102
|
|
|
$subcollectionValue = StringUtils::toCamelCase($parametri2); |
103
|
|
|
} |
104
|
|
|
//NOT VALID FOR WINDOWS OS |
105
|
|
|
$apiClass = $apiUtils->getApiControllerClass($this->project, $outcome); |
106
|
|
|
|
107
|
|
|
$apiController = new $apiClass(null, $this->config, $this->headerSelector); |
108
|
|
|
$subArray = [ $subcollection => $subcollectionValue ]; |
109
|
|
|
if (!empty($this->map[$collection]['sub-api'])) { |
110
|
|
|
$subArray = $this->map[$collection]['sub-api'][$subcollection] = $subcollectionValue; |
111
|
|
|
} |
112
|
|
|
$this->map($collection, [ |
113
|
|
|
'util' => $apiUtils, |
114
|
|
|
'api' => $apiController, |
115
|
|
|
'sub-api' => $subArray, |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function map(string $collection, mixed $tools): void |
120
|
|
|
{ |
121
|
|
|
if (!isset($this->map[$collection])) { |
122
|
|
|
$this->map[$collection] = $tools; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* It checks if error is due to a Broken Pipe |
128
|
|
|
* NO MORE USED SINCE 3.0.46 |
129
|
|
|
private function isBrokenPipe(Exception &$error) : bool |
130
|
|
|
{ |
131
|
|
|
$outcome = false; |
132
|
|
|
if (strpos($error->getMessage(), 'broken pipe') !== false) { |
133
|
|
|
$outcome = true; |
134
|
|
|
} |
135
|
|
|
return $outcome; |
136
|
|
|
} |
137
|
|
|
*/ |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set the token managed by wso2TokenService into api-service |
141
|
|
|
* If token is already set, then it doesn't change it |
142
|
|
|
*/ |
143
|
|
|
private function setToken(mixed $api): string |
144
|
|
|
{ |
145
|
|
|
$token = ''; |
146
|
|
|
if ($this->isOauth2()) { |
147
|
|
|
$token = $this->oauth2Service->getToken(); |
148
|
|
|
if (empty($api->getConfig()->getAccessToken())) { |
149
|
|
|
$this->refreshToken($api, $token); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
return $token; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the token managed by oauth2TokenService into api-service |
157
|
|
|
* It forces the refresh of token |
158
|
|
|
* If token is already set, it replaces it |
159
|
|
|
*/ |
160
|
|
|
private function refreshToken(mixed $api, string $token): void |
161
|
|
|
{ |
162
|
|
|
if ($this->isOauth2()) { |
163
|
|
|
//call oauth2 service for a new token |
164
|
|
|
$this->oauth2Service->refreshToken(); |
165
|
|
|
//get the new token |
166
|
|
|
$token = $this->oauth2Service->getToken(); |
167
|
|
|
//set the new token to be used by api |
168
|
|
|
$api->getConfig()->setAccessToken($token); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Return the proper method to be used |
175
|
|
|
*/ |
176
|
|
|
private function getMethod(String $collection, String $getMethod, String $subcollection = null): string |
177
|
|
|
{ |
178
|
|
|
$tools = $this->map[$collection]; |
179
|
|
|
$method = $tools['util']->$getMethod(); |
180
|
|
|
if ($subcollection != null) { |
|
|
|
|
181
|
|
|
$method .= $tools['sub-api'][$subcollection]; |
182
|
|
|
} |
183
|
|
|
return $method; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Retry one more time the already done call to API objects |
189
|
|
|
*/ |
190
|
|
|
private function retry(object $object, string $method, mixed $args): mixed |
191
|
|
|
{ |
192
|
|
|
return $object->$method(...$args); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Return the amount of element existent for the given collection. |
197
|
|
|
*/ |
198
|
|
|
public function getCount(String $collection, String $subcollection = null): mixed |
199
|
|
|
{ |
200
|
|
|
$getCountmethod = $this->getMethod($collection, 'getCount', $subcollection); |
201
|
|
|
$tools = $this->map[$collection]; |
202
|
|
|
//the array of arguments |
203
|
|
|
$arguments = array(); |
204
|
|
|
if ($subcollection != null) { |
|
|
|
|
205
|
|
|
array_push($arguments, $subcollection); |
206
|
|
|
} |
207
|
|
|
$amount = null; |
|
|
|
|
208
|
|
|
$token = $this->setToken($tools['api']); |
209
|
|
|
try { |
210
|
|
|
$amount = $tools['api']->$getCountmethod(...$arguments); |
211
|
|
|
} catch (\Exception $apiEx) { |
212
|
|
|
$this->refreshToken($tools['api'], $token); |
213
|
|
|
$amount = $this->retry($tools['api'], $getCountmethod, $arguments); |
214
|
|
|
} |
215
|
|
|
return $amount; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get items existent for the given collection, filtered as requested. |
220
|
|
|
*/ |
221
|
|
|
public function getAll(string $collection, ?int $offset, ?int $limit, mixed $sort = null, ?string $condition, string $subcollection = null): mixed |
222
|
|
|
{ |
223
|
|
|
$tools = $this->map[$collection]; |
224
|
|
|
$getAllmethod = $this->getMethod($collection, 'getAll', $subcollection); |
225
|
|
|
$results = []; |
|
|
|
|
226
|
|
|
//the array of arguments |
227
|
|
|
$arguments = array(); |
228
|
|
|
if ($subcollection != null) { |
|
|
|
|
229
|
|
|
array_push($arguments, $subcollection); |
230
|
|
|
} |
231
|
|
|
array_push($arguments, $offset); |
232
|
|
|
array_push($arguments, $limit); |
233
|
|
|
array_push($arguments, $sort); |
234
|
|
|
array_push($arguments, $condition); |
235
|
|
|
|
236
|
|
|
$token = $this->setToken($tools['api']); |
237
|
|
|
try { |
238
|
|
|
$results = $tools['api']->$getAllmethod(...$arguments); |
239
|
|
|
} catch (\Exception $apiEx) { |
240
|
|
|
$this->refreshToken($tools['api'], $token); |
241
|
|
|
$results = $this->retry($tools['api'], $getAllmethod, $arguments); |
242
|
|
|
} |
243
|
|
|
return $results; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get items existent for the given collection in "to-string" format, filtered as requested. |
248
|
|
|
*/ |
249
|
|
|
public function getAllToString(string $collection, ?int $offset, ?int $limit, mixed $sort = null, ?string $cond, String $sub = null): mixed |
250
|
|
|
{ |
251
|
|
|
$tools = $this->map[$collection]; |
252
|
|
|
$getMethod = $this->getMethod($collection, 'getAllToString', $sub); |
253
|
|
|
$results = []; |
|
|
|
|
254
|
|
|
//the array of arguments |
255
|
|
|
$arguments = array(); |
256
|
|
|
if ($sub != null) { |
|
|
|
|
257
|
|
|
array_push($arguments, $sub); |
258
|
|
|
} |
259
|
|
|
array_push($arguments, $offset); |
260
|
|
|
array_push($arguments, $limit); |
261
|
|
|
array_push($arguments, $sort); |
262
|
|
|
array_push($arguments, $cond); |
263
|
|
|
|
264
|
|
|
$token = $this->setToken($tools['api']); |
265
|
|
|
try { |
266
|
|
|
$results = $tools['api']->$getMethod(...$arguments); |
267
|
|
|
} catch (\Exception $apiEx) { |
268
|
|
|
$this->refreshToken($tools['api'], $token); |
269
|
|
|
$results = $this->retry($tools['api'], $getMethod, $arguments); |
270
|
|
|
} |
271
|
|
|
return $results; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get items existent for the given collection, filtered as requested. |
276
|
|
|
* subcollection contains the name of sub-collection if any |
277
|
|
|
* subcollectionVar contains the value of sub-collection if any |
278
|
|
|
*/ |
279
|
|
|
public function getItem(String $collection, int $id, String $subcollection = null, string $subcollectionVar = null): mixed |
280
|
|
|
{ |
281
|
|
|
$tools = $this->map[$collection]; |
282
|
|
|
$getAllmethod = $this->getMethod($collection, 'getItem', $subcollection); |
283
|
|
|
$results = []; |
|
|
|
|
284
|
|
|
$arguments = array(); |
285
|
|
|
if ($subcollectionVar != null) { |
|
|
|
|
286
|
|
|
array_push($arguments, $subcollectionVar); |
287
|
|
|
} |
288
|
|
|
array_push($arguments, $id); |
289
|
|
|
$token = $this->setToken($tools['api']); |
290
|
|
|
try { |
291
|
|
|
$results = $tools['api']->$getAllmethod(...$arguments); |
292
|
|
|
} catch (\Exception $apiEx) { |
293
|
|
|
$this->refreshToken($tools['api'], $token); |
294
|
|
|
$results = $this->retry($tools['api'], $getAllmethod, $arguments); |
295
|
|
|
} |
296
|
|
|
return $results; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Delete an existent item |
301
|
|
|
*/ |
302
|
|
|
public function deleteItem(String $collection, int $id, String $subcollection = null): mixed |
303
|
|
|
{ |
304
|
|
|
$tools = $this->map[$collection]; |
305
|
|
|
$deleteMethod = $this->getMethod($collection, 'getDelete', $subcollection); |
306
|
|
|
$arguments = array(); |
307
|
|
|
if ($subcollection != null) { |
|
|
|
|
308
|
|
|
array_push($arguments, $subcollection); |
309
|
|
|
} |
310
|
|
|
array_push($arguments, $id); |
311
|
|
|
$token = $this->setToken($tools['api']); |
312
|
|
|
try { |
313
|
|
|
$response = $tools['api']->$deleteMethod(...$arguments); |
314
|
|
|
} catch (\Exception $apiEx) { |
315
|
|
|
$this->refreshToken($tools['api'], $token); |
316
|
|
|
$response = $this->retry($tools['api'], $deleteMethod, $arguments); |
317
|
|
|
} |
318
|
|
|
return $response; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Create the requested object, using the given body item. |
324
|
|
|
*/ |
325
|
|
|
public function postCreate(String $collection, mixed $body, String $subcollection = null): mixed |
326
|
|
|
{ |
327
|
|
|
$tools = $this->map[$collection]; |
328
|
|
|
$createMethod = $this->getMethod($collection, 'getCreate', $subcollection); |
329
|
|
|
$results = []; |
|
|
|
|
330
|
|
|
$arguments = array(); |
331
|
|
|
array_push($arguments, $body); |
332
|
|
|
if ($subcollection != null) { |
|
|
|
|
333
|
|
|
array_push($arguments, $subcollection); |
334
|
|
|
} |
335
|
|
|
$token = $this->setToken($tools['api']); |
336
|
|
|
try { |
337
|
|
|
$results = $tools['api']->$createMethod(...$arguments); |
338
|
|
|
} catch (\Exception $apiEx) { |
339
|
|
|
$this->refreshToken($tools['api'], $token); |
340
|
|
|
$results = $this->retry($tools['api'], $createMethod, $arguments); |
341
|
|
|
} |
342
|
|
|
return $results; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Create the requested object, using the given body item. |
348
|
|
|
*/ |
349
|
|
|
public function postUpdate(String $collection, object $body, int $id, String $subcollection = null): mixed |
350
|
|
|
{ |
351
|
|
|
$tools = $this->map[$collection]; |
352
|
|
|
$createMethod = $this->getMethod($collection, 'getUpdateItem', $subcollection); |
353
|
|
|
$results = []; |
|
|
|
|
354
|
|
|
$arguments = array(); |
355
|
|
|
array_push($arguments, $body); |
356
|
|
|
array_push($arguments, $id); |
357
|
|
|
if ($subcollection != null) { |
|
|
|
|
358
|
|
|
array_push($arguments, $subcollection); |
359
|
|
|
} |
360
|
|
|
$token = $this->setToken($tools['api']); |
361
|
|
|
try { |
362
|
|
|
$results = $tools['api']->$createMethod(...$arguments); |
363
|
|
|
} catch (\Exception $apiEx) { |
364
|
|
|
$this->refreshToken($tools['api'], $token); |
365
|
|
|
$results = $this->retry($tools['api'], $createMethod, $arguments); |
366
|
|
|
} |
367
|
|
|
return $results; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* It prepares entity values so that they can be used with types compliant with BiCoreBundle. |
372
|
|
|
* For example it transforms a date that arrive in string format into a DateTime. |
373
|
|
|
* @deprecated: evaluate to migrate on ApiManagerUtil |
374
|
|
|
*/ |
375
|
|
|
public function setupApiValues(mixed $entityout): mixed |
376
|
|
|
{ |
377
|
|
|
return $this->apiManUtil->setupApiValues($entityout); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Map first object transformed into the second where possible, |
382
|
|
|
* attempting to map each field of first into field of the second. |
383
|
|
|
* @deprecated: evaluate to migrate on ApiManagerUtil |
384
|
|
|
*/ |
385
|
|
|
public function mapData(object $modelEntity, object $controllerItem): mixed |
386
|
|
|
{ |
387
|
|
|
return $this->apiManUtil->mapData($modelEntity, $controllerItem); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths