Completed
Push — master ( 44676f...b7bc18 )
by Jens
13:18
created

Config::getCorrelationIdProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 3.0416
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\Helper\CorrelationIdProvider;
12
use Commercetools\Core\Helper\DefaultCorrelationIdProvider;
13
use Commercetools\Core\Helper\Uuid;
14
use Commercetools\Core\Model\Common\ContextAwareInterface;
15
use Commercetools\Core\Model\Common\ContextTrait;
16
use Psr\Log\LogLevel;
17
18
/**
19
 * Client configuration object
20
 *
21
 * @description
22
 *
23
 * Often configuration like credentials is stored in YAML or INI files. To setup the configuration object
24
 * this can be done by the fromArray method.
25
 *
26
 * Configuration file:
27
 *
28
 * ```
29
 * [commercetools]
30
 * client_id = '<client-id>'
31
 * client_secret = '<client-secret>'
32
 * project = '<project>'
33
 * ```
34
 *
35
 * Config instantiation:
36
 *
37
 * ```php
38
 * $iniConfig = parse_ini_file('<config-file>.ini', true);
39
 * $config = Config::fromArray($iniConfig['commercetools']);
40
 * ```
41
 *
42
 * ### Exceptions ###
43
 *
44
 * The client by default suppresses exceptions when a response had been returned by the API and the result
45
 * can be handled afterwards by checking the isError method of the response. For interacting with Exceptions
46
 * they can be enabled with the throwExceptions flag.
47
 *
48
 * ```php
49
 * $config->setThrowExceptions(true);
50
 * $client = new Client($config);
51
 * try {
52
 *     $response = $client->execute($request);
53
 * } catch (\Commercetools\Core\Error\ApiException $e) {
54
 *     // handle Exception
55
 * }
56
 * ```
57
 * @package Commercetools\Core
58
 */
