Completed
Push — master ( 1d2d63...5f432b )
by Jens
09:03
created

Config   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 460
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.6%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 44
c 5
b 0
f 2
lcom 1
cbo 2
dl 0
loc 460
ccs 106
cts 117
cp 0.906
rs 8.3396

34 Methods

Rating   Name   Duplication   Size   Complexity  
A getScope() 0 20 4
A setScope() 0 9 2
A getOauthUrl() 0 10 3
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 getCacheDir() 0 4 1
A setCacheDir() 0 6 1
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 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

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