Passed
Push — develop ( 28ee2c...8aa457 )
by Jens
10:01
created

Config   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 515
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.6%

Importance

Changes 0
Metric Value
wmc 50
lcom 1
cbo 2
dl 0
loc 515
ccs 120
cts 131
cp 0.916
rs 8.6206
c 0
b 0
f 0

38 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 16 2
A camelize() 0 15 1
A getClientSecret() 0 4 1
A setClientSecret() 0 6 1
A getClientId() 0 4 1
A setClientId() 0 6 1
A getProject() 0 4 1
A setProject() 0 6 1
A getScope() 0 20 4
A setScope() 0 12 3
A getOauthUrl() 0 12 4
A setOauthUrl() 0 6 1
A getApiUrl() 0 4 1
A setApiUrl() 0 6 1
A check() 0 16 4
A getBatchPoolSize() 0 4 1
A setBatchPoolSize() 0 6 1
A getAdapter() 0 4 1
A setAdapter() 0 6 1
A getThrowExceptions() 0 4 1
A setThrowExceptions() 0 6 1
A getAcceptEncoding() 0 4 1
A setAcceptEncoding() 0 6 1
A of() 0 4 1
A getGrantType() 0 4 1
A setGrantType() 0 6 1
A getUsername() 0 4 1
A setUsername() 0 6 1
A getPassword() 0 4 1
A setPassword() 0 6 1
A getRefreshToken() 0 4 1
A setRefreshToken() 0 6 1
A getAnonymousId() 0 4 1
A setAnonymousId() 0 6 1
A getCacheDir() 0 4 1
A setCacheDir() 0 6 1
A getLogLevel() 0 4 1
A setLogLevel() 0 6 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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