Completed
Push — master ( b2aef0...c91c69 )
by Jonathan
04:05
created

AbstractReportingCloud::getUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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