Completed
Push — develop ( f7f657...c57f2e )
by
unknown
07:28
created

Config   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 26
c 2
b 1
f 1
lcom 1
cbo 2
dl 0
loc 280
ccs 72
cts 80
cp 0.9
rs 10

22 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 16 2
A camelize() 0 15 1
A getClientSecret() 0 4 1
A setClientSecret() 0 6 1
A getClientId() 0 4 1
A setClientId() 0 6 1
A getProject() 0 4 1
A setProject() 0 6 1
A getOauthUrl() 0 4 1
A setOauthUrl() 0 6 1
A getApiUrl() 0 4 1
A setApiUrl() 0 6 1
A check() 0 16 4
A getBatchPoolSize() 0 4 1
A setBatchPoolSize() 0 6 1
A getAdapter() 0 4 1
A setAdapter() 0 6 1
A getThrowExceptions() 0 4 1
A setThrowExceptions() 0 6 1
A getAcceptEncoding() 0 4 1
A setAcceptEncoding() 0 6 1
A of() 0 4 1
1
<?php
2
/**
3
 * @author @ct-jensschulze <[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 PROJECT = 'project';
63
    const API_URL = 'api_url';
64
65
    /**
66
     * @var string
67
     */
68
    protected $clientSecret;
69
70
    /**
71
     * @var string
72
     */
73
    protected $clientId;
74
75
    /**
76
     * @var string
77
     */
78
    protected $project;
79
80
    /**
81
     * @var string
82
     */
83
    protected $oauthUrl = 'https://auth.sphere.io/oauth/token';
84
85
    /**
86
     * @var string
87
     */
88
    protected $apiUrl = 'https://api.sphere.io';
89
90
    /**
91
     * @var int
92
     */
93
    protected $batchPoolSize = 25;
94
95
    protected $adapter;
96
97
    /**
98
     * @var bool
99
     */
100
    protected $throwExceptions = false;
101
102
    protected $acceptEncoding;
103
104
    /**
105
     * @param array $configValues
106
     * @return static
107
     */
108 35
    public static function fromArray(array $configValues)
109
    {
110 35
        $config = static::of();
111 35
        array_walk(
112 35
            $configValues,
113 33
            function ($value, $key) use ($config) {
114 33
                $functionName = 'set' . $config->camelize($key);
115 33
                if (!is_callable([$config, $functionName])) {
116 1
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
117
                }
118 32
                $config->$functionName($value);
119 32
            }
120 35
        );
121
122 34
        return $config;
123
    }
124
125 33
    protected function camelize($scored)
126
    {
127 33
        return lcfirst(
128 33
            implode(
129 33
                '',
130 33
                array_map(
131 33
                    'ucfirst',
132 33
                    array_map(
133 33
                        'strtolower',
134 33
                        explode('_', $scored)
135 33
                    )
136 33
                )
137 33
            )
138 33
        );
139
    }
140
141
    /**
142
     * @return string
143
     */
144 30
    public function getClientSecret()
145
    {
146 30
        return $this->clientSecret;
147
    }
148
149
    /**
150
     * @param string $clientSecret
151
     * @return $this
152
     */
153 31
    public function setClientSecret($clientSecret)
154
    {
155 31
        $this->clientSecret = $clientSecret;
156
157 31
        return $this;
158
    }
159
160
    /**
161
     * @return string
162
     */
163 34
    public function getClientId()
164
    {
165 34
        return $this->clientId;
166
    }
167
168
    /**
169
     * @param string $clientId
170
     * @return $this
171
     */
172 31
    public function setClientId($clientId)
173
    {
174 31
        $this->clientId = $clientId;
175
176 31
        return $this;
177
    }
178
179
    /**
180
     * @return string
181
     */
182 30
    public function getProject()
183
    {
184 30
        return $this->project;
185
    }
186
187
    /**
188
     * @param string $project
189
     * @return $this
190
     */
191 31
    public function setProject($project)
192
    {
193 31
        $this->project = $project;
194
195 31
        return $this;
196
    }
197
198
    /**
199
     * @return string
200
     */
201 7
    public function getOauthUrl()
202
    {
203 7
        return $this->oauthUrl;
204
    }
205
206
    /**
207
     * @param string $oauthUrl
208
     * @return $this
209
     */
210 31
    public function setOauthUrl($oauthUrl)
211
    {
212 31
        $this->oauthUrl = $oauthUrl;
213
214 31
        return $this;
215
    }
216
217
    /**
218
     * @return string
219
     */
220 23
    public function getApiUrl()
221
    {
222 23
        return $this->apiUrl;
223
    }
224
225
    /**
226
     * @param string $apiUrl
227
     */
228 31
    public function setApiUrl($apiUrl)
229
    {
230 31
        $this->apiUrl = $apiUrl;
231
232 31
        return $this;
233
    }
234
235
    /**
236
     * @return bool
237
     */
238 31
    public function check()
239
    {
240 31
        if (is_null($this->getClientId())) {
241 4
            throw new InvalidArgumentException(Message::NO_CLIENT_ID);
242
        }
243
244 27
        if (is_null($this->getClientSecret())) {
245
            throw new InvalidArgumentException(Message::NO_CLIENT_SECRET);
246
        }
247
248 27
        if (is_null($this->getProject())) {
249
            throw new InvalidArgumentException(Message::NO_PROJECT_ID);
250
        }
251
252 27
        return true;
253
    }
254
255
    /**
256
     * @return int
257
     */
258 1
    public function getBatchPoolSize()
259
    {
260 1
        return $this->batchPoolSize;
261
    }
262
263
    /**
264
     * @param int $batchPoolSize
265
     */
266 1
    public function setBatchPoolSize($batchPoolSize)
267
    {
268 1
        $this->batchPoolSize = $batchPoolSize;
269
270 1
        return $this;
271
    }
272
273
    /**
274
     * @return string
275
     */
276 24
    public function getAdapter()
277
    {
278 24
        return $this->adapter;
279
    }
280
281
    /**
282
     * @param string $adapter
283
     */
284
    public function setAdapter($adapter)
285
    {
286
        $this->adapter = $adapter;
287
288
        return $this;
289
    }
290
291
    /**
292
     * @return bool
293
     */
294 12
    public function getThrowExceptions()
295
    {
296 12
        return $this->throwExceptions;
297
    }
298
299
    /**
300
     * @param bool $throwExceptions
301
     */
302 9
    public function setThrowExceptions($throwExceptions)
303
    {
304 9
        $this->throwExceptions = $throwExceptions;
305
306 9
        return $this;
307
    }
308
309
    /**
310
     * @return string
311
     */
312 24
    public function getAcceptEncoding()
313
    {
314 24
        return $this->acceptEncoding;
315
    }
316
317
    /**
318
     * @param string $acceptEncoding
319
     */
320
    public function setAcceptEncoding($acceptEncoding)
321
    {
322
        $this->acceptEncoding = $acceptEncoding;
323
324
        return $this;
325
    }
326
327
    /**
328
     * @return static
329
     */
330 35
    public static function of()
331
    {
332 35
        return new static();
333
    }
334
}
335