Completed
Push — develop ( 445b2f...03e8c5 )
by Jens
14:38
created

Config::setMessageFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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