Completed
Push — develop ( 10a3f8...fe23c8 )
by Jens
10:24
created

Config::setRefreshToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
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
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 316
    protected $throwExceptions = false;
115
116 316
    protected $acceptEncoding = 'gzip';
117 316
118
    protected $grantType = 'client_credentials';
119 316
120 314
    /**
121 314
     * @var string
122 1
     */
123
    protected $username;
124 313
125 316
    /**
126
     * @var string
127
     */
128 315
    protected $password;
129
130
    /**
131 314
     * @var string
132
     */
133 314
    protected $refreshToken;
134
135 314
    /**
136
     * @param array $configValues
137 314
     * @return static
138
     */
139 314
    public static function fromArray(array $configValues)
140 314
    {
141
        $config = static::of();
142
        array_walk(
143
            $configValues,
144
            function ($value, $key) use ($config) {
145
                $functionName = 'set' . $config->camelize($key);
146
                if (!is_callable([$config, $functionName])) {
147
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
148
                }
149
                $config->$functionName($value);
150 302
            }
151
        );
152 302
153
        return $config;
154
    }
155
156
    protected function camelize($scored)
157
    {
158
        return lcfirst(
159 304
            implode(
160
                '',
161 304
                array_map(
162
                    'ucfirst',
163 304
                    array_map(
164
                        'strtolower',
165
                        explode('_', $scored)
166
                    )
167
                )
168
            )
169 306
        );
170
    }
171 306
172
    /**
173
     * @return string
174
     */
175
    public function getClientSecret()
176
    {
177
        return $this->clientSecret;
178 304
    }
179
180 304
    /**
181
     * @param string $clientSecret
182 304
     * @return $this
183
     */
184
    public function setClientSecret($clientSecret)
185
    {
186
        $this->clientSecret = $clientSecret;
187
188 311
        return $this;
189
    }
190 311
191
    /**
192
     * @return string
193
     */
194
    public function getClientId()
195
    {
196
        return $this->clientId;
197 312
    }
198
199 312
    /**
200
     * @param string $clientId
201 312
     * @return $this
202
     */
203
    public function setClientId($clientId)
204
    {
205
        $this->clientId = $clientId;
206
207 279
        return $this;
208
    }
209 279
210 279
    /**
211
     * @return string
212 279
     */
213 279
    public function getProject()
214 279
    {
215 278
        return $this->project;
216 277
    }
217
218 278
    /**
219
     * @param string $project
220 279
     * @return $this
221
     */
222
    public function setProject($project)
223 279
    {
224
        $this->project = $project;
225 279
226
        return $this;
227
    }
228
229
    /**
230
     * @return string
231
     */
232 12
    public function getScope()
233
    {
234 12
        $scope = $this->scope;
235 7
        $project = $this->getProject();
236
237 12
        $permissions = [];
238
        foreach ($scope as $key => $value) {
239 12
            if (is_numeric($key)) { // scope defined as string e.g. scope:project_key
240
                if (strpos($value, ':') === false) { // scope without project key
241
                    $value = $value . ':' . $project;
242
                }
243
                $permissions[] = $value;
244
            } else { // scope defined as array
245 274
                $permissions[] = $key . ':' . $value;
246
            }
247 274
        }
248
        $scope = implode(' ', $permissions);
249
250
        return $scope;
251
    }
252
253
    /**
254 304
     * @param string $scope
255
     * @return $this
256 304
     */
257
    public function setScope($scope)
258 304
    {
259
        if (!is_array($scope)) {
260
            $scope = explode(' ', $scope);
261
        }
262
        $this->scope = $scope;
263
264 293
        return $this;
265
    }
266 293
267
    /**
268
     * @return string
269
     */
270
    public function getOauthUrl()
271
    {
272
        switch ($this->getGrantType()) {
273 304
            case static::GRANT_TYPE_PASSWORD:
274
            case static::GRANT_TYPE_REFRESH:
275 304
                return $this->oauthUrl . '/oauth/' . $this->getProject() . '/customers/token';
276
            default:
277 304
                return $this->oauthUrl . '/oauth/token';
278
        }
279
    }
280
281
    /**
282
     * @param string $oauthUrl
283 303
     * @return $this
284
     */
285 303
    public function setOauthUrl($oauthUrl)
