|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jayS-de <[email protected]> |
|
4
|
|
|
* @created: 20.01.15, 17:54 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Commercetools\Core; |
|
8
|
|
|
|
|
9
|
|
|
use Commercetools\Core\Error\Message; |
|
10
|
|
|
use Commercetools\Core\Error\InvalidArgumentException; |
|
11
|
|
|
use Commercetools\Core\Model\Common\ContextAwareInterface; |
|
12
|
|
|
use Commercetools\Core\Model\Common\ContextTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Client configuration object |
|
16
|
|
|
* |
|
17
|
|
|
* @description |
|
18
|
|
|
* |
|
19
|
|
|
* Often configuration like credentials is stored in YAML or INI files. To setup the configuration object |
|
20
|
|
|
* this can be done by the fromArray method. |
|
21
|
|
|
* |
|
22
|
|
|
* Configuration file: |
|
23
|
|
|
* |
|
24
|
|
|
* ``` |
|
25
|
|
|
* [commercetools] |
|
26
|
|
|
* client_id = '<client-id>' |
|
27
|
|
|
* client_secret = '<client-secret>' |
|
28
|
|
|
* project = '<project>' |
|
29
|
|
|
* ``` |
|
30
|
|
|
* |
|
31
|
|
|
* Config instantiation: |
|
32
|
|
|
* |
|
33
|
|
|
* ```php |
|
34
|
|
|
* $iniConfig = parse_ini_file('<config-file>.ini', true); |
|
35
|
|
|
* $config = Config::fromArray($iniConfig['commercetools']); |
|
36
|
|
|
* ``` |
|
37
|
|
|
* |
|
38
|
|
|
* ### Exceptions ### |
|
39
|
|
|
* |
|
40
|
|
|
* The client by default suppresses exceptions when a response had been returned by the API and the result |
|
41
|
|
|
* can be handled afterwards by checking the isError method of the response. For interacting with Exceptions |
|
42
|
|
|
* they can be enabled with the throwExceptions flag. |
|
43
|
|
|
* |
|
44
|
|
|
* ```php |
|
45
|
|
|
* $config->setThrowExceptions(true); |
|
46
|
|
|
* $client = new Client($config); |
|
47
|
|
|
* try { |
|
48
|
|
|
* $response = $client->execute($request); |
|
49
|
|
|
* } catch (\Commercetools\Core\Error\ApiException $e) { |
|
50
|
|
|
* // handle Exception |
|
51
|
|
|
* } |
|
52
|
|
|
* ``` |
|
53
|
|
|
* @package Commercetools\Core |
|
54
|
|
|
*/ |
|
55
|
|
|
class Config implements ContextAwareInterface |
|
56
|
|
|
{ |
|
57
|
|
|
use ContextTrait; |
|
58
|
|
|
|
|
59
|
|
|
const OAUTH_URL = 'oauth_url'; |
|
60
|
|
|
const CLIENT_ID = 'client_id'; |
|
61
|
|
|
const CLIENT_SECRET = 'client_secret'; |
|
62
|
|
|
const SCOPE = 'scope'; |
|
63
|
|
|
const PROJECT = 'project'; |
|
64
|
|
|
const API_URL = 'api_url'; |
|
65
|
|
|
const USER_NAME = 'username'; |
|
66
|
|
|
const PASSWORD = 'password'; |
|
67
|
|
|
const REFRESH_TOKEN = 'refresh_token'; |
|
68
|
|
|
const GRANT_TYPE = 'grant_type'; |
|
69
|
|
|
|
|
70
|
|
|
const GRANT_TYPE_CLIENT = 'client_credentials'; |
|
71
|
|
|
const GRANT_TYPE_PASSWORD = 'password'; |
|
72
|
|
|
const GRANT_TYPE_REFRESH = 'refresh_token'; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $clientSecret; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $clientId; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var string |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $project; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var array |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $scope = ['manage_project']; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected $oauthUrl = 'https://auth.sphere.io'; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @var string |
|
101
|
|
|
*/ |
|
102
|
|
|
protected $apiUrl = 'https://api.sphere.io'; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @var int |
|
106
|
|
|
*/ |
|
107
|
|
|
protected $batchPoolSize = 25; |
|
108
|
|
|
|
|
109
|
|
|
protected $adapter; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @var bool |
|
113
|
|
|
*/ |
|
114
|
|
|
protected $throwExceptions = false; |
|
115
|
|
|
|
|
116
|
|
|
protected $acceptEncoding = 'gzip'; |
|
117
|
|
|
|
|
118
|
|
|
protected $grantType = 'client_credentials'; |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @var string |
|
122
|
|
|
*/ |
|
123
|
|
|
protected $username; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @var string |
|
127
|
|
|
*/ |
|
128
|
|
|
protected $password; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @var string |
|
132
|
|
|
*/ |
|
133
|
|
|
protected $refreshToken; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param array $configValues |
|
137
|
|
|
* @return static |
|
138
|
|
|
*/ |
|
139
|
57 |
|
public static function fromArray(array $configValues) |
|
140
|
|
|
{ |
|
141
|
57 |
|
$config = static::of(); |
|
142
|
57 |
|
array_walk( |
|
143
|
|
|
$configValues, |
|
144
|
57 |
|
function ($value, $key) use ($config) { |
|
145
|
55 |
|
$functionName = 'set' . $config->camelize($key); |
|
146
|
55 |
|
if (!is_callable([$config, $functionName])) { |
|
147
|
1 |
|
throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key)); |
|
148
|
|
|
} |
|
149
|
54 |
|
$config->$functionName($value); |
|
150
|
57 |
|
} |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
56 |
|
return $config; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
55 |
|
protected function camelize($scored) |
|
157
|
|
|
{ |
|
158
|
55 |
|
return lcfirst( |
|
159
|
|
|
implode( |
|
160
|
55 |
|
'', |
|
161
|
|
|
array_map( |
|
162
|
55 |
|
'ucfirst', |
|
163
|
|
|
array_map( |
|
164
|
55 |
|
'strtolower', |
|
165
|
55 |
|
explode('_', $scored) |
|
166
|
|
|
) |
|
167
|
|
|
) |
|
168
|
|
|
) |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
44 |
|
public function getClientSecret() |
|
176
|
|
|
{ |
|
177
|
44 |
|
return $this->clientSecret; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param string $clientSecret |
|
182
|
|
|
* @return $this |
|
183
|
|
|
*/ |
|
184
|
45 |
|
public function setClientSecret($clientSecret) |
|
185
|
|
|
{ |
|
186
|
45 |
|
$this->clientSecret = $clientSecret; |
|
187
|
|
|
|
|
188
|
45 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
48 |
|
public function getClientId() |
|
195
|
|
|
{ |
|
196
|
48 |
|
return $this->clientId; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param string $clientId |
|
201
|
|
|
* @return $this |
|
202
|
|
|
*/ |
|
203
|
45 |
|
public function setClientId($clientId) |
|
204
|
|
|
{ |
|
205
|
45 |
|
$this->clientId = $clientId; |
|
206
|
|
|
|
|
207
|
45 |
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return string |
|
212
|
|
|
*/ |
|
213
|
323 |
|
public function getProject() |
|
214
|
|
|
{ |
|
215
|
323 |
|
return $this->project; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param string $project |
|
220
|
|
|
* @return $this |
|
221
|
|
|
*/ |
|
222
|
53 |
|
public function setProject($project) |
|
223
|
|
|
{ |
|
224
|
53 |
|
$this->project = $project; |
|
225
|
|
|
|
|
226
|
53 |
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return string |
|
231
|
|
|
*/ |
|
232
|
291 |
|
public function getScope() |
|
233
|
|
|
{ |
|
234
|
291 |
|
$scope = $this->scope; |
|
235
|
291 |
|
$project = $this->getProject(); |
|
236
|
|
|
|
|
237
|
291 |
|
$permissions = []; |
|
238
|
291 |
|
foreach ($scope as $key => $value) { |
|
239
|
291 |
|
if (is_numeric($key)) { // scope defined as string e.g. scope:project_key |
|
240
|
290 |
|
if (strpos($value, ':') === false) { // scope without project key |
|
241
|
289 |
|
$value = $value . ':' . $project; |
|
242
|
|
|
} |
|
243
|
290 |
|
$permissions[] = $value; |
|
244
|
|
|
} else { // scope defined as array |
|
245
|
291 |
|
$permissions[] = $key . ':' . $value; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
291 |
|
$scope = implode(' ', $permissions); |
|
249
|
|
|
|
|
250
|
291 |
|
return $scope; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param string $scope |
|
255
|
|
|
* @return $this |
|
256
|
|
|
*/ |
|
257
|
16 |
|
public function setScope($scope) |
|
258
|
|
|
{ |
|
259
|
16 |
|
if (!is_array($scope)) { |
|
260
|
11 |
|
$scope = explode(' ', $scope); |
|
261
|
|
|
} |
|
262
|
16 |
|
$this->scope = $scope; |
|
263
|
|
|
|
|
264
|
16 |
|
return $this; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @return string |
|
269
|
|
|
*/ |
|
270
|
16 |
|
public function getOauthUrl() |
|
271
|
|
|
{ |
|
272
|
16 |
|
switch ($this->getGrantType()) { |
|
273
|
16 |
|
case static::GRANT_TYPE_PASSWORD: |
|
274
|
13 |
|
case static::GRANT_TYPE_REFRESH: |
|
275
|
4 |
|
return $this->oauthUrl . '/oauth/' . $this->getProject() . '/customers/token'; |
|
276
|
|
|
default: |
|
277
|
12 |
|
return $this->oauthUrl . '/oauth/token'; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param string $oauthUrl |
|
283
|
|
|
* @return $this |
|
284
|
|
|
*/ |
|
285
|
45 |
|
public function setOauthUrl($oauthUrl) |
|
286
|
|
|
{ |
|
287
|
45 |
|
$this->oauthUrl = $oauthUrl; |
|
288
|
|
|
|
|
289
|
45 |
|
return $this; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @return string |
|
294
|
|
|
*/ |
|
295
|
34 |
|
public function getApiUrl() |
|
296
|
|
|
{ |
|
297
|
34 |
|
return $this->apiUrl; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @param string $apiUrl |
|
302
|
|
|
* @return $this |
|
303
|
|
|
*/ |
|
304
|
45 |
|
public function setApiUrl($apiUrl) |
|
305
|
|
|
{ |
|
306
|
45 |
|
$this->apiUrl = $apiUrl; |
|
307
|
|
|
|
|
308
|
45 |
|
return $this; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @return bool |
|
313
|
|
|
*/ |
|
314
|
44 |
|
public function check() |
|
315
|
|
|
{ |
|
316
|
44 |
|
if (is_null($this->getClientId())) { |
|
317
|
4 |
|
throw new InvalidArgumentException(Message::NO_CLIENT_ID); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
40 |
|
if (is_null($this->getClientSecret())) { |
|
321
|
|
|
throw new InvalidArgumentException(Message::NO_CLIENT_SECRET); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
40 |
|
if (is_null($this->getProject())) { |
|
325
|
|
|
throw new InvalidArgumentException(Message::NO_PROJECT_ID); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
40 |
|
return true; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* @return int |
|
333
|
|
|
*/ |
|
334
|
1 |
|
public function getBatchPoolSize() |
|
335
|
|
|
{ |
|
336
|
1 |
|
return $this->batchPoolSize; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* @param int $batchPoolSize |
|
341
|
|
|
* @return $this |
|
342
|
|
|
*/ |
|
343
|
1 |
|
public function setBatchPoolSize($batchPoolSize) |
|
344
|
|
|
{ |
|
345
|
1 |
|
$this->batchPoolSize = $batchPoolSize; |
|
346
|
|
|
|
|
347
|
1 |
|
return $this; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* @return string |
|
352
|
|
|
*/ |
|
353
|
37 |
|
public function getAdapter() |
|
354
|
|
|
{ |
|
355
|
37 |
|
return $this->adapter; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* @param string $adapter |
|
360
|
|
|
* @return $this |
|
361
|
|
|
*/ |
|
362
|
|
|
public function setAdapter($adapter) |
|
363
|
|
|
{ |
|
364
|
|
|
$this->adapter = $adapter; |
|
365
|
|
|
|
|
366
|
|
|
return $this; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* @return bool |
|
371
|
|
|
*/ |
|
372
|
49 |
|
public function getThrowExceptions() |
|
373
|
|
|
{ |
|
374
|
49 |
|
return $this->throwExceptions; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* @param bool $throwExceptions |
|
379
|
|
|
* @return $this |
|
380
|
|
|
*/ |
|
381
|
9 |
|
public function setThrowExceptions($throwExceptions) |
|
382
|
|
|
{ |
|
383
|
9 |
|
$this->throwExceptions = $throwExceptions; |
|
384
|
|
|
|
|
385
|
9 |
|
return $this; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
/** |
|
389
|
|
|
* @return string |
|
390
|
|
|
*/ |
|
391
|
37 |
|
public function getAcceptEncoding() |
|
392
|
|
|
{ |
|
393
|
37 |
|
return $this->acceptEncoding; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* @param string $acceptEncoding |
|
398
|
|
|
* @return $this |
|
399
|
|
|
*/ |
|
400
|
|
|
public function setAcceptEncoding($acceptEncoding) |
|
401
|
|
|
{ |
|
402
|
|
|
$this->acceptEncoding = $acceptEncoding; |
|
403
|
|
|
|
|
404
|
|
|
return $this; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* @return static |
|
409
|
|
|
*/ |
|
410
|
57 |
|
public static function of() |
|
411
|
|
|
{ |
|
412
|
57 |
|
return new static(); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* @return string |
|
417
|
|
|
*/ |
|
418
|
286 |
|
public function getGrantType() |
|
419
|
|
|
{ |
|
420
|
286 |
|
return $this->grantType; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
/** |
|
424
|
|
|
* @param string $grantType |
|
425
|
|
|
* @return $this |
|
426
|
|
|
*/ |
|
427
|
4 |
|
public function setGrantType($grantType) |
|
428
|
|
|
{ |
|
429
|
4 |
|
$this->grantType = $grantType; |
|
430
|
|
|
|
|
431
|
4 |
|
return $this; |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* @return string |
|
436
|
|
|
*/ |
|
437
|
4 |
|
public function getUsername() |
|
438
|
|
|
{ |
|
439
|
4 |
|
return $this->username; |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
|
|
/** |
|
443
|
|
|
* @param string $username |
|
444
|
|
|
* @return $this |
|
445
|
|
|
*/ |
|
446
|
4 |
|
public function setUsername($username) |
|
447
|
|
|
{ |
|
448
|
4 |
|
$this->username = $username; |
|
449
|
|
|
|
|
450
|
4 |
|
return $this; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* @return string |
|
455
|
|
|
*/ |
|
456
|
4 |
|
public function getPassword() |
|
457
|
|
|
{ |
|
458
|
4 |
|
return $this->password; |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
/** |
|
462
|
|
|
* @param string $password |
|
463
|
|
|
* @return $this |
|
464
|
|
|
*/ |
|
465
|
4 |
|
public function setPassword($password) |
|
466
|
|
|
{ |
|
467
|
4 |
|
$this->password = $password; |
|
468
|
|
|
|
|
469
|
4 |
|
return $this; |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
/** |
|
473
|
|
|
* @return string |
|
474
|
|
|
*/ |
|
475
|
4 |
|
public function getRefreshToken() |
|
476
|
|
|
{ |
|
477
|
4 |
|
return $this->refreshToken; |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
/** |
|
481
|
|
|
* @param string $refreshToken |
|
482
|
|
|
* @return $this |
|
483
|
|
|
*/ |
|
484
|
4 |
|
public function setRefreshToken($refreshToken) |
|
485
|
|
|
{ |
|
486
|
4 |
|
$this->refreshToken = $refreshToken; |
|
487
|
|
|
|
|
488
|
4 |
|
return $this; |
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
|