Completed
Pull Request — develop (#333)
by Jens
14:58
created

Config::check()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

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