286 4
    {
287
        $this->oauthUrl = $oauthUrl;
288
289 299
        return $this;
290
    }
291
292
    /**
293 299
     * @return string
294
     */
295
    public function getApiUrl()
296
    {
297 299
        return $this->apiUrl;
298
    }
299
300
    /**
301
     * @param string $apiUrl
302
     * @return $this
303 1
     */
304
    public function setApiUrl($apiUrl)
305 1
    {
306
        $this->apiUrl = $apiUrl;
307
308
        return $this;
309
    }
310
311
    /**
312 1
     * @return bool
313
     */
314 1
    public function check()
315
    {
316 1
        if (is_null($this->getClientId())) {
317
            throw new InvalidArgumentException(Message::NO_CLIENT_ID);
318
        }
319
320
        if (is_null($this->getClientSecret())) {
321
            throw new InvalidArgumentException(Message::NO_CLIENT_SECRET);
322 296
        }
323
324 296
        if (is_null($this->getProject())) {
325
            throw new InvalidArgumentException(Message::NO_PROJECT_ID);
326
        }
327
328
        return true;
329
    }
330
331
    /**
332
     * @return int
333
     */
334
    public function getBatchPoolSize()
335
    {
336
        return $this->batchPoolSize;
337
    }
338
339
    /**
340
     * @param int $batchPoolSize
341 44
     * @return $this
342
     */
343 44
    public function setBatchPoolSize($batchPoolSize)
344
    {
345
        $this->batchPoolSize = $batchPoolSize;
346
347
        return $this;
348
    }
349
350 9
    /**
351
     * @return string
352 9
     */
353
    public function getAdapter()
354 9
    {
355
        return $this->adapter;
356
    }
357
358
    /**
359
     * @param string $adapter
360 296
     * @return $this
361
     */
362 296
    public function setAdapter($adapter)
363
    {
364
        $this->adapter = $adapter;
365
366
        return $this;
367
    }
368
369
    /**
370
     * @return bool
371
     */
372
    public function getThrowExceptions()
373
    {
374
        return $this->throwExceptions;
375
    }
376
377
    /**
378
     * @param bool $throwExceptions
379 316
     * @return $this
380
     */
381 316
    public function setThrowExceptions($throwExceptions)
382
    {
383
        $this->throwExceptions = $throwExceptions;
384
385
        return $this;
386
    }
387
388
    /**
389
     * @return string
390
     */
391
    public function getAcceptEncoding()
392
    {
393
        return $this->acceptEncoding;
394
    }
395
396
    /**
397
     * @param string $acceptEncoding
398
     * @return $this
399
     */
400
    public function setAcceptEncoding($acceptEncoding)
401
    {
402
        $this->acceptEncoding = $acceptEncoding;
403
404
        return $this;
405
    }
406
407
    /**
408
     * @return static
409
     */
410
    public static function of()
411
    {
412
        return new static();
413
    }
414
415
    /**
416
     * @return string
417
     */
418
    public function getGrantType()
419
    {
420
        return $this->grantType;
421
    }
422
423
    /**
424
     * @param string $grantType
425
     * @return $this
426
     */
427
    public function setGrantType($grantType)
428
    {
429
        $this->grantType = $grantType;
430
431
        return $this;
432
    }
433
434
    /**
435
     * @return string
436
     */
437
    public function getUsername()
438
    {
439
        return $this->username;
440
    }
441
442
    /**
443
     * @param string $username
444
     * @return $this
445
     */
446
    public function setUsername($username)
447
    {
448
        $this->username = $username;
449
450
        return $this;
451
    }
452
453
    /**
454
     * @return string
455
     */
456
    public function getPassword()
457
    {
458
        return $this->password;
459
    }
460
461
    /**
462
     * @param string $password
463
     * @return $this
464
     */
465
    public function setPassword($password)
466
    {
467
        $this->password = $password;
468
469
        return $this;
470
    }
471
472
    /**
473
     * @return string
474
     */
475
    public function getRefreshToken()
476
    {
477
        return $this->refreshToken;
478
    }
479
480
    /**
481
     * @param string $refreshToken
482
     * @return $this
483
     */
484
    public function setRefreshToken($refreshToken)
485
    {
486
        $this->refreshToken = $refreshToken;
487
488
        return $this;
489
    }
490
}
491