Passed
Push — develop ( 58ad97...afcaed )
by Jens
12:59 queued 04:03
created

Config::getAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    protected $clientOptions = [];
168
169
    /**
170
     * @param array $configValues
171
     * @return static
172
     */
173 101
    public static function fromArray(array $configValues)
174
    {
175 101
        $config = static::of();
176 101
        array_walk(
177 101
            $configValues,
178 101
            function ($value, $key) use ($config) {
179 99
                $functionName = 'set' . $config->camelize($key);
180 99
                if (!is_callable([$config, $functionName])) {
181 1
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
182
                }
183 98
                $config->$functionName($value);
184 101
            }
185
        );
186
187 100
        return $config;
188
    }
189
190 99
    protected function camelize($scored)
191
    {
192 99
        return lcfirst(
193 99
            implode(
194 99
                '',
195 99
                array_map(
196 99
                    'ucfirst',
197 99
                    array_map(
198 99
                        'strtolower',
199 99
                        explode('_', $scored)
200
                    )
201
                )
202
            )
203
        );
204
    }
205
206
    /**
207
     * @return string
208
     */
209 84
    public function getClientSecret()
210
    {
211 84
        return $this->clientSecret;
212
    }
213
214
    /**
215
     * @param string $clientSecret
216
     * @return $this
217
     */
218 89
    public function setClientSecret($clientSecret)
219
    {
220 89
        $this->clientSecret = $clientSecret;
221
222 89
        return $this;
223
    }
224
225
    /**
226
     * @return string
227
     */
228 88
    public function getClientId()
229
    {
230 88
        return $this->clientId;
231
    }
232
233
    /**
234
     * @param string $clientId
235
     * @return $this
236
     */
237 89
    public function setClientId($clientId)
238
    {
239 89
        $this->clientId = $clientId;
240
241 89
        return $this;
242
    }
243
244
    /**
245
     * @return string
246
     */
247 534
    public function getProject()
248
    {
249 534
        return $this->project;
250
    }
251
252
    /**
253
     * @param string $project
254
     * @return $this
255
     */
256 97
    public function setProject($project)
257
    {
258 97
        $this->project = $project;
259
260 97
        return $this;
261
    }
262
263
    /**
264
     * @return string
265
     */
266 496
    public function getScope()
267
    {
268 496
        $scope = $this->scope;
269 496
        $project = $this->getProject();
270
271 496
        $permissions = [];
272 496
        foreach ($scope as $key => $value) {
273 491
            if (is_numeric($key)) { // scope defined as string e.g. scope:project_key
274 490
                if (strpos($value, ':') === false) { // scope without project key
275 489
                    $value = $value . ':' . $project;
276
                }
277 490
                $permissions[] = $value;
278
            } else { // scope defined as array
279 491
                $permissions[] = $key . ':' . $value;
280
            }
281
        }
282 496
        $scope = implode(' ', $permissions);
283
284 496
        return $scope;
285
    }
286
287
    /**
288
     * @param string $scope
289
     * @return $this
290
     */
291 54
    public function setScope($scope)
292
    {
293 54
        if (empty($scope)) {
294 5
            $scope = [];
295
        }
296 54
        if (!is_array($scope)) {
297 24
            $scope = explode(' ', $scope);
298
        }
299 54
        $this->scope = $scope;
300
301 54
        return $this;
302
    }
303
304
    /**
305
     * @return string
306
     */
307 51
    public function getOauthUrl()
308
    {
309 51
        switch ($this->getGrantType()) {
310 51
            case static::GRANT_TYPE_ANONYMOUS:
311 11
                return $this->oauthUrl . '/oauth/' . $this->getProject() . '/anonymous/token';
312 46
            case static::GRANT_TYPE_PASSWORD:
313 41
            case static::GRANT_TYPE_REFRESH:
314 23
                return $this->oauthUrl . '/oauth/' . $this->getProject() . '/customers/token';
315
            default:
316 41
                return $this->oauthUrl . '/oauth/token';
317
        }
318
    }
319
320
    /**
321
     * @param string $oauthUrl
322
     * @return $this
323
     */
