Completed
Push — develop ( 676144...1f3cb9 )
by Jens
29:47 queued 16:36
created

Config::setApiUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 1
cts 1
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
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 string
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 297
    protected $acceptEncoding = 'gzip';
109
110 297
    /**
111 297
     * @param array $configValues
112
     * @return static
113 297
     */
114 295
    public static function fromArray(array $configValues)
115 295
    {
116 1
        $config = static::of();
117
        array_walk(
118 294
            $configValues,
119 297
            function ($value, $key) use ($config) {
120
                $functionName = 'set' . $config->camelize($key);
121
                if (!is_callable([$config, $functionName])) {
122 296
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
123
                }
124
                $config->$functionName($value);
125 295
            }
126
        );
127 295
128
        return $config;
129 295
    }
130
131 295
    protected function camelize($scored)
132
    {
133 295
        return lcfirst(
134 295
            implode(
135
                '',
136
                array_map(
137
                    'ucfirst',
138
                    array_map(
139
                        'strtolower',
140
                        explode('_', $scored)
141
                    )
142
                )
143
            )
144 292
        );
145
    }
146 292
147
    /**
148
     * @return string
149
     */
150
    public function getClientSecret()
151
    {
152
        return $this->clientSecret;
153 293
    }
154
155 293
    /**
156
     * @param string $clientSecret
157 293
     * @return $this
158
     */
159
    public function setClientSecret($clientSecret)
160
    {
161
        $this->clientSecret = $clientSecret;
162
163 296
        return $this;
164
    }
165 296
166
    /**
167
     * @return string
168
     */
169
    public function getClientId()
170
    {
171
        return $this->clientId;
172 293
    }
173
174 293
    /**
175
     * @param string $clientId
176 293
     * @return $this
177
     */
178
    public function setClientId($clientId)
179
    {
180
        $this->clientId = $clientId;
181
182 292
        return $this;
183
    }
184 292
185
    /**
186
     * @return string
187
     */
188
    public function getProject()
189
    {
190
        return $this->project;
191 293
    }
192
193 293
    /**
194
     * @param string $project
195 293
     * @return $this
196
     */
197
    public function setProject($project)
198
    {
199
        $this->project = $project;
200
201 264
        return $this;
202
    }
203 264
204
    /**
205
     * @return string
206
     */
207
    public function getScope()
208
    {
209
        return $this->scope;
210 293
    }
211
212 293
    /**
213
     * @param string $scope
214 293
     * @return $this
215
     */
216
    public function setScope($scope)
217
    {
218
        $this->scope = $scope;
219
        return $this;
220 285
    }
221
222 285
    /**
223
     * @return string
224
     */
225
    public function getOauthUrl()
226
    {
227
        return $this->oauthUrl;
228 293
    }
229
230 293
    /**
231
     * @param string $oauthUrl
232 293
     * @return $this
233
     */
234
    public function setOauthUrl($oauthUrl)
235
    {
236
        $this->oauthUrl = $oauthUrl;
237
238 293
        return $this;
239
    }
240 293
241 4
    /**
242
     * @return string
243
     */
244 289
    public function getApiUrl()
245
    {
246
        return $this->apiUrl;
247
    }
248 289
249
    /**
250
     * @param string $apiUrl
251
     * @return $this
252 289
     */
253
    public function setApiUrl($apiUrl)
254
    {
255
        $this->apiUrl = $apiUrl;
256
257
        return $this;
258 1
    }
259
260 1
    /**
261
     * @return bool
262
     */
263
    public function check()
264
    {
265
        if (is_null($this->getClientId())) {
266 1
            throw new InvalidArgumentException(Message::NO_CLIENT_ID);
267
        }
268 1
269
        if (is_null($this->getClientSecret())) {
270 1
            throw new InvalidArgumentException(Message::NO_CLIENT_SECRET);
271
        }
272
273
        if (is_null($this->getProject())) {
274
            throw new InvalidArgumentException(Message::NO_PROJECT_ID);
275
        }
276 286
277
        return true;
278 286
    }
279
280
    /**
281
     * @return int
282
     */
283
    public function getBatchPoolSize()
284
    {
285
        return $this->batchPoolSize;
286
    }
287
288
    /**
289
     * @param int $batchPoolSize
290
     * @return $this
291
     */
292
    public function setBatchPoolSize($batchPoolSize)
293
    {
294 37
        $this->batchPoolSize = $batchPoolSize;
295
296 37
        return $this;
297
    }
298
299
    /**
300
     * @return string
301
     */
302 9
    public function getAdapter()
303
    {
304 9
        return $this->adapter;
305
    }
306 9
307
    /**
308
     * @param string $adapter
309
     * @return $this
310
     */
311
    public function setAdapter($adapter)
312 286
    {
313
        $this->adapter = $adapter;
314 286
315
        return $this;
316
    }
317
318
    /**
319
     * @return bool
320
     */
321
    public function getThrowExceptions()
322
    {
323
        return $this->throwExceptions;
324
    }
325
326
    /**
327
     * @param bool $throwExceptions
328
     * @return $this
329
     */
330 297
    public function setThrowExceptions($throwExceptions)
331
    {
332 297
        $this->throwExceptions = $throwExceptions;
333
334
        return $this;
335
    }
336
337
    /**
338
     * @return string
339
     */
340
    public function getAcceptEncoding()
341
    {
342
        return $this->acceptEncoding;
343
    }
344
345
    /**
346
     * @param string $acceptEncoding
347
     * @return $this
348
     */
349
    public function setAcceptEncoding($acceptEncoding)
350
    {
351
        $this->acceptEncoding = $acceptEncoding;
352
353
        return $this;
354
    }
355
356
    /**
357
     * @return static
358
     */
359
    public static function of()
360
    {
361
        return new static();
362
    }
363
}
364