Completed
Push — develop ( 5e8154...a89f38 )
by Jens
08:15
created

Config::setBatchPoolSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\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 ANONYMOUS_ID = 'anonymous_id';
69
    const GRANT_TYPE = 'grant_type';
70
71
    const GRANT_TYPE_CLIENT = 'client_credentials';
72
    const GRANT_TYPE_PASSWORD = 'password';
73
    const GRANT_TYPE_REFRESH = 'refresh_token';
74
    const GRANT_TYPE_ANONYMOUS = 'anonymous_token';
75
76
    /**
77
     * @var string
78
     */
79
    protected $clientSecret;
80
81
    /**
82
     * @var string
83
     */
84
    protected $clientId;
85
86
    /**
87
     * @var string
88
     */
89
    protected $project;
90
91
    /**
92
     * @var array
93
     */
94
    protected $scope = ['manage_project'];
95
96
    /**
97
     * @var string
98
     */
99
    protected $oauthUrl = 'https://auth.sphere.io';
100
101
    /**
102
     * @var string
103
     */
104
    protected $apiUrl = 'https://api.sphere.io';
105
106
    /**
107
     * @var int
108
     */
109
    protected $batchPoolSize = 25;
110
111
    protected $adapter;
112
113
    /**
114
     * @var bool
115
     */
116
    protected $throwExceptions = false;
117
118
    protected $acceptEncoding = 'gzip';
119
120
    protected $grantType = 'client_credentials';
121
122
    /**
123
     * @var string
124
     */
125
    protected $username;
126
127
    /**
128
     * @var string
129
     */
130
    protected $password;
131
132
    /**
133
     * @var string
134
     */
135
    protected $refreshToken;
136
137
    /**
138
     * @var string
139
     */
140
    protected $anonymousId;
141
142
    /**
143
     * @var string
144 60
     */
145
    protected $cacheDir;
146 60
147 60
    /**
148
     * @param array $configValues
149 60
     * @return static
150 58
     */
151 58
    public static function fromArray(array $configValues)
152 1
    {
153
        $config = static::of();
154 57
        array_walk(
155 60
            $configValues,
156
            function ($value, $key) use ($config) {
157
                $functionName = 'set' . $config->camelize($key);
158 59
                if (!is_callable([$config, $functionName])) {
159
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
160
                }
161 58
                $config->$functionName($value);
162
            }
163 58
        );
164
165 58
        return $config;
166
    }
167 58
168
    protected function camelize($scored)