324 89
    public function setOauthUrl($oauthUrl)
325
    {
326 89
        $this->oauthUrl = $oauthUrl;
327
328 89
        return $this;
329
    }
330
331
    /**
332
     * @return string
333
     */
334 63
    public function getApiUrl()
335
    {
336 63
        return $this->apiUrl;
337
    }
338
339
    /**
340
     * @param string $apiUrl
341
     * @return $this
342
     */
343 89
    public function setApiUrl($apiUrl)
344
    {
345 89
        $this->apiUrl = $apiUrl;
346
347 89
        return $this;
348
    }
349
350
    /**
351
     * @return bool
352
     */
353 84
    public function check()
354
    {
355 84
        if (is_null($this->getClientId())) {
356 4
            throw new InvalidArgumentException(Message::NO_CLIENT_ID);
357
        }
358
359 80
        if (is_null($this->getClientSecret())) {
360
            throw new InvalidArgumentException(Message::NO_CLIENT_SECRET);
361
        }
362
363 80
        if (is_null($this->getProject())) {
364
            throw new InvalidArgumentException(Message::NO_PROJECT_ID);
365
        }
366
367 80
        return true;
368
    }
369
370
    /**
371
     * @deprecated use getClientOptions()['concurrency'] instead
372
     * @return int
373
     */
374 1
    public function getBatchPoolSize()
375
    {
376 1
        if (!isset($this->clientOptions['concurrency'])) {
377 1
            return $this->batchPoolSize;
378
        }
379 1
        return $this->clientOptions['concurrency'];
380
    }
381
382
    /**
383
     * @deprecated use setClientOptions(['concurrency' => 5]) instead
384
     * @param int $batchPoolSize
385
     * @return $this
386
     */
387 1
    public function setBatchPoolSize($batchPoolSize)
388
    {
389 1
        $this->clientOptions['concurrency'] = $batchPoolSize;
390 1
        $this->batchPoolSize = $batchPoolSize;
391
392 1
        return $this;
393
    }
394
395
    /**
396
     * @return string
397
     */
398 75
    public function getAdapter()
399
    {
400 75
        return $this->adapter;
401
    }
402
403
    /**
404
     * @param string $adapter
405
     * @return $this
406
     */
407
    public function setAdapter($adapter)
408
    {
409
        $this->adapter = $adapter;
410
411
        return $this;
412
    }
413
414
    /**
415
     * @return bool
416
     */
417 122
    public function getThrowExceptions()
418
    {
419 122
        return $this->throwExceptions;
420
    }
421
422
    /**
423
     * @param bool $throwExceptions
424
     * @return $this
425
     */
426 9
    public function setThrowExceptions($throwExceptions)
427
    {
428 9
        $this->throwExceptions = (bool)$throwExceptions;
429
430 9
        return $this;
431
    }
432
433
    /**
434
     * @return string
435
     */
436 74
    public function getAcceptEncoding()
437
    {
438 74
        return $this->acceptEncoding;
439
    }
440
441
    /**
442
     * @param string $acceptEncoding
443
     * @return $this
444
     */
445
    public function setAcceptEncoding($acceptEncoding)
446
    {
447
        $this->acceptEncoding = $acceptEncoding;
448
449
        return $this;
450
    }
451
452
    /**
453
     * @return static
454
     */
455 101
    public static function of()
456
    {
457 101
        return new static();
458
    }
459
460
    /**
461
     * @return string
462
     */
463 489
    public function getGrantType()
464
    {
465 489
        return $this->grantType;
466
    }
467
468
    /**
469
     * @param string $grantType
470
     * @return $this
471
     */
472 33
    public function setGrantType($grantType)
473
    {
474 33
        $this->grantType = $grantType;
475
476 33
        return $this;
477
    }
478
479
    /**
480
     * @return string
481
     */
482 19
    public function getUsername()
483
    {
484 19
        return $this->username;
485
    }
486
487
    /**
488
     * @param string $username
489
     * @return $this
490
     */
491 23
    public function setUsername($username)
492
    {
493 23
        $this->username = $username;
494
495 23
        return $this;
496
    }
497
498
    /**
499
     * @return string
500
     */
