Passed
Branch refactor_config_class (b4bd7c)
by Jens
07:34
created

Config::isEnableCorrelationId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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->credentials = new Credentials();
100 109
        $this->clientConfig = new ClientConfig('https://api.sphere.io');
101 109
        $this->oauthClientConfig = new ClientConfig('https://auth.sphere.io');
102 109
    }
103
104
    /**
105
     * @param array $configValues
106
     * @return static
107
     */
108 105
    public static function fromArray(array $configValues)
109
    {
110 105
        $config = static::of();
111 105
        array_walk(
112 105
            $configValues,
113 105
            function ($value, $key) use ($config) {
114 102
                $functionName = 'set' . $config->camelize($key);
115 102
                if (!is_callable([$config, $functionName])) {
116 1
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
117
                }
118 101
                $config->$functionName($value);
119 105
            }
120
        );
121
122 104
        return $config;
123
    }
124
125 102
    protected function camelize($scored)
126
    {
127 102
        return lcfirst(
128 102
            implode(
129 102
                '',
130 102
                array_map(
131 102
                    'ucfirst',
132 102
                    array_map(
133 102
                        'strtolower',
134 102
                        explode('_', $scored)
135
                    )
136
                )
137
            )
138
        );
139
    }
140
141
    /**
142
     * @deprecated
143
     * @return string
144
     */
145 5
    public function getOauthUrl()
146
    {
147 5
        switch ($this->credentials->getGrantType()) {
148 5
            case static::GRANT_TYPE_ANONYMOUS:
149
                return $this->oauthClientConfig->getBaseUri() . '/oauth/' .
150
                    $this->credentials->getProject() . '/anonymous/token';
151 5
            case static::GRANT_TYPE_PASSWORD:
152 5
            case static::GRANT_TYPE_REFRESH:
153
                return $this->oauthClientConfig->getBaseUri() . '/oauth/' .
154
                    $this->credentials->getProject() . '/customers/token';
155
            default:
156 5
                return $this->oauthClientConfig->getBaseUri() . '/oauth/token';
157
        }
158
    }
159
160
    /**
161
     * @return static
162
     */
163 107
    public static function of()
164
    {
165 107
        return new static();
166
    }
167
168
    /**
169
     * @deprecated
170
     * @return string
171
     */
172 2
    public function getGrantType()
173
    {
174 2
        return $this->credentials->getGrantType();
175
    }
176
177
    /**
178
     * @deprecated
179
     * @param string $grantType
180
     * @return $this
181
     */
182 36
    public function setGrantType($grantType)
183
    {
184 36
        $this->credentials->setGrantType($grantType);
185
186 36
        return $this;
187
    }
188
189
    /**
190
     * @deprecated
191
     * @return string
192
     */
193
    public function getUsername()
194
    {
195
        return $this->credentials->getUsername();
196
    }
197
198
    /**
199
     * @deprecated
200
     * @param string $username
201
     * @return $this
202
     */
203 24
    public function setUsername($username)
204
    {
205 24
        $this->credentials->setUsername($username);
206
207 24
        return $this;
208
    }
209
210
    /**
211
     * @deprecated
212
     * @return string
213
     */
214
    public function getPassword()
215
    {
216
        return $this->credentials->getPassword();
217
    }
218
219
    /**
220
     * @deprecated
221
     * @param string $password
222
     * @return $this
223
     */
224 24
    public function setPassword($password)
225
    {
226 24
        $this->credentials->setPassword($password);
227
228 24
        return $this;
229
    }
230
231
    /**
232
     * @deprecated
233
     * @return string
234
     */
235 1
    public function getRefreshToken()
236
    {
237 1
        return $this->credentials->getRefreshToken();
238
    }
239
240
    /**
241
     * @deprecated
242
     * @param string $refreshToken
243
     * @return $this
244
     */
245 4
    public function setRefreshToken($refreshToken)
