Passed
Branch refactor_config_class (788a46)
by Jens
08:38
created

Config::check()   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\Client\ClientConfig;
10
use Commercetools\Core\Client\Credentials;
11
use Commercetools\Core\Error\Message;
12
use Commercetools\Core\Error\InvalidArgumentException;
13
use Commercetools\Core\Helper\CorrelationIdProvider;
14
use Commercetools\Core\Helper\DefaultCorrelationIdProvider;
15
use Commercetools\Core\Helper\Uuid;
16
use Commercetools\Core\Model\Common\ContextAwareInterface;
17
use Commercetools\Core\Model\Common\ContextTrait;
18
19
/**
20
 * Client configuration object
21
 *
22
 * @description
23
 *
24
 * Often configuration like credentials is stored in YAML or INI files. To setup the configuration object
25
 * this can be done by the fromArray method.
26
 *
27
 * Configuration file:
28
 *
29
 * ```
30
 * [commercetools]
31
 * client_id = '<client-id>'
32
 * client_secret = '<client-secret>'
33
 * project = '<project>'
34
 * ```
35
 *
36
 * Config instantiation:
37
 *
38
 * ```php
39
 * $iniConfig = parse_ini_file('<config-file>.ini', true);
40
 * $config = Config::fromArray($iniConfig['commercetools']);
41
 * ```
42
 *
43
 * ### Exceptions ###
44
 *
45
 * The client by default suppresses exceptions when a response had been returned by the API and the result
46
 * can be handled afterwards by checking the isError method of the response. For interacting with Exceptions
47
 * they can be enabled with the throwExceptions flag.
48
 *
49
 * ```php
50
 * $config->setThrowExceptions(true);
51
 * $client = new Client($config);
52
 * try {
53
 *     $response = $client->execute($request);
54
 * } catch (\Commercetools\Core\Error\ApiException $e) {
55
 *     // handle Exception
56
 * }
57
 * ```
58
 * @package Commercetools\Core
59
 */
