Completed
Push — master ( b10734...824501 )
by Jonathan
09:23
created

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