246
    {
247 4
        $this->credentials->setRefreshToken($refreshToken);
248
249 4
        return $this;
250
    }
251
252
    /**
253
     * @deprecated
254
     * @return string
255
     */
256
    public function getAnonymousId()
257
    {
258
        return $this->credentials->getAnonymousId();
259
    }
260
261
    /**
262
     * @deprecated
263
     * @param string $anonymousId
264
     * @return $this
265
     */
266 4
    public function setAnonymousId($anonymousId)
267
    {
268 4
        $this->credentials->setAnonymousId($anonymousId);
269
270 4
        return $this;
271
    }
272
273
    /**
274
     * @return string
275
     */
276 81
    public function getCacheDir()
277
    {
278 81
        return $this->cacheDir;
279
    }
280
281
    /**
282
     * @param string $cacheDir
283
     * @return $this
284
     */
285
    public function setCacheDir($cacheDir)
286
    {
287
        $this->cacheDir = $cacheDir;
288
289
        return $this;
290
    }
291
292
    /**
293
     * @deprecated
294
     * @return string
295
     */
296
    public function getLogLevel()
297
    {
298
        return $this->clientConfig->getLogLevel();
299
    }
300
301
    /**
302
     * @deprecated
303
     * @param string $logLevel
304
     * @return $this
305
     */
306 1
    public function setLogLevel($logLevel)
307
    {
308 1
        $this->clientConfig->setLogLevel($logLevel);
309
310 1
        return $this;
311
    }
312
313
    /**
314
     * @deprecated
315
     * @return mixed
316
     */
317
    public function getMessageFormatter()
318
    {
319
        return $this->clientConfig->getMessageFormatter();
320
    }
321
322
    /**
323
     * @deprecated
324
     * @param mixed $messageFormatter
325
     * @return $this
326
     */
327
    public function setMessageFormatter($messageFormatter)
328
    {
329
        $this->clientConfig->setMessageFormatter($messageFormatter);
330
        return $this;
331
    }
332
333
    /**
334
     * @deprecated
335
     * @return CorrelationIdProvider|null
336
     */
337
    public function getCorrelationIdProvider()
338
    {
339
        return $this->clientConfig->getCorrelationIdProvider();
340
    }
341
342
    /**
343
     * @deprecated
344
     * @param CorrelationIdProvider $correlationIdProvider
345
     * @return Config
346
     */
347 1
    public function setCorrelationIdProvider(CorrelationIdProvider $correlationIdProvider)
348
    {
349 1
        $this->clientConfig->setCorrelationIdProvider($correlationIdProvider);
350
351 1
        return $this;
352
    }
353
354
    /**
355
     * @deprecated
356
     * @return bool
357
     */
358
    public function isEnableCorrelationId()
359
    {
360
        return $this->clientConfig->isEnableCorrelationId();
361
    }
362
363
    /**
364
     * @deprecated
365
     * @param bool $enableCorrelationId
366
     * @return Config
367
     */
368 41
    public function setEnableCorrelationId($enableCorrelationId)
369
    {
370 41
        $this->clientConfig->setEnableCorrelationId($enableCorrelationId);
371 41
        return $this;
372
    }
373
374
    /**
375
     * @deprecated
376
     * @return array
377
     */
378
    public function getClientOptions()
379
    {
380
        return $this->clientConfig->getClientOptions();
381
    }
382
383
    /**
384
     * @deprecated
385
     * @param array $clientConfig
386
     * @return Config
387
     */
388
    public function setClientOptions(array $clientConfig)
389
    {
390
        $this->clientConfig->setClientOptions($clientConfig);
391
        return $this;
392
    }
393
394
    /**
395
     * @deprecated
396
     * @return array
397
     */
398
    public function getOAuthClientOptions()
399
    {
400
        return $this->oauthClientConfig->getClientOptions();
401
    }
402
403
    /**
404
     * @deprecated
405
     * @param array $clientOptions
406
     * @return Config
407
     */