60
class Config extends ConfigObject implements ContextAwareInterface
61
{
62
    use ContextTrait;
63
64
    const OAUTH_URL = 'oauth_url';
65
    const CLIENT_ID = 'client_id';
66
    const CLIENT_SECRET = 'client_secret';
67
    const SCOPE = 'scope';
68
    const PROJECT = 'project';
69
    const API_URL = 'api_url';
70
    const USER_NAME = Credentials::USER_NAME;
71
    const PASSWORD = Credentials::PASSWORD;
72
    const REFRESH_TOKEN = Credentials::REFRESH_TOKEN;
73
    const BEARER_TOKEN = Credentials::BEARER_TOKEN;
74
    const ANONYMOUS_ID = Credentials::ANONYMOUS_ID;
75
    const GRANT_TYPE = Credentials::GRANT_TYPE;
76
77
    const GRANT_TYPE_CLIENT = Credentials::GRANT_TYPE_CLIENT;
78
    const GRANT_TYPE_PASSWORD = Credentials::GRANT_TYPE_PASSWORD;
79
    const GRANT_TYPE_REFRESH = Credentials::GRANT_TYPE_REFRESH;
80
    const GRANT_TYPE_ANONYMOUS = Credentials::GRANT_TYPE_ANONYMOUS;
81
    const GRANT_TYPE_BEARER_TOKEN = Credentials::GRANT_TYPE_BEARER_TOKEN;
82
83
    /**
84
     * @var Credentials
85
     */
86
    protected $credentials;
87
88
    /**
89
     * @var string
90
     */
91
    protected $cacheDir;
92
93
    protected $clientConfig;
94
95
    protected $oauthClientConfig;
96
97 109
    public function __construct()
98
    {
99 109
        $this->enableCorrelationId = Uuid::active();
0 ignored issues
show
Bug Best Practice introduced by
The property enableCorrelationId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
100 109
        $this->credentials = new Credentials();
101 109
        $this->clientConfig = new ClientConfig('https://api.sphere.io');
102 109
        $this->oauthClientConfig = new ClientConfig('https://auth.sphere.io');
103 109
    }
104
105
    /**
106
     * @param array $configValues
107
     * @return static
108
     */
109 105
    public static function fromArray(array $configValues)
110
    {
111 105
        $config = static::of();
112 105
        array_walk(
113 105
            $configValues,
114 105
            function ($value, $key) use ($config) {
115 102
                $functionName = 'set' . $config->camelize($key);
116 102
                if (!is_callable([$config, $functionName])) {
117 1
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
118
                }
119 101
                $config->$functionName($value);
120 105
            }
121
        );
122
123 104
        return $config;
124
    }
125
126 102
    protected function camelize($scored)
127
    {
128 102
        return lcfirst(
129 102
            implode(
130 102
                '',
131 102
                array_map(
132 102
                    'ucfirst',
133 102
                    array_map(
134 102
                        'strtolower',
135 102
                        explode('_', $scored)
136
                    )
137
                )
138
            )
139
        );
140
    }
141
142
    /**
143
     * @deprecated
144
     * @return string
145
     */
146 5
    public function getOauthUrl()
147
    {
148 5
        switch ($this->credentials->getGrantType()) {
149 5
            case static::GRANT_TYPE_ANONYMOUS:
150
                return $this->oauthClientConfig->getBaseUri() . '/oauth/' .
151
                    $this->credentials->getProject() . '/anonymous/token';
152 5
            case static::GRANT_TYPE_PASSWORD:
153 5
            case static::GRANT_TYPE_REFRESH:
154
                return $this->oauthClientConfig->getBaseUri() . '/oauth/' .
155
                    $this->credentials->getProject() . '/customers/token';
156
            default:
157 5
                return $this->oauthClientConfig->getBaseUri() . '/oauth/token';
158
        }
159
    }
160
161
    /**
162
     * @return static
163
     */
164 107
    public static function of()
165
    {
166 107
        return new static();
167
    }
168
169
    /**
170
     * @deprecated
171
     * @return string
172
     */
173 2
    public function getGrantType()
174
    {
175 2
        return $this->credentials->getGrantType();
176
    }
177
178
    /**
179
     * @deprecated
180
     * @param string $grantType
181
     * @return $this
182
     */
183 36
    public function setGrantType($grantType)
184
    {
185 36
        $this->credentials->setGrantType($grantType);
186
187 36
        return $this;
188
    }
189
190
    /**
191
     * @deprecated
192
     * @return string
193
     */
194
    public function getUsername()
195
    {
196
        return $this->credentials->getUsername();
197
    }
198
199
    /**
200
     * @deprecated
201
     * @param string $username
202
     * @return $this
203
     */
204 24
    public function setUsername($username)
205
    {
206 24
        $this->credentials->setUsername($username);
207
208 24
        return $this;
209
    }
210
211
    /**
212
     * @deprecated
213
     * @return string
214
     */
215
    public function getPassword()
216
    {
217
        return $this->credentials->getPassword();
218
    }
219
220
    /**
221
     * @deprecated
222
     * @param string $password
223
     * @return $this
224
     */
225 24
    public function setPassword($password)
226
    {
227 24
        $this->credentials->setPassword($password);
228
229 24
        return $this;
230
    }
231
232
    /**
233
     * @deprecated
234
     * @return string
235
     */
236 1
    public function getRefreshToken()
237
    {
238 1
        return $this->credentials->getRefreshToken();
239
    }
240
241
    /**
242
     * @deprecated
243
     * @param string $refreshToken
244
     * @return $this
245
     */
246 4
    public function setRefreshToken($refreshToken)
247
    {
248 4
        $this->credentials->setRefreshToken($refreshToken);
249
250 4
        return $this;
251
    }
252
253
    /**
254
     * @deprecated
255
     * @return string
256
     */
257
    public function getAnonymousId()
258
    {
259
        return $this->credentials->getAnonymousId();
260
    }
261
262
    /**
263
     * @deprecated
264
     * @param string $anonymousId
265
     * @return $this
266
     */
267 4
    public function setAnonymousId($anonymousId)
268
    {
269 4
        $this->credentials->setAnonymousId($anonymousId);
270
271 4
        return $this;
272
    }
273
274
    /**
275
     * @return string
276
     */
277 81
    public function getCacheDir()
278
    {
279 81
        return $this->cacheDir;
280
    }
281
282
    /**
283
     * @param string $cacheDir
284
     * @return $this
285
     */
286
    public function setCacheDir($cacheDir)
287
    {
288
        $this->cacheDir = $cacheDir;
289
290
        return $this;
291
    }
292
293
    /**
294
     * @deprecated
295
     * @return string
296
     */
297
    public function getLogLevel()
298
    {
299
        return $this->clientConfig->getLogLevel();
300
    }
301
302
    /**
303
     * @deprecated
304
     * @param string $logLevel
305
     * @return $this
306
     */
307 1
    public function setLogLevel($logLevel)
308
    {
309 1
        $this->clientConfig->setLogLevel($logLevel);
310
311 1
        return $this;
312
    }
313
314
    /**
315
     * @deprecated
316
     * @return mixed
317
     */
318
    public function getMessageFormatter()
319
    {
320
        return $this->clientConfig->getMessageFormatter();
321
    }
322
323
    /**
324
     * @deprecated
325
     * @param mixed $messageFormatter
326
     * @return $this
327
     */
328
    public function setMessageFormatter($messageFormatter)
329
    {
330
        $this->clientConfig->setMessageFormatter($messageFormatter);
331
        return $this;
332
    }
333
334
    /**
335
     * @deprecated
336
     * @return CorrelationIdProvider|null
337
     */
338
    public function getCorrelationIdProvider()
339
    {
340
        return $this->clientConfig->getCorrelationIdProvider();
341
    }
342
343
    /**
344
     * @deprecated
345
     * @param CorrelationIdProvider $correlationIdProvider
346
     * @return Config
347
     */
348 1
    public function setCorrelationIdProvider(CorrelationIdProvider $correlationIdProvider)
349
    {
350 1
        $this->clientConfig->setCorrelationIdProvider($correlationIdProvider);
351
352 1
        return $this;
353
    }
354
355
    /**
356
     * @deprecated
357
     * @return bool
358
     */
359
    public function isEnableCorrelationId()
360
    {
361
        return $this->clientConfig->isEnableCorrelationId();
362
    }
363
364
    /**
365
     * @deprecated
366
     * @param bool $enableCorrelationId
367
     * @return Config
368
     */
369 41
    public function setEnableCorrelationId($enableCorrelationId)
370
    {
371 41
        $this->clientConfig->setEnableCorrelationId($enableCorrelationId);
372 41
        return $this;
373
    }
374
375
    /**
376
     * @deprecated
377
     * @return array
378
     */
379
    public function getClientOptions()
380
    {
381
        return $this->clientConfig->getClientOptions();
382
    }
383
384
    /**
385
     * @deprecated
386
     * @param array $clientConfig
387
     * @return Config
388
     */
389
    public function setClientOptions(array $clientConfig)
390
    {
391
        $this->clientConfig->setClientOptions($clientConfig);
392
        return $this;
393
    }
394
395
    /**
396
     * @deprecated
397
     * @return array
398
     */
399
    public function getOAuthClientOptions()
400
    {
401
        return $this->oauthClientConfig->getClientOptions();
402
    }
403
404
    /**
405
     * @deprecated
406
     * @param array $clientOptions
407
     * @return Config
408
     */
409
    public function setOAuthClientOptions(array $clientOptions)
410
    {
411
        $this->oauthClientConfig->setClientOptions($clientOptions);
412
        return $this;
413
    }
414
415
    /**
416
     * @deprecated
417
     * @return string
418
     */
419 1
    public function getBearerToken()
420
    {
421 1
        return $this->credentials->getBearerToken();
422
    }
423
424
    /**
425
     * @deprecated
426
     * @param string $bearerToken
427
     * @return Config
428
     */
429 2
    public function setBearerToken($bearerToken)
430
    {
431 2
        $this->credentials->setBearerToken($bearerToken);
432 2
        return $this;
433
    }
434
435
    /**
436
     * @deprecated
437
     * @return string
438
     */
439 3
    public function getClientSecret()
440
    {
441 3
        return $this->credentials->getClientSecret();
442
    }
443
444
    /**
445
     * @deprecated
446
     * @param string $clientSecret
447
     * @return $this
448
     */
449 92
    public function setClientSecret($clientSecret)
450
    {
451 92
        $this->credentials->setClientSecret($clientSecret);
452
453 92
        return $this;
454
    }
455
456
    /**
457
     * @deprecated
458
     * @return string
459
     */
460 3
    public function getClientId()
461
    {
462 3
        return $this->credentials->getClientId();
463
    }
464
465
    /**
466
     * @deprecated
467
     * @param string $clientId
468
     * @return $this
469
     */
470 92
    public function setClientId($clientId)
471
    {
472 92
        $this->credentials->setClientId($clientId);
473
474 92
        return $this;
475
    }
476
477
    /**
478
     * @deprecated
479
     * @return string
480
     */
481 8
    public function getProject()
482
    {
483 8
        return $this->credentials->getProject();
484
    }
485
486
    /**
487
     * @deprecated
488
     * @param string $project
489
     * @return $this
490
     */
491 102
    public function setProject($project)
492
    {
493 102
        $this->credentials->setProject($project);
494 102
        $this->clientConfig->setProject($project);
495
496 102
        return $this;
497
    }
498
499
    /**
500
     * @deprecated
501
     * @return string
502
     */
503 18
    public function getScope()
504
    {
505 18
        return $this->credentials->getScope();
506
    }
507
508
    /**
509
     * @deprecated
510
     * @param string $scope
511
     * @return $this
512
     */
513 55
    public function setScope($scope)
514
    {
515 55
        $this->credentials->setScope($scope);
516
517 55
        return $this;
518
    }
519
520
    /**
521
     * @deprecated
522
     * @param string $oauthUrl
523
     * @return $this
524
     */
525 90
    public function setOauthUrl($oauthUrl)
526
    {
527 90
        $this->oauthClientConfig->setBaseUri($oauthUrl);
528
529 90
        return $this;
530
    }
531
532
    /**
533
     * @deprecated
534
     * @return string
535
     */
536 6
    public function getApiUrl()
537
    {
538 6
        return $this->clientConfig->getBaseUri();
539
    }
540
541
    /**
542
     * @deprecated
543
     * @param string $apiUrl
544
     * @return $this
545
     */
546 90
    public function setApiUrl($apiUrl)
547
    {
548 90
        $this->clientConfig->setBaseUri($apiUrl);
549
550 90
        return $this;
551
    }
552
553
    /**
554
     * @deprecated
555
     * @return bool
556
     */
557 7
    public function check()
558
    {
559 7
        return $this->credentials->check();
560
    }
561
562
    /**
563
     * @deprecated use getClientOptions()['concurrency'] instead
564
     * @return int
565
     */
566 1
    public function getBatchPoolSize()
567
    {
568 1
        return $this->clientConfig->getBatchPoolSize();
0 ignored issues
show
Deprecated Code introduced by
The function Commercetools\Core\Clien...fig::getBatchPoolSize() has been deprecated: use getClientOptions()['concurrency'] instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

568
        return /** @scrutinizer ignore-deprecated */ $this->clientConfig->getBatchPoolSize();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
569
    }
570
571
    /**
572
     * @deprecated use setClientOptions(['concurrency' => 5]) instead
573
     * @param int $batchPoolSize
574
     * @return $this
575
     */
576 1
    public function setBatchPoolSize($batchPoolSize)
577
    {
578 1
        $this->clientConfig->setBatchPoolSize($batchPoolSize);
0 ignored issues
show
Deprecated Code introduced by
The function Commercetools\Core\Clien...fig::setBatchPoolSize() has been deprecated: use setClientOptions(['concurrency' => 5]) instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

578
        /** @scrutinizer ignore-deprecated */ $this->clientConfig->setBatchPoolSize($batchPoolSize);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
579
580 1
        return $this;
581
    }
582
583
    /**
584
     * @deprecated
585
     * @return string
586
     */
587 1
    public function getAdapter()
588
    {
589 1
        return $this->clientConfig->getAdapter();
590
    }
591
592
    /**
593
     * @deprecated
594
     * @param string $adapter
595
     * @return $this
596
     */
597
    public function setAdapter($adapter)
598
    {
599
        $this->clientConfig->setAdapter($adapter);
600
601
        return $this;
602
    }
603
604
    /**
605
     * @deprecated
606
     * @return bool
607
     */
608
    public function getThrowExceptions()
609
    {
610
        return $this->clientConfig->isThrowExceptions();
611
    }
612
613
    /**
614
     * @deprecated
615
     * @param bool $throwExceptions
616
     * @return $this
617
     */
618 9
    public function setThrowExceptions($throwExceptions)
619
    {
620 9
        $this->clientConfig->setThrowExceptions($throwExceptions);
621
622 9
        return $this;
623
    }
624
625
    /**
626
     * @deprecated
627
     * @return string
628
     */
629
    public function getAcceptEncoding()
630
    {
631
        return $this->clientConfig->getAcceptEncoding();
632
    }
633
634
    /**
635
     * @deprecated
636
     * @param string $acceptEncoding
637
     * @return $this
638
     */
639
    public function setAcceptEncoding($acceptEncoding)
640
    {
641
        $this->clientConfig->setAcceptEncoding($acceptEncoding);
642
643
        return $this;
644
    }
645
646
    /**
647
     * @return Credentials
648
     */
649 81
    public function getCredentials()
650
    {
651 81
        return $this->credentials;
652
    }
653
654
    /**
655
     * @param Credentials $credentials
656
     * @return Config
657
     */
658
    public function setCredentials(Credentials $credentials)
659
    {
660
        $this->credentials = $credentials;
661
        return $this;
662
    }
663
664
    /**
665
     * @return ClientConfig
666
     */
667 85
    public function getClientConfig()
668
    {
669 85
        return $this->clientConfig;
670
    }
671
672
    /**
673
     * @param ClientConfig $clientConfig
674
     * @return Config
675
     */
676
    public function setClientConfig(ClientConfig $clientConfig)
677
    {
678
        $this->clientConfig = $clientConfig;
679
        return $this;
680
    }
681
682
    /**
683
     * @return ClientConfig
684
     */
685 81
    public function getOauthClientConfig()
686
    {
687 81
        return $this->oauthClientConfig;
688
    }
689
690
    /**
691
     * @param ClientConfig $oauthClientConfig
692
     * @return Config
693
     */
694
    public function setOauthClientConfig(ClientConfig $oauthClientConfig)
695
    {
696
        $this->oauthClientConfig = $oauthClientConfig;
697
        return $this;
698
    }
699
}
700