501 19
    public function getPassword()
502
    {
503 19
        return $this->password;
504
    }
505
506
    /**
507
     * @param string $password
508
     * @return $this
509
     */
510 23
    public function setPassword($password)
511
    {
512 23
        $this->password = $password;
513
514 23
        return $this;
515
    }
516
517
    /**
518
     * @return string
519
     */
520 28
    public function getRefreshToken()
521
    {
522 28
        return $this->refreshToken;
523
    }
524
525
    /**
526
     * @param string $refreshToken
527
     * @return $this
528
     */
529 28
    public function setRefreshToken($refreshToken)
530
    {
531 28
        $this->refreshToken = $refreshToken;
532
533 28
        return $this;
534
    }
535
536
    /**
537
     * @return string
538
     */
539 11
    public function getAnonymousId()
540
    {
541 11
        return $this->anonymousId;
542
    }
543
544
    /**
545
     * @param string $anonymousId
546
     * @return string
547
     */
548 4
    public function setAnonymousId($anonymousId)
549
    {
550 4
        $this->anonymousId = $anonymousId;
551
552 4
        return $this;
553
    }
554
555
    /**
556
     * @return string
557
     */
558 77
    public function getCacheDir()
559
    {
560 77
        return $this->cacheDir;
561
    }
562
563
    /**
564
     * @param string $cacheDir
565
     * @return $this
566
     */
567
    public function setCacheDir($cacheDir)
568
    {
569
        $this->cacheDir = $cacheDir;
570
571
        return $this;
572
    }
573
574
    /**
575
     * @return string
576
     */
577 40
    public function getLogLevel()
578
    {
579 40
        return $this->logLevel;
580
    }
581
582
    /**
583
     * @param string $logLevel
584
     * @return $this
585
     */
586 1
    public function setLogLevel($logLevel)
587
    {
588 1
        $this->logLevel = $logLevel;
589
590 1
        return $this;
591
    }
592
593
    /**
594
     * @return mixed
595
     */
596 40
    public function getMessageFormatter()
597
    {
598 40
        return $this->messageFormatter;
599
    }
600
601
    /**
602
     * @param mixed $messageFormatter
603
     * @return $this
604
     */
605
    public function setMessageFormatter($messageFormatter)
606
    {
607
        $this->messageFormatter = $messageFormatter;
608
        return $this;
609
    }
610
611
    /**
612
     * @return CorrelationIdProvider|null
613
     */
614 73
    public function getCorrelationIdProvider()
615
    {
616 73
        if (!$this->isEnableCorrelationId()) {
617 34
            return null;
618
        }
619 39
        if (is_null($this->correlationIdProvider)) {
620 37
            $this->correlationIdProvider = DefaultCorrelationIdProvider::of($this->getProject());
621
        }
622 39
        return $this->correlationIdProvider;
623
    }
624
625
    /**
626
     * @param CorrelationIdProvider $correlationIdProvider
627
     * @return Config
628
     */
629 2
    public function setCorrelationIdProvider(CorrelationIdProvider $correlationIdProvider)
630
    {
631 2
        $this->correlationIdProvider = $correlationIdProvider;
632 2
        $this->setEnableCorrelationId(true);
633 2
        return $this;
634
    }
635
636
    /**
637
     * @return bool
638
     */
639 73
    public function isEnableCorrelationId()
640
    {
641 73
        return $this->enableCorrelationId;
642
    }
643
644
    /**
645
     * @param bool $enableCorrelationId
646
     * @return Config
647
     */
648 40
    public function setEnableCorrelationId($enableCorrelationId)
649
    {
650 40
        $this->enableCorrelationId = (bool)$enableCorrelationId;
651 40
        return $this;
652
    }
653
654
    /**
655
     * @return array
656
     */
657 58
    public function getClientOptions()
658
    {
659 58
        return $this->clientOptions;
660
    }
661
662
    /**
663
     * @param array $clientOptions
664
     * @return Config
665
     */
666
    public function setClientOptions(array $clientOptions)
667
    {
668
        $this->clientOptions = $clientOptions;
669
        return $this;
670
    }
671
}
672