Passed
Push — develop ( 53aaa0...5746c4 )
by Jens
07:20
created

Config::getClientOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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