Completed
Push — master ( 2d0c9c...1268a4 )
by Jens
10:44
created

Config::getScope()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 13
nc 4
nop 0
crap 4
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
66
    /**
67
     * @var string
68
     */
69
    protected $clientSecret;
70
71
    /**
72
     * @var string
73
     */
74
    protected $clientId;
75
76
    /**
77
     * @var string
78
     */
79
    protected $project;
80
81
    /**
82
     * @var array
83
     */
84
    protected $scope = ['manage_project'];
85
86
    /**
87
     * @var string
88
     */
89
    protected $oauthUrl = 'https://auth.sphere.io/oauth/token';
90
91
    /**
92
     * @var string
93
     */
94
    protected $apiUrl = 'https://api.sphere.io';
95
96
    /**
97
     * @var int
98
     */
99
    protected $batchPoolSize = 25;
100
101
    protected $adapter;
102
103
    /**
104
     * @var bool
105
     */
106
    protected $throwExceptions = false;
107
108
    protected $acceptEncoding = 'gzip';
109
110
    /**
111
     * @param array $configValues
112
     * @return static
113
     */
114 316
    public static function fromArray(array $configValues)
115
    {
116 316
        $config = static::of();
117 316
        array_walk(
118
            $configValues,
119 316
            function ($value, $key) use ($config) {
120 314
                $functionName = 'set' . $config->camelize($key);
121 314
                if (!is_callable([$config, $functionName])) {
122 1
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
123
                }
124 313
                $config->$functionName($value);
125 316
            }
126
        );
127
128 315
        return $config;
129
    }
130
131 314
    protected function camelize($scored)
132
    {
133 314
        return lcfirst(
134
            implode(
135 314
                '',
136
                array_map(
137 314
                    'ucfirst',
138
                    array_map(
139 314
                        'strtolower',
140 314
                        explode('_', $scored)
141
                    )
142
                )
143
            )
144
        );
145
    }
146
147
    /**
148
     * @return string
149
     */
150 302
    public function getClientSecret()
151
    {
152 302
        return $this->clientSecret;
153
    }
154
155
    /**
156
     * @param string $clientSecret
157
     * @return $this
158
     */
159 304
    public function setClientSecret($clientSecret)
160
    {
161 304
        $this->clientSecret = $clientSecret;
162
163 304
        return $this;
164
    }
165
166
    /**
167
     * @return string
168
     */
169 306
    public function getClientId()
170
    {
171 306
        return $this->clientId;
172
    }
173
174
    /**
175
     * @param string $clientId
176
     * @return $this
177
     */
178 304
    public function setClientId($clientId)
179
    {
180 304
        $this->clientId = $clientId;
181
182 304
        return $this;
183
    }
184
185
    /**
186
     * @return string
187
     */
188 311
    public function getProject()
189
    {
190 311
        return $this->project;
191
    }
192
193
    /**
194
     * @param string $project
195
     * @return $this
196
     */
197 312
    public function setProject($project)
198
    {
199 312
        $this->project = $project;
200
201 312
        return $this;
202
    }
203
204
    /**
205
     * @return string
206
     */
207 279
    public function getScope()
208
    {
209 279
        $scope = $this->scope;
210 279
        $project = $this->getProject();
211
212 279
        $permissions = [];
213 279
        foreach ($scope as $key => $value) {
214 279
            if (is_numeric($key)) { // scope defined as string e.g. scope:project_key
215 278
                if (strpos($value, ':') === false) { // scope without project key
216 277
                    $value = $value . ':' . $project;
217
                }
218 278
                $permissions[] = $value;
219
            } else { // scope defined as array
220 279
                $permissions[] = $key . ':' . $value;
221
            }
222
        }
223 279
        $scope = implode(' ', $permissions);
224
225 279
        return $scope;
226
    }
227
228
    /**
229
     * @param string $scope
230
     * @return $this
231
     */
232 12
    public function setScope($scope)
233
    {
234 12
        if (!is_array($scope)) {
235 7
            $scope = explode(' ', $scope);
236
        }
237 12
        $this->scope = $scope;
238
239 12
        return $this;
240
    }
241
242
    /**
243
     * @return string
244
     */
245 274
    public function getOauthUrl()
246
    {
247 274
        return $this->oauthUrl;
248
    }
249
250
    /**
251
     * @param string $oauthUrl
252
     * @return $this
253
     */
254 304
    public function setOauthUrl($oauthUrl)
255
    {
256 304
        $this->oauthUrl = $oauthUrl;
257
258 304
        return $this;
259
    }
260
261
    /**
262
     * @return string
263
     */
264 293
    public function getApiUrl()
265
    {
266 293
        return $this->apiUrl;
267
    }
268
269
    /**
270
     * @param string $apiUrl
271
     * @return $this
272
     */
273 304
    public function setApiUrl($apiUrl)
274
    {
275 304
        $this->apiUrl = $apiUrl;
276
277 304
        return $this;
278
    }
279
280
    /**
281
     * @return bool
282
     */
283 303
    public function check()
284
    {
285 303
        if (is_null($this->getClientId())) {
286 4
            throw new InvalidArgumentException(Message::NO_CLIENT_ID);
287
        }
288
289 299
        if (is_null($this->getClientSecret())) {
290
            throw new InvalidArgumentException(Message::NO_CLIENT_SECRET);
291
        }
292
293 299
        if (is_null($this->getProject())) {
294
            throw new InvalidArgumentException(Message::NO_PROJECT_ID);
295
        }
296
297 299
        return true;
298
    }
299
300
    /**
301
     * @return int
302
     */
303 1
    public function getBatchPoolSize()
304
    {
305 1
        return $this->batchPoolSize;
306
    }
307
308
    /**
309
     * @param int $batchPoolSize
310
     * @return $this
311
     */
312 1
    public function setBatchPoolSize($batchPoolSize)
313
    {
314 1
        $this->batchPoolSize = $batchPoolSize;
315
316 1
        return $this;
317
    }
318
319
    /**
320
     * @return string
321
     */
322 296
    public function getAdapter()
323
    {
324 296
        return $this->adapter;
325
    }
326
327
    /**
328
     * @param string $adapter
329
     * @return $this
330
     */
331
    public function setAdapter($adapter)
332
    {
333
        $this->adapter = $adapter;
334
335
        return $this;
336
    }
337
338
    /**
339
     * @return bool
340
     */
341 44
    public function getThrowExceptions()
342
    {
343 44
        return $this->throwExceptions;
344
    }
345
346
    /**
347
     * @param bool $throwExceptions
348
     * @return $this
349
     */
350 9
    public function setThrowExceptions($throwExceptions)
351
    {
352 9
        $this->throwExceptions = $throwExceptions;
353
354 9
        return $this;
355
    }
356
357
    /**
358
     * @return string
359
     */
360 296
    public function getAcceptEncoding()
361
    {
362 296
        return $this->acceptEncoding;
363
    }
364
365
    /**
366
     * @param string $acceptEncoding
367
     * @return $this
368
     */
369
    public function setAcceptEncoding($acceptEncoding)
370
    {
371
        $this->acceptEncoding = $acceptEncoding;
372
373
        return $this;
374
    }
375
376
    /**
377
     * @return static
378
     */
379 316
    public static function of()
380
    {
381 316
        return new static();
382
    }
383
}
384