Completed
Push — master ( 2ac606...daf487 )
by Jonathan
08:49
created

AbstractReportingCloud::getTimeout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * Official wrapper (authored by Text Control GmbH, publisher of ReportingCloud) to access ReportingCloud in PHP.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2016 Text Control GmbH
12
 */
13
namespace TxTextControl\ReportingCloud;
14
15
use GuzzleHttp\Client;
16
use GuzzleHttp\RequestOptions;
17
use TxTextControl\ReportingCloud\Exception\RuntimeException;
18
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
19
20
/**
21
 * Abstract ReportingCloud
22
 *
23
 * @package TxTextControl\ReportingCloud
24
 * @author  Jonathan Maron (@JonathanMaron)
25
 */
26
abstract class AbstractReportingCloud
27
{
28
    /**
29
     * Default date/time format of backend is 'ISO 8601'
30
     *
31
     * Note, last letter is 'P' and not 'O':
32
     *
33
     * O - Difference to Greenwich time (GMT) in hours (e.g. +0200)
34
     * P - Difference to Greenwich time (GMT) with colon between hours and minutes (e.g. +02:00)
35
     *
36
     * Backend uses the 'P' variant
37
     *
38
     * @const DEFAULT_DATE_FORMAT
39
     */
40
    const DEFAULT_DATE_FORMAT = 'Y-m-d\TH:i:sP';
41
42
    /**
43
     * Default time zone of backend
44
     *
45
     * @const DEFAULT_TIME_ZONE
46
     */
47
    const DEFAULT_TIME_ZONE = 'UTC';
48
49
    /**
50
     * Default base URI of backend
51
     *
52
     * @const DEFAULT_BASE_URI
53
     */
54
    const DEFAULT_BASE_URI = 'https://api.reporting.cloud';
55
56
    /**
57
     * Default version string of backend
58
     *
59
     * @const DEFAULT_VERSION
60
     */
61
    const DEFAULT_VERSION = 'v1';
62
63
    /**
64
     * Default timeout of backend in seconds
65
     *
66
     * @const DEFAULT_TIMEOUT
67
     */
68
    const DEFAULT_TIMEOUT = 120; // seconds
69
70
    /**
71
     * Default debug flag of REST client
72
     *
73
     * @const DEFAULT_DEBUG
74
     */
75
    const DEFAULT_DEBUG = false;
76
77
    /**
78
     * Backend username
79
     *
80
     * @var string
81
     */
82
    protected $username;
83
84
    /**
85
     * Backend password
86
     *
87
     * @var string
88
     */
89
    protected $password;
90
91
    /**
92
     * Backend base URI
93
     *
94
     * @var string
95
     */
96
    protected $baseUri;
97
98
    /**
99
     * Backend version string
100
     *
101
     * @var string
102
     */
103
    protected $version;
104
105
    /**
106
     * Backend timeout in seconds
107
     *
108
     * @var integer
109
     */
110
    protected $timeout;
111
112
    /**
113
     * REST client to backend
114
     *
115
     * @var Client
116
     */
117
    protected $client;
118
119
    /**
120
     * Debug flag of REST client
121
     *
122
     * @var boolean
123
     */
124
    protected $debug;
125
126
    /**
127
     * AbstractReportingCloud constructor
128
     *
129
     * @param array $options
130
     */
131 46
    public function __construct($options = [])
132
    {
133 46
        if (array_key_exists('username', $options)) {
134 1
            $this->setUsername($options['username']);
135 1
        }
136
137 46
        if (array_key_exists('password', $options)) {
138 1
            $this->setPassword($options['password']);
139 1
        }
140
141 46
        if (array_key_exists('base_uri', $options)) {
142 1
            $this->setBaseUri($options['base_uri']);
143 1
        }
144
145 46
        if (array_key_exists('version', $options)) {
146 1
            $this->setVersion($options['version']);
147 1
        }
148
149 46
        if (array_key_exists('timeout', $options)) {
150 1
            $this->setTimeout($options['timeout']);
151 1
        }
152
153 46
        if (array_key_exists('debug', $options)) {
154 1
            $this->setDebug($options['debug']);
155 1
        }
156 46
    }
157
158
    /**
159
     * Return the REST client of the backend web service
160
     *
161
     * @return \GuzzleHttp\Client
162
     */
163 14
    public function getClient()
164
    {
165 14
        if (null === $this->client) {
166
167 14
            $usernamePassword = sprintf('%s:%s', $this->getUsername(), $this->getPassword());
168 14
            $authorization    = sprintf('Basic %s', base64_encode($usernamePassword));
169
170 14
            $client = new Client([
171 14
                'base_uri'              => $this->getBaseUri(),
172 14
                RequestOptions::TIMEOUT => $this->getTimeout(),
173 14
                RequestOptions::DEBUG   => $this->getDebug(),
174 14
                RequestOptions::HEADERS => [
175 14
                    'Authorization' => $authorization,
176 14
                ],
177 14
            ]);
178
179 14
            $this->setClient($client);
180 14
        }
181
182 14
        return $this->client;
183
    }
184
185
    /**
186
     * Set the REST client of the backend web service
187
     *
188
     * @param Client $client REST client
189
     *
190
     * @return ReportingCloud
191
     */
192 14
    public function setClient(Client $client)
193
    {
194 14
        $this->client = $client;
195
196 14
        return $this;
197
    }
198
199
    /**
200
     * Return the base URI of the backend web service
201
     *
202
     * @return string
203
     */
204 18
    public function getBaseUri()
205
    {
206 18
        if (null === $this->baseUri) {
207 16
            $this->setBaseUri(self::DEFAULT_BASE_URI);
208 16
        }
209
210 18
        return $this->baseUri;
211
    }
212
213
    /**
214
     * Set the base URI of the backend web service
215
     *
216
     * @param string $baseUri Base URI
217
     *
218
     * @return ReportingCloud
219
     */
220 18
    public function setBaseUri($baseUri)
221
    {
222 18
        $this->baseUri = $baseUri;
223
224 18
        return $this;
225
    }
226
227
    /**
228
     * Get the timeout (in seconds) of the backend web service
229
     *
230
     * @return integer
231
     */
232 17
    public function getTimeout()
233
    {
234 17
        if (null === $this->timeout) {
235 15
            $this->setTimeout(self::DEFAULT_TIMEOUT);
236 15
        }
237
238 17
        return $this->timeout;
239
    }
240
241
    /**
242
     * Set the timeout (in seconds) of the backend web service
243
     *
244
     * @param integer $timeout Timeout
245
     *
246
     * @return ReportingCloud
247
     */
248 17
    public function setTimeout($timeout)
249
    {
250 17
        $this->timeout = (integer) $timeout;
251
252 17
        return $this;
253
    }
254
255
    /**
256
     * Return the username
257
     *
258
     * @return string
259
     */
260 17
    public function getUsername()
261
    {
262 17
        return $this->username;
263
    }
264
265
    /**
266
     * Set the username
267
     *
268
     * @param string $username Username
269
     *
270
     * @return ReportingCloud
271
     */
272 43
    public function setUsername($username)
273
    {
274 43
        $this->username = $username;
275
276 43
        return $this;
277
    }
278
279
    /**
280
     * Return the password
281
     *
282
     * @return string
283
     */
284 17
    public function getPassword()
285
    {
286 17
        return $this->password;
287
    }
288
289
    /**
290
     * Set the password
291
     *
292
     * @param string $password Password
293
     *
294
     * @return ReportingCloud
295
     */
296 43
    public function setPassword($password)
297
    {
298 43
        $this->password = $password;
299
300 43
        return $this;
301
    }
302
303
    /**
304
     * Return the debug flag
305
     *
306
     * @return mixed
307
     */
308 17
    public function getDebug()
309
    {
310 17
        if (null === $this->debug) {
311 15
            $this->setDebug(self::DEFAULT_DEBUG);
312 15
        }
313
314 17
        return $this->debug;
315
    }
316
317
    /**
318
     * Set the debug flag
319
     *
320
     * @param boolean $debug Debug flag
321
     *
322
     * @return ReportingCloud
323
     */
324 17
    public function setDebug($debug)
325
    {
326 17
        $this->debug = (boolean) $debug;
327
328 17
        return $this;
329
    }
330
331
    /**
332
     * Construct URI with version number
333
     *
334
     * @param string $uri URI
335
     *
336
     * @return string
337
     */
338 13
    protected function uri($uri)
339
    {
340 13
        return sprintf('/%s%s', $this->getVersion(), $uri);
341
    }
342
343
    /**
344
     * Get the version string of the backend web service
345
     *
346
     * @return string
347
     */
348 16
    public function getVersion()
349
    {
350 16
        if (null === $this->version) {
351 14
            $this->version = self::DEFAULT_VERSION;
352 14
        }
353
354 16
        return $this->version;
355
    }
356
357
    /**
358
     * Set the version string of the backend web service
359
     *
360
     * @param string $version Version string
361
     *
362
     * @return ReportingCloud
363
     */
364 2
    public function setVersion($version)
365
    {
366 2
        $this->version = $version;
367
368 2
        return $this;
369
    }
370
371
    /**
372
     * Request the URI with options
373
     *
374
     * @param string $method  HTTP method
375
     * @param string $uri     URI
376
     * @param array  $options Options
377
     *
378
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
379
     *
380
     * @throws RuntimeException
381
     */
382 13
    protected function request($method, $uri, $options)
383
    {
384 13
        $ret = null;
385
386 13
        $client = $this->getClient();
387
388
        try {
389
390 13
            if (getenv('TRAVIS')) {
391
                $options['curl'][CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_1;
392
            }
393
394 13
            $ret = $client->request($method, $uri, $options);
395
396 13
        } catch (\Exception $exception) {
397
398
            // \GuzzleHttp\Exception\ClientException
399
            // \GuzzleHttp\Exception\ServerException
400
401 1
            $message = (string) $exception->getMessage();
402 1
            $code    = (integer) $exception->getCode();
403
404 1
            throw new RuntimeException($message, $code);
405
        }
406
407 12
        return $ret;
408
    }
409
410
    /**
411
     * Using the passed propertyMap, recursively normalizes the keys of the passed array
412
     *
413
     * @param array       $array       Array
414
     * @param PropertyMap $propertyMap PropertyMap
415
     *
416
     * @return array
417
     */
418 3
    protected function normalizeArrayKeys($array, PropertyMap $propertyMap)
419
    {
420 3
        $ret = [];
421
422 3
        foreach ($array as $key => $value) {
423 3
            $map = $propertyMap->getMap();
424 3
            if (isset($map[$key])) {
425 3
                $key = $map[$key];
426 3
            }
427 3
            if (is_array($value)) {
428 2
                $value = $this->normalizeArrayKeys($value, $propertyMap);
429 2
            }
430 3
            $ret[$key] = $value;
431 3
        }
432
433 3
        return $ret;
434
    }
435
436
}