59
class Config implements ContextAwareInterface
60
{
61
    use ContextTrait;
62
63
    const OAUTH_URL = 'oauth_url';
64
    const CLIENT_ID = 'client_id';
65
    const CLIENT_SECRET = 'client_secret';
66
    const SCOPE = 'scope';
67
    const PROJECT = 'project';
68
    const API_URL = 'api_url';
69
    const USER_NAME = 'username';
70
    const PASSWORD = 'password';
71
    const REFRESH_TOKEN = 'refresh_token';
72
    const ANONYMOUS_ID = 'anonymous_id';
73
    const GRANT_TYPE = 'grant_type';
74
75
    const GRANT_TYPE_CLIENT = 'client_credentials';
76
    const GRANT_TYPE_PASSWORD = 'password';
77
    const GRANT_TYPE_REFRESH = 'refresh_token';
78
    const GRANT_TYPE_ANONYMOUS = 'anonymous_token';
79
80
    /**
81
     * @var string
82
     */
83
    protected $clientSecret;
84
85
    /**
86
     * @var string
87
     */
88
    protected $clientId;
89
90
    /**
91
     * @var string
92
     */
93
    protected $project;
94
95
    /**
96
     * @var array
97
     */
98
    protected $scope = ['manage_project'];
99
100
    /**
101
     * @var string
102
     */
103
    protected $oauthUrl = 'https://auth.sphere.io';
104
105
    /**
106
     * @var string
107
     */
108
    protected $apiUrl = 'https://api.sphere.io';
109
110
    /**
111
     * @var int
112
     */
113
    protected $batchPoolSize = 25;
114
115
    protected $adapter;
116
117
    /**
118
     * @var bool
119
     */
120
    protected $throwExceptions = false;
121
122
    protected $acceptEncoding = 'gzip';
123
124
    protected $grantType = 'client_credentials';
125
126
    /**
127
     * @var string
128
     */
129
    protected $username;
130
131
    /**
132
     * @var string
133
     */
134
    protected $password;
135
136
    /**
137
     * @var string
138
     */
139
    protected $refreshToken;
140
141
    /**
142
     * @var string
143
     */
144
    protected $anonymousId;
145
146
    /**
147
     * @var string
148
     */
149
    protected $cacheDir;
150
151
    /**
152
     * @var string
153
     */
154
    protected $logLevel = LogLevel::INFO;
155
156
    protected $messageFormatter;
157
158
    /**
159
     * @var bool
160
     */
161
    protected $enableCorrelationId = false;
162
163
    /**
164
     * @var CorrelationIdProvider
165
     */
166
    protected $correlationIdProvider;
167
168
    protected $clientOptions = [];
169
170 104
    public function __construct()
171
    {
172 104
        $this->enableCorrelationId = Uuid::active();
173 104
    }
174
175
    /**
176
     * @param array $configValues
177
     * @return static
178
     */
179 101
    public static function fromArray(array $configValues)
180
    {
181 101
        $config = static::of();
182 101
        array_walk(
183 101
            $configValues,
184 101
            function ($value, $key) use ($config) {
185 99
                $functionName = 'set' . $config->camelize($key);
186 99
                if (!is_callable([$config, $functionName])) {
187 1
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
188
                }
189 98
                $config->$functionName($value);
190 101
            }
191
        );
192
193 100
        return $config;
194
    }
195
196 99
    protected function camelize($scored)
197
    {
198 99
        return lcfirst(
199 99
            implode(
200 99
                '',
201 99
                array_map(
202 99
                    'ucfirst',
203 99
                    array_map(
204 99
                        'strtolower',
205 99
                        explode('_', $scored)
206
                    )
207
                )
208
            )
209
        );
210
    }
211
212
    /**
213
     * @return string
214
     */
215 84
    public function getClientSecret()
216
    {
217 84
        return $this->clientSecret;
218
    }
219
220
    /**
221
     * @param string $clientSecret
222
     * @return $this
223
     */
224 89
    public function setClientSecret($clientSecret)
225
    {
226 89
        $this->clientSecret = $clientSecret;
227
228 89
        return $this;
229
    }
230
231
    /**
232
     * @return string
233
     */
234 88
    public function getClientId()
235
    {
236 88
        return $this->clientId;
237
    }
238
239
    /**
240
     * @param string $clientId
241
     * @return $this
242
     */
243 89
    public function setClientId($clientId)
244
    {
245 89
        $this->clientId = $clientId;
246
247 89
        return $this;
248
    }
249
250
    /**
251
     * @return string
252
     */
253 554
    public function getProject()
254
    {
255 554
        return $this->project;
256
    }
257
258
    /**
259
     * @param string $project
260
     * @return $this
261
     */
262 97
    public function setProject($project)
263
    {
264 97
        $this->project = $project;
265
266 97
        return $this;
267
    }
268
269
    /**
270
     * @return string
271
     */
272 516
    public function getScope()
273
    {
274 516
        $scope = $this->scope;
275 516
        $project = $this->getProject();
276
277 516
        $permissions = [];
278 516
        foreach ($scope as $key => $value) {
279 511
            if (is_numeric($key)) { // scope defined as string e.g. scope:project_key
280 510
                if (strpos($value, ':') === false) { // scope without project key
281 509
                    $value = $value . ':' . $project;
282
                }
283 510
                $permissions[] = $value;
284
            } else { // scope defined as array
285 511
                $permissions[] = $key . ':' . $value;
286
            }
287
        }
288 516
        $scope = implode(' ', $permissions);
289
290 516
        return $scope;
291
    }
292
293
    /**
294
     * @param string $scope
295
     * @return $this
296
     */
297 54
    public function setScope($scope)
298
    {
299 54
        if (empty($scope)) {
300 5
            $scope = [];
301
        }
302 54
        if (!is_array($scope)) {
303 24
            $scope = explode(' ', $scope);
304
        }
305 54
        $this->scope = $scope;
306
307 54
        return $this;
308
    }
309
310
    /**
311
     * @return string
312
     */
313 51
    public function getOauthUrl()
314
    {
315 51
        switch ($this->getGrantType()) {
316 51
            case static::GRANT_TYPE_ANONYMOUS:
317 11
                return $this->oauthUrl . '/oauth/' . $this->getProject() . '/anonymous/token';
318 46
            case static::GRANT_TYPE_PASSWORD:
319 41
            case static::GRANT_TYPE_REFRESH:
320 23
                return $this->oauthUrl . '/oauth/' . $this->getProject() . '/customers/token';
321
            default:
322 41
                return $this->oauthUrl . '/oauth/token';
323
        }
324
    }
325
326
    /**
327
     * @param string $oauthUrl
328
     * @return $this
329
     */
330 89
    public function setOauthUrl($oauthUrl)
331
    {
332 89
        $this->oauthUrl = $oauthUrl;
333
334 89
        return $this;
335
    }
336
337
    /**
338
     * @return string
339
     */
340 63
    public function getApiUrl()
341
    {
342 63
        return $this->apiUrl;
343
    }
344
345
    /**
346
     * @param string $apiUrl
347
     * @return $this
348
     */
349 89
    public function setApiUrl($apiUrl)
350
    {
351 89
        $this->apiUrl = $apiUrl;
352
353 89
        return $this;
354
    }
355
356
    /**
357
     * @return bool
358
     */
359 84
    public function check()
360
    {
361 84
        if (is_null($this->getClientId())) {
362 4
            throw new InvalidArgumentException(Message::NO_CLIENT_ID);
363
        }
364
365 80
        if (is_null($this->getClientSecret())) {
366
            throw new InvalidArgumentException(Message::NO_CLIENT_SECRET);
367
        }
368
369 80
        if (is_null($this->getProject())) {
370
            throw new InvalidArgumentException(Message::NO_PROJECT_ID);
371
        }
372
373 80
        return true;
374
    }
375
376
    /**
377
     * @deprecated use getClientOptions()['concurrency'] instead
378
     * @return int
379
     */
380 1
    public function getBatchPoolSize()
381
    {
382 1
        if (!isset($this->clientOptions['concurrency'])) {
383 1
            return $this->batchPoolSize;
384
        }
385 1
        return $this->clientOptions['concurrency'];
386
    }
387
388
    /**
389
     * @deprecated use setClientOptions(['concurrency' => 5]) instead
390
     * @param int $batchPoolSize
391
     * @return $this
392
     */
393 1
    public function setBatchPoolSize($batchPoolSize)
394
    {
395 1
        $this->clientOptions['concurrency'] = $batchPoolSize;
396 1
        $this->batchPoolSize = $batchPoolSize;
397
398 1
        return $this;
399
    }
400
401
    /**
402
     * @return string
403
     */
404 75
    public function getAdapter()
405
    {
406 75
        return $this->adapter;
407
    }
408
409
    /**
410
     * @param string $adapter
411
     * @return $this
412
     */
413
    public function setAdapter($adapter)
414
    {
415
        $this->adapter = $adapter;
416
417
        return $this;
418
    }
419
420
    /**
421
     * @return bool
422
     */
423 122
    public function getThrowExceptions()
424
    {
425 122
        return $this->throwExceptions;
426
    }
427
428
    /**
429
     * @param bool $throwExceptions
430
     * @return $this
431
     */
432 9
    public function setThrowExceptions($throwExceptions)
433
    {
434 9
        $this->throwExceptions = (bool)$throwExceptions;
435
436 9
        return $this;
437
    }
438
439
    /**
440
     * @return string
441
     */
442 74
    public function getAcceptEncoding()
443
    {
444 74
        return $this->acceptEncoding;
445
    }
446
447
    /**
448
     * @param string $acceptEncoding
449
     * @return $this
450
     */
451
    public function setAcceptEncoding($acceptEncoding)
452
    {
453
        $this->acceptEncoding = $acceptEncoding;
454
455
        return $this;
456
    }
457
458
    /**
459
     * @return static
460
     */
461 101
    public static function of()
462
    {
463 101
        return new static();
464
    }
465
466
    /**
467
     * @return string
468
     */
469 509
    public function getGrantType()
470
    {
471 509
        return $this->grantType;
472
    }
473
474
    /**
475
     * @param string $grantType
476
     * @return $this
477
     */
478 33
    public function setGrantType($grantType)
479
    {
480 33
        $this->grantType = $grantType;
481
482 33
        return $this;
483
    }
484
485
    /**
486
     * @return string
487
     */
488 19
    public function getUsername()
489
    {
490 19
        return $this->username;
491
    }
492
493
    /**
494
     * @param string $username
495
     * @return $this
496
     */
497 23
    public function setUsername($username)
498
    {
499 23
        $this->username = $username;
500
501 23
        return $this;
502
    }
503
504
    /**
505
     * @return string
506
     */
507 19
    public function getPassword()
508
    {
509 19
        return $this->password;
510
    }
511
512
    /**
513
     * @param string $password
514
     * @return $this
515
     */
516 23
    public function setPassword($password)
517
    {
518 23
        $this->password = $password;
519
520 23
        return $this;
521
    }
522
523
    /**
524
     * @return string
525
     */
526 28
    public function getRefreshToken()
527
    {
528 28
        return $this->refreshToken;
529
    }
530
531
    /**
532
     * @param string $refreshToken
533
     * @return $this
534
     */
535 28
    public function setRefreshToken($refreshToken)
536
    {
537 28
        $this->refreshToken = $refreshToken;
538
539 28
        return $this;
540
    }
541
542
    /**
543
     * @return string
544
     */
545 11
    public function getAnonymousId()
546
    {
547 11
        return $this->anonymousId;
548
    }
549
550
    /**
551
     * @param string $anonymousId
552
     * @return string
553
     */
554 4
    public function setAnonymousId($anonymousId)
555
    {
556 4
        $this->anonymousId = $anonymousId;
557
558 4
        return $this;
559
    }
560
561
    /**
562
     * @return string
563
     */
564 77
    public function getCacheDir()
565
    {
566 77
        return $this->cacheDir;
567
    }
568
569
    /**
570
     * @param string $cacheDir
571
     * @return $this
572
     */
573
    public function setCacheDir($cacheDir)
574
    {
575
        $this->cacheDir = $cacheDir;
576
577
        return $this;
578
    }
579
580
    /**
581
     * @return string
582
     */
583 40
    public function getLogLevel()
584
    {
585 40
        return $this->logLevel;
586
    }
587
588
    /**
589
     * @param string $logLevel
590
     * @return $this
591
     */
592 1
    public function setLogLevel($logLevel)
593
    {
594 1
        $this->logLevel = $logLevel;
595
596 1
        return $this;
597
    }
598
599
    /**
600
     * @return mixed
601
     */
602 40
    public function getMessageFormatter()
603
    {
604 40
        return $this->messageFormatter;
605
    }
606
607
    /**
608
     * @param mixed $messageFormatter
609
     * @return $this
610
     */
611
    public function setMessageFormatter($messageFormatter)
612
    {
613
        $this->messageFormatter = $messageFormatter;
614
        return $this;
615
    }
616
617
    /**
618
     * @return CorrelationIdProvider|null
619
     */
620 73
    public function getCorrelationIdProvider()
621
    {
622 73
        if (!$this->isEnableCorrelationId()) {
623
            return null;
624
        }
625 73
        if (is_null($this->correlationIdProvider)) {
626 71
            $this->correlationIdProvider = DefaultCorrelationIdProvider::of($this->getProject());
627
        }
628 73
        return $this->correlationIdProvider;
629
    }
630
631
    /**
632
     * @param CorrelationIdProvider $correlationIdProvider
633
     * @return Config
634
     */
635 2
    public function setCorrelationIdProvider(CorrelationIdProvider $correlationIdProvider)
636
    {
637 2
        $this->correlationIdProvider = $correlationIdProvider;
638 2
        $this->setEnableCorrelationId(true);
639 2
        return $this;
640
    }
641
642
    /**
643
     * @return bool
644
     */
645 73
    public function isEnableCorrelationId()
646
    {
647 73
        return $this->enableCorrelationId;
648
    }
649
650
    /**
651
     * @param bool $enableCorrelationId
652
     * @return Config
653
     */
654 40
    public function setEnableCorrelationId($enableCorrelationId)
655
    {
656 40
        $this->enableCorrelationId = (bool)$enableCorrelationId;
657 40
        return $this;
658
    }
659
660
    /**
661
     * @return array
662
     */
663 58
    public function getClientOptions()
664
    {
665 58
        return $this->clientOptions;
666
    }
667
668
    /**
669
     * @param array $clientOptions
670
     * @return Config
671
     */
672
    public function setClientOptions(array $clientOptions)
673
    {
674
        $this->clientOptions = $clientOptions;
675
        return $this;
676
    }
677
}
678