Completed
Push — master ( 82e0b5...620952 )
by Jens
17:43
created

Config::getScope()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

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