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 ANONYMOUS_ID = 'anonymous_id'; |
69
|
|
|
const GRANT_TYPE = 'grant_type'; |
70
|
|
|
|
71
|
|
|
const GRANT_TYPE_CLIENT = 'client_credentials'; |
72
|
|
|
const GRANT_TYPE_PASSWORD = 'password'; |
73
|
|
|
const GRANT_TYPE_REFRESH = 'refresh_token'; |
74
|
|
|
const GRANT_TYPE_ANONYMOUS = 'anonymous_token'; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
protected $clientSecret; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $clientId; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
protected $project; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
protected $scope = ['manage_project']; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var string |
98
|
|
|
*/ |
99
|
|
|
protected $oauthUrl = 'https://auth.sphere.io'; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var string |
103
|
|
|
*/ |
104
|
|
|
protected $apiUrl = 'https://api.sphere.io'; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var int |
108
|
|
|
*/ |
109
|
|
|
protected $batchPoolSize = 25; |
110
|
|
|
|
111
|
|
|
protected $adapter; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @var bool |
115
|
|
|
*/ |
116
|
|
|
protected $throwExceptions = false; |
117
|
|
|
|
118
|
|
|
protected $acceptEncoding = 'gzip'; |
119
|
|
|
|
120
|
|
|
protected $grantType = 'client_credentials'; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @var string |
124
|
|
|
*/ |
125
|
|
|
protected $username; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @var string |
129
|
|
|
*/ |
130
|
|
|
protected $password; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @var string |
134
|
|
|
*/ |
135
|
|
|
protected $refreshToken; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @var string |
139
|
|
|
*/ |
140
|
|
|
protected $anonymousId; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @var string |
144
|
|
|
*/ |
145
|
|
|
protected $cacheDir; |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array $configValues |
149
|
|
|
* @return static |
150
|
|
|
*/ |
151
|
89 |
|
public static function fromArray(array $configValues) |
152
|
|
|
{ |
153
|
89 |
|
$config = static::of(); |
154
|
89 |
|
array_walk( |
155
|
|
|
$configValues, |
156
|
89 |
|
function ($value, $key) use ($config) { |
157
|
87 |
|
$functionName = 'set' . $config->camelize($key); |
158
|
87 |
|
if (!is_callable([$config, $functionName])) { |
159
|
1 |
|
throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key)); |
160
|
|
|
} |
161
|
86 |
|
$config->$functionName($value); |
162
|
89 |
|
} |
163
|
|
|
); |
164
|
|
|
|
165
|
88 |
|
return $config; |
166
|
|
|
} |
167
|
|
|
|
168
|
87 |
|
protected function camelize($scored) |
169
|
|
|
{ |
170
|
87 |
|
return lcfirst( |
171
|
|
|
implode( |
172
|
87 |
|
'', |
173
|
|
|
array_map( |
174
|
87 |
|
'ucfirst', |
175
|
|
|
array_map( |
176
|
87 |
|
'strtolower', |
177
|
87 |
|
explode('_', $scored) |
178
|
|
|
) |
179
|
|
|
) |
180
|
|
|
) |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
77 |
|
public function getClientSecret() |
188
|
|
|
{ |
189
|
77 |
|
return $this->clientSecret; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $clientSecret |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
77 |
|
public function setClientSecret($clientSecret) |
197
|
|
|
{ |
198
|
77 |
|
$this->clientSecret = $clientSecret; |
199
|
|
|
|
200
|
77 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
81 |
|
public function getClientId() |
207
|
|
|
{ |
208
|
81 |
|
return $this->clientId; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param string $clientId |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
77 |
|
public function setClientId($clientId) |
216
|
|
|
{ |
217
|
77 |
|
$this->clientId = $clientId; |
218
|
|
|
|
219
|
77 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
396 |
|
public function getProject() |
226
|
|
|
{ |
227
|
396 |
|
return $this->project; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $project |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
85 |
|
public function setProject($project) |
235
|
|
|
{ |
236
|
85 |
|
$this->project = $project; |
237
|
|
|
|
238
|
85 |
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
361 |
|
public function getScope() |
245
|
|
|
{ |
246
|
361 |
|
$scope = $this->scope; |
247
|
361 |
|
$project = $this->getProject(); |
248
|
|
|
|
249
|
361 |
|
$permissions = []; |
250
|
361 |
|
foreach ($scope as $key => $value) { |
251
|
361 |
|
if (is_numeric($key)) { // scope defined as string e.g. scope:project_key |
252
|
360 |
|
if (strpos($value, ':') === false) { // scope without project key |
253
|
359 |
|
$value = $value . ':' . $project; |
254
|
|
|
} |
255
|
360 |
|
$permissions[] = $value; |
256
|
|
|
} else { // scope defined as array |
257
|
361 |
|
$permissions[] = $key . ':' . $value; |
258
|
|
|
} |
259
|
|
|
} |
260
|
361 |
|
$scope = implode(' ', $permissions); |
261
|
|
|
|
262
|
361 |
|
return $scope; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param string $scope |
267
|
|
|
* @return $this |
268
|
|
|
*/ |
269
|
45 |
|
public function setScope($scope) |
270
|
|
|
{ |
271
|
45 |
|
if (!is_array($scope)) { |
272
|
19 |
|
$scope = explode(' ', $scope); |
273
|
|
|
} |
274
|
45 |
|
$this->scope = $scope; |
275
|
|
|
|
276
|
45 |
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
46 |
|
public function getOauthUrl() |
283
|
|
|
{ |
284
|
46 |
|
switch ($this->getGrantType()) { |
285
|
46 |
|
case static::GRANT_TYPE_ANONYMOUS: |
286
|
11 |
|
return $this->oauthUrl . '/oauth/' . $this->getProject() . '/anonymous/token'; |
287
|
41 |
|
case static::GRANT_TYPE_PASSWORD: |
288
|
36 |
|
case static::GRANT_TYPE_REFRESH: |
289
|
23 |
|
return $this->oauthUrl . '/oauth/' . $this->getProject() . '/customers/token'; |
290
|
|
|
default: |
291
|
36 |
|
return $this->oauthUrl . '/oauth/token'; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param string $oauthUrl |
297
|
|
|
* @return $this |
298
|
|
|
*/ |
299
|
77 |
|
public function setOauthUrl($oauthUrl) |
300
|
|
|
{ |
301
|
77 |
|
$this->oauthUrl = $oauthUrl; |
302
|
|
|
|
303
|
77 |
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
58 |
|
public function getApiUrl() |
310
|
|
|
{ |
311
|
58 |
|
return $this->apiUrl; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param string $apiUrl |
316
|
|
|
* @return $this |
317
|
|
|
*/ |
318
|
77 |
|
public function setApiUrl($apiUrl) |
319
|
|
|
{ |
320
|
77 |
|
$this->apiUrl = $apiUrl; |
321
|
|
|
|
322
|
77 |
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return bool |
327
|
|
|
*/ |
328
|
76 |
|
public function check() |
329
|
|
|
{ |
330
|
76 |
|
if (is_null($this->getClientId())) { |
331
|
4 |
|
throw new InvalidArgumentException(Message::NO_CLIENT_ID); |
332
|
|
|
} |
333
|
|
|
|
334
|
72 |
|
if (is_null($this->getClientSecret())) { |
335
|
|
|
throw new InvalidArgumentException(Message::NO_CLIENT_SECRET); |
336
|
|
|
} |
337
|
|
|
|
338
|
72 |
|
if (is_null($this->getProject())) { |
339
|
|
|
throw new InvalidArgumentException(Message::NO_PROJECT_ID); |
340
|
|
|
} |
341
|
|
|
|
342
|
72 |
|
return true; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @return int |
347
|
|
|
*/ |
348
|
1 |
|
public function getBatchPoolSize() |
349
|
|
|
{ |
350
|
1 |
|
return $this->batchPoolSize; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param int $batchPoolSize |
355
|
|
|
* @return $this |
356
|
|
|
*/ |
357
|
1 |
|
public function setBatchPoolSize($batchPoolSize) |
358
|
|
|
{ |
359
|
1 |
|
$this->batchPoolSize = $batchPoolSize; |
360
|
|
|
|
361
|
1 |
|
return $this; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @return string |
366
|
|
|
*/ |
367
|
66 |
|
public function getAdapter() |
368
|
|
|
{ |
369
|
66 |
|
return $this->adapter; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @param string $adapter |
374
|
|
|
* @return $this |
375
|
|
|
*/ |
376
|
|
|
public function setAdapter($adapter) |
377
|
|
|
{ |
378
|
|
|
$this->adapter = $adapter; |
379
|
|
|
|
380
|
|
|
return $this; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @return bool |
385
|
|
|
*/ |
386
|
58 |
|
public function getThrowExceptions() |
387
|
|
|
{ |
388
|
58 |
|
return $this->throwExceptions; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @param bool $throwExceptions |
393
|
|
|
* @return $this |
394
|
|
|
*/ |
395
|
9 |
|
public function setThrowExceptions($throwExceptions) |
396
|
|
|
{ |
397
|
9 |
|
$this->throwExceptions = $throwExceptions; |
398
|
|
|
|
399
|
9 |
|
return $this; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @return string |
404
|
|
|
*/ |
405
|
66 |
|
public function getAcceptEncoding() |
406
|
|
|
{ |
407
|
66 |
|
return $this->acceptEncoding; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @param string $acceptEncoding |
412
|
|
|
* @return $this |
413
|
|
|
*/ |
414
|
|
|
public function setAcceptEncoding($acceptEncoding) |
415
|
|
|
{ |
416
|
|
|
$this->acceptEncoding = $acceptEncoding; |
417
|
|
|
|
418
|
|
|
return $this; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @return static |
423
|
|
|
*/ |
424
|
89 |
|
public static function of() |
425
|
|
|
{ |
426
|
89 |
|
return new static(); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* @return string |
431
|
|
|
*/ |
432
|
356 |
|
public function getGrantType() |
433
|
|
|
{ |
434
|
356 |
|
return $this->grantType; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* @param string $grantType |
439
|
|
|
* @return $this |
440
|
|
|
*/ |
441
|
33 |
|
public function setGrantType($grantType) |
442
|
|
|
{ |
443
|
33 |
|
$this->grantType = $grantType; |
444
|
|
|
|
445
|
33 |
|
return $this; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* @return string |
450
|
|
|
*/ |
451
|
19 |
|
public function getUsername() |
452
|
|
|
{ |
453
|
19 |
|
return $this->username; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* @param string $username |
458
|
|
|
* @return $this |
459
|
|
|
*/ |
460
|
23 |
|
public function setUsername($username) |
461
|
|
|
{ |
462
|
23 |
|
$this->username = $username; |
463
|
|
|
|
464
|
23 |
|
return $this; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @return string |
469
|
|
|
*/ |
470
|
19 |
|
public function getPassword() |
471
|
|
|
{ |
472
|
19 |
|
return $this->password; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* @param string $password |
477
|
|
|
* @return $this |
478
|
|
|
*/ |
479
|
23 |
|
public function setPassword($password) |
480
|
|
|
{ |
481
|
23 |
|
$this->password = $password; |
482
|
|
|
|
483
|
23 |
|
return $this; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* @return string |
488
|
|
|
*/ |
489
|
28 |
|
public function getRefreshToken() |
490
|
|
|
{ |
491
|
28 |
|
return $this->refreshToken; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* @param string $refreshToken |
496
|
|
|
* @return $this |
497
|
|
|
*/ |
498
|
28 |
|
public function setRefreshToken($refreshToken) |
499
|
|
|
{ |
500
|
28 |
|
$this->refreshToken = $refreshToken; |
501
|
|
|
|
502
|
28 |
|
return $this; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* @return string |
507
|
|
|
*/ |
508
|
11 |
|
public function getAnonymousId() |
509
|
|
|
{ |
510
|
11 |
|
return $this->anonymousId; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* @param string $anonymousId |
515
|
|
|
* @return string |
516
|
|
|
*/ |
517
|
4 |
|
public function setAnonymousId($anonymousId) |
518
|
|
|
{ |
519
|
4 |
|
$this->anonymousId = $anonymousId; |
520
|
|
|
|
521
|
4 |
|
return $this; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* @return string |
526
|
|
|
*/ |
527
|
69 |
|
public function getCacheDir() |
528
|
|
|
{ |
529
|
69 |
|
return $this->cacheDir; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* @param string $cacheDir |
534
|
|
|
* @return $this |
535
|
|
|
*/ |
536
|
|
|
public function setCacheDir($cacheDir) |
537
|
|
|
{ |
538
|
|
|
$this->cacheDir = $cacheDir; |
539
|
|
|
|
540
|
|
|
return $this; |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
|