408
    public function setOAuthClientOptions(array $clientOptions)
409
    {
410
        $this->oauthClientConfig->setClientOptions($clientOptions);
411
        return $this;
412
    }
413
414
    /**
415
     * @deprecated
416
     * @return string
417
     */
418 1
    public function getBearerToken()
419
    {
420 1
        return $this->credentials->getBearerToken();
421
    }
422
423
    /**
424
     * @deprecated
425
     * @param string $bearerToken
426
     * @return Config
427
     */
428 2
    public function setBearerToken($bearerToken)
429
    {
430 2
        $this->credentials->setBearerToken($bearerToken);
431 2
        return $this;
432
    }
433
434
    /**
435
     * @deprecated
436
     * @return string
437
     */
438 3
    public function getClientSecret()
439
    {
440 3
        return $this->credentials->getClientSecret();
441
    }
442
443
    /**
444
     * @deprecated
445
     * @param string $clientSecret
446
     * @return $this
447
     */
448 92
    public function setClientSecret($clientSecret)
449
    {
450 92
        $this->credentials->setClientSecret($clientSecret);
451
452 92
        return $this;
453
    }
454
455
    /**
456
     * @deprecated
457
     * @return string
458
     */
459 3
    public function getClientId()
460
    {
461 3
        return $this->credentials->getClientId();
462
    }
463
464
    /**
465
     * @deprecated
466
     * @param string $clientId
467
     * @return $this
468
     */
469 92
    public function setClientId($clientId)
470
    {
471 92
        $this->credentials->setClientId($clientId);
472
473 92
        return $this;
474
    }
475
476
    /**
477
     * @deprecated
478
     * @return string
479
     */
480 8
    public function getProject()
481
    {
482 8
        return $this->credentials->getProject();
483
    }
484
485
    /**
486
     * @deprecated
487
     * @param string $project
488
     * @return $this
489
     */
490 102
    public function setProject($project)
491
    {
492 102
        $this->credentials->setProject($project);
493 102
        $this->clientConfig->setProject($project);
494
495 102
        return $this;
496
    }
497
498
    /**
499
     * @deprecated
500
     * @return string
501
     */
502 18
    public function getScope()
503
    {
504 18
        return $this->credentials->getScope();
505
    }
506
507
    /**
508
     * @deprecated
509
     * @param string $scope
510
     * @return $this
511
     */
512 55
    public function setScope($scope)
513
    {
514 55
        $this->credentials->setScope($scope);
515
516 55
        return $this;
517
    }
518
519
    /**
520
     * @deprecated
521
     * @param string $oauthUrl
522
     * @return $this
523
     */
524 90
    public function setOauthUrl($oauthUrl)
525
    {
526 90
        $this->oauthClientConfig->setBaseUri($oauthUrl);
527
528 90
        return $this;
529
    }
530
531
    /**
532
     * @deprecated
533
     * @return string
534
     */
535 6
    public function getApiUrl()
536
    {
537 6
        return $this->clientConfig->getBaseUri();
538
    }
539
540
    /**
541
     * @deprecated
542
     * @param string $apiUrl
543
     * @return $this
544
     */
545 90
    public function setApiUrl($apiUrl)
546
    {
547 90
        $this->clientConfig->setBaseUri($apiUrl);
548
549 90
        return $this;
550
    }
551
552
    /**
553
     * @deprecated
554
     * @return bool
555
     */
556 7
    public function check()
557
    {
558 7
        return $this->credentials->check();
559
    }
560
561
    /**
562
     * @deprecated use getClientOptions()['concurrency'] instead
563
     * @return int
564
     */
565 1
    public function getBatchPoolSize()
566
    {
567 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

567
        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...
568
    }
569
570
    /**
571
     * @deprecated use setClientOptions(['concurrency' => 5]) instead
572
     * @param int $batchPoolSize
573
     * @return $this
574
     */
575 1
    public function setBatchPoolSize($batchPoolSize)
576
    {
577 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

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