Passed
Branch refactor_config_class (6c05a2)
by Jens
08:37
created

Config   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 592
Duplicated Lines 0 %

Test Coverage

Coverage 63.7%

Importance

Changes 0
Metric Value
wmc 57
dl 0
loc 592
ccs 93
cts 146
cp 0.637
rs 5.1724
c 0
b 0
f 0

54 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getBearerToken() 0 3 1
A setClientOptions() 0 4 1
A getThrowExceptions() 0 3 1
A getClientId() 0 3 1
A setCredentials() 0 4 1
A getUsername() 0 3 1
A getAcceptEncoding() 0 3 1
A setAdapter() 0 5 1
A setAnonymousId() 0 5 1
A isEnableCorrelationId() 0 3 1
A getAnonymousId() 0 3 1
A getAdapter() 0 3 1
A getRefreshToken() 0 3 1
A setCorrelationIdProvider() 0 5 1
A getPassword() 0 3 1
A getLogLevel() 0 3 1
A setMessageFormatter() 0 4 1
A getOauthUrl() 0 12 4
A setThrowExceptions() 0 5 1
A getOAuthClientOptions() 0 3 1
A getClientOptions() 0 3 1
A setScope() 0 5 1
A setClientId() 0 5 1
A getGrantType() 0 3 1
A setAcceptEncoding() 0 5 1
A getScope() 0 3 1
A setRefreshToken() 0 5 1
A getOauthClientConfig() 0 3 1
A setCacheDir() 0 5 1
A setLogLevel() 0 5 1
A setBearerToken() 0 4 1
A setPassword() 0 5 1
A getCacheDir() 0 3 1
A getMessageFormatter() 0 3 1
A setProject() 0 6 1
A setBatchPoolSize() 0 5 1
A getCorrelationIdProvider() 0 3 1
A check() 0 3 1
A setOauthClientConfig() 0 4 1
A getBatchPoolSize() 0 3 1
A setClientConfig() 0 4 1
A setClientSecret() 0 5 1
A setApiUrl() 0 5 1
A setUsername() 0 5 1
A getProject() 0 3 1
A setEnableCorrelationId() 0 4 1
A getCredentials() 0 3 1
A setGrantType() 0 5 1
A getClientSecret() 0 3 1
A getClientConfig() 0 3 1
A setOAuthClientOptions() 0 4 1
A getApiUrl() 0 3 1
A setOauthUrl() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.

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

522
        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...
523
    }
524
525
    /**
526
     * @deprecated use setClientOptions(['concurrency' => 5]) instead
527
     * @param int $batchPoolSize
528
     * @return $this
529
     */
530 1
    public function setBatchPoolSize($batchPoolSize)
531
    {
532 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

532
        /** @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...
533
534 1
        return $this;
535
    }
536
537
    /**
538
     * @deprecated
539
     * @return string
540
     */
541 1
    public function getAdapter()
542
    {
543 1
        return $this->clientConfig->getAdapter();
544
    }
545
546
    /**
547
     * @deprecated
548
     * @param string $adapter
549
     * @return $this
550
     */
551
    public function setAdapter($adapter)
552
    {
553
        $this->clientConfig->setAdapter($adapter);
554
555
        return $this;
556
    }
557
558
    /**
559
     * @deprecated
560
     * @return bool
561
     */
562
    public function getThrowExceptions()
563
    {
564
        return $this->clientConfig->isThrowExceptions();
565
    }
566
567
    /**
568
     * @deprecated
569
     * @param bool $throwExceptions
570
     * @return $this
571
     */
572 9
    public function setThrowExceptions($throwExceptions)
573
    {
574 9
        $this->clientConfig->setThrowExceptions($throwExceptions);
575
576 9
        return $this;
577
    }
578
579
    /**
580
     * @deprecated
581
     * @return string
582
     */
583
    public function getAcceptEncoding()
584
    {
585
        return $this->clientConfig->getAcceptEncoding();
586
    }
587
588
    /**
589
     * @deprecated
590
     * @param string $acceptEncoding
591
     * @return $this
592
     */
593
    public function setAcceptEncoding($acceptEncoding)
594
    {
595
        $this->clientConfig->setAcceptEncoding($acceptEncoding);
596
597
        return $this;
598
    }
599
600
    /**
601
     * @return Credentials
602
     */
603 81
    public function getCredentials()
604
    {
605 81
        return $this->credentials;
606
    }
607
608
    /**
609
     * @param Credentials $credentials
610
     * @return Config
611
     */
612
    public function setCredentials(Credentials $credentials)
613
    {
614
        $this->credentials = $credentials;
615
        return $this;
616
    }
617
618
    /**
619
     * @return ClientConfig
620
     */
621 85
    public function getClientConfig()
622
    {
623 85
        return $this->clientConfig;
624
    }
625
626
    /**
627
     * @param ClientConfig $clientConfig
628
     * @return Config
629
     */
630
    public function setClientConfig(ClientConfig $clientConfig)
631
    {
632
        $this->clientConfig = $clientConfig;
633
        return $this;
634
    }
635
636
    /**
637
     * @return ClientConfig
638
     */
639 81
    public function getOauthClientConfig()
640
    {
641 81
        return $this->oauthClientConfig;
642
    }
643
644
    /**
645
     * @param ClientConfig $oauthClientConfig
646
     * @return Config
647
     */
648
    public function setOauthClientConfig(ClientConfig $oauthClientConfig)
649
    {
650
        $this->oauthClientConfig = $oauthClientConfig;
651
        return $this;
652
    }
653
}
654