169 58
    {
170 58
        return lcfirst(
171
            implode(
172
                '',
173
                array_map(
174
                    'ucfirst',
175
                    array_map(
176
                        'strtolower',
177
                        explode('_', $scored)
178
                    )
179
                )
180 47
            )
181
        );
182 47
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getClientSecret()
188
    {
189 48
        return $this->clientSecret;
190
    }
191 48
192
    /**
193 48
     * @param string $clientSecret
194
     * @return $this
195
     */
196
    public function setClientSecret($clientSecret)
197
    {
198
        $this->clientSecret = $clientSecret;
199 51
200
        return $this;
201 51
    }
202
203
    /**
204
     * @return string
205
     */
206
    public function getClientId()
207
    {
208 48
        return $this->clientId;
209
    }
210 48
211
    /**
212 48
     * @param string $clientId
213
     * @return $this
214
     */
215
    public function setClientId($clientId)
216
    {
217
        $this->clientId = $clientId;
218 362
219
        return $this;
220 362
    }
221
222
    /**
223
     * @return string
224
     */
225
    public function getProject()
226
    {
227 56
        return $this->project;
228
    }
229 56
230
    /**
231 56
     * @param string $project
232
     * @return $this
233
     */
234
    public function setProject($project)
235
    {
236
        $this->project = $project;
237 327
238
        return $this;
239 327
    }
240 327
241
    /**
242 327
     * @return string
243 327
     */
244 327
    public function getScope()
245 326
    {
246 325
        $scope = $this->scope;
247
        $project = $this->getProject();
248 326
249
        $permissions = [];
250 327
        foreach ($scope as $key => $value) {
251
            if (is_numeric($key)) { // scope defined as string e.g. scope:project_key
252
                if (strpos($value, ':') === false) { // scope without project key
253 327
                    $value = $value . ':' . $project;
254
                }
255 327
                $permissions[] = $value;
256
            } else { // scope defined as array
257
                $permissions[] = $key . ':' . $value;
258
            }
259
        }
260
        $scope = implode(' ', $permissions);
261
262 16
        return $scope;
263
    }
264 16
265 11
    /**
266
     * @param string $scope
267 16
     * @return $this
268
     */
269 16
    public function setScope($scope)
270
    {
271
        if (!is_array($scope)) {
272
            $scope = explode(' ', $scope);
273
        }
274
        $this->scope = $scope;
275 16
276
        return $this;
277 16
    }
278 16
279 13
    /**
280 4
     * @return string
281
     */
282 12
    public function getOauthUrl()
283
    {
284
        switch ($this->getGrantType()) {
285
            case static::GRANT_TYPE_ANONYMOUS:
286
                return $this->oauthUrl . '/oauth/' . $this->getProject() . '/anonymous/token';
287
            case static::GRANT_TYPE_PASSWORD:
288
            case static::GRANT_TYPE_REFRESH:
289
                return $this->oauthUrl . '/oauth/' . $this->getProject() . '/customers/token';
290 48
            default:
291
                return $this->oauthUrl . '/oauth/token';
292 48
        }
293
    }
294 48
295
    /**
296
     * @param string $oauthUrl
297
     * @return $this
298
     */
299
    public function setOauthUrl($oauthUrl)
300 34
    {
301
        $this->oauthUrl = $oauthUrl;
302 34
303
        return $this;
304
    }
305
306
    /**
307
     * @return string
308
     */
309 48
    public function getApiUrl()
310
    {
311 48
        return $this->apiUrl;
312
    }
313 48
314
    /**
315
     * @param string $apiUrl
316
     * @return $this
317
     */
318
    public function setApiUrl($apiUrl)
319 47
    {
320
        $this->apiUrl = $apiUrl;
321 47
322 4
        return $this;
323
    }
324
325 43
    /**
326
     * @return bool
327
     */
328
    public function check()
329 43
    {
330
        if (is_null($this->getClientId())) {
331
            throw new InvalidArgumentException(Message::NO_CLIENT_ID);
332
        }
333 43
334
        if (is_null($this->getClientSecret())) {
335
            throw new InvalidArgumentException(Message::NO_CLIENT_SECRET);
336
        }
337
338
        if (is_null($this->getProject())) {
339 1
            throw new InvalidArgumentException(Message::NO_PROJECT_ID);
340
        }
341 1
342
        return true;
343
    }
344
345
    /**
346
     * @return int
347
     */
348 1
    public function getBatchPoolSize()
349
    {
350 1
        return $this->batchPoolSize;
351
    }
352 1
353
    /**
354
     * @param int $batchPoolSize
355
     * @return $this
356
     */
357
    public function setBatchPoolSize($batchPoolSize)
358 37
    {
359
        $this->batchPoolSize = $batchPoolSize;
360 37
361
        return $this;
362
    }
363
364
    /**
365
     * @return string
366
     */
367
    public function getAdapter()
368
    {
369
        return $this->adapter;
370
    }
371
372
    /**
373
     * @param string $adapter
374
     * @return $this
375
     */
376
    public function setAdapter($adapter)
377 52
    {
378
        $this->adapter = $adapter;
379 52
380
        return $this;
381
    }
382
383
    /**
384
     * @return bool
385
     */
386 9
    public function getThrowExceptions()
387
    {
388 9
        return $this->throwExceptions;
389
    }
390 9
391
    /**
392
     * @param bool $throwExceptions
393
     * @return $this
394
     */
395
    public function setThrowExceptions($throwExceptions)
396 37
    {
397
        $this->throwExceptions = $throwExceptions;
398 37
399
        return $this;
400
    }
401
402
    /**
403
     * @return string
404
     */
405
    public function getAcceptEncoding()
406
    {
407
        return $this->acceptEncoding;
408
    }
409
410
    /**
411
     * @param string $acceptEncoding
412
     * @return $this
413
     */
414
    public function setAcceptEncoding($acceptEncoding)
415 60
    {
416
        $this->acceptEncoding = $acceptEncoding;
417 60
418
        return $this;
419
    }
420
421
    /**
422
     * @return static
423 322
     */
424
    public static function of()
425 322
    {
426
        return new static();
427
    }
428
429
    /**
430
     * @return string
431
     */
432 4
    public function getGrantType()
433
    {
434 4
        return $this->grantType;
435
    }
436 4
437
    /**
438
     * @param string $grantType
439
     * @return $this
440
     */
441
    public function setGrantType($grantType)
442 4
    {
443
        $this->grantType = $grantType;
444 4
445
        return $this;
446
    }
447
448
    /**
449
     * @return string
450
     */
451 4
    public function getUsername()
452
    {
453 4
        return $this->username;
454
    }
455 4
456
    /**
457
     * @param string $username
458
     * @return $this
459
     */
460
    public function setUsername($username)
461 4
    {
462
        $this->username = $username;
463 4
464
        return $this;
465
    }
466
467
    /**
468
     * @return string
469
     */
470 4
    public function getPassword()
471
    {
472 4
        return $this->password;
473
    }
474 4
475
    /**
476
     * @param string $password
477
     * @return $this
478
     */
479
    public function setPassword($password)
480 4
    {
481
        $this->password = $password;
482 4
483
        return $this;
484
    }
485
486
    /**
487
     * @return string
488
     */
489 4
    public function getRefreshToken()
490
    {
491 4
        return $this->refreshToken;
492
    }
493 4
494
    /**
495
     * @param string $refreshToken
496
     * @return $this
497
     */
498
    public function setRefreshToken($refreshToken)
499 40
    {
500
        $this->refreshToken = $refreshToken;
501 40
502
        return $this;
503
    }
504
505
    /**
506
     * @return string
507
     */
508
    public function getAnonymousId()
509
    {
510
        return $this->anonymousId;
511
    }
512
513
    /**
514
     * @param string $anonymousId
515
     * @return string
516
     */
517
    public function setAnonymousId($anonymousId)
518
    {
519
        $this->anonymousId = $anonymousId;
520
521
        return $this;
522
    }
523
524
    /**
525
     * @return string
526
     */
527
    public function getCacheDir()
528
    {
529
        return $this->cacheDir;
530
    }
531
532
    /**
533
     * @param string $cacheDir
534
     * @return $this
535
     */
536
    public function setCacheDir($cacheDir)
537
    {
538
        $this->cacheDir = $cacheDir;
539
540
        return $this;
541
    }
542
}
543