Passed
Branch development-2.0 (33fa0b)
by Jonathan
05:48
created

AbstractReportingCloud::getTest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP Wrapper
6
 *
7
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud;
16
17
use GuzzleHttp\Client;
18
use GuzzleHttp\Exception\GuzzleException;
19
use GuzzleHttp\Psr7\Response;
20
use GuzzleHttp\RequestOptions;
21
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
22
use TxTextControl\ReportingCloud\Exception\RuntimeException;
23
use TxTextControl\ReportingCloud\Filter\Filter;
24
25
/**
26
 * Abstract ReportingCloud
27
 *
28
 * @package TxTextControl\ReportingCloud
29
 * @author  Jonathan Maron (@JonathanMaron)
30
 */
31
abstract class AbstractReportingCloud
32
{
33
    /**
34
     * Constants
35
     * -----------------------------------------------------------------------------------------------------------------
36
     */
37
38
    /**
39
     * Default date/time format of backend is 'ISO 8601'
40
     *
41
     * Note, last letter is 'P' and not 'O':
42
     *
43
     * O - Difference to Greenwich time (GMT) in hours (e.g. +0200)
44
     * P - Difference to Greenwich time (GMT) with colon between hours and minutes (e.g. +02:00)
45
     *
46
     * Backend uses the 'P' variant
47
     *
48
     * @const DEFAULT_DATE_FORMAT
49
     */
50
    public const DEFAULT_DATE_FORMAT = 'Y-m-d\TH:i:sP';
51
52
    /**
53
     * Default time zone of backend
54
     *
55
     * @const DEFAULT_TIME_ZONE
56
     */
57
    public const DEFAULT_TIME_ZONE = 'UTC';
58
59
    /**
60
     * Default base URI of backend
61
     *
62
     * @const DEFAULT_BASE_URI
63
     */
64
    protected const DEFAULT_BASE_URI = 'https://api.reporting.cloud';
65
66
    /**
67
     * Default version string of backend
68
     *
69
     * @const DEFAULT_VERSION
70
     */
71
    protected const DEFAULT_VERSION = 'v1';
72
73
    /**
74
     * Default timeout of backend in seconds
75
     *
76
     * @const DEFAULT_TIMEOUT
77
     */
78
    protected const DEFAULT_TIMEOUT = 120;
79
80
    /**
81
     * Default test flag of backend
82
     *
83
     * @const DEFAULT_TEST
84
     */
85
    protected const DEFAULT_TEST = false;
86
87
    /**
88
     * Default debug flag of REST client
89
     *
90
     * @const DEFAULT_DEBUG
91
     */
92
    protected const DEFAULT_DEBUG = false;
93
94
    /**
95
     * Constants (ReportingCloud values)
96
     * -----------------------------------------------------------------------------------------------------------------
97
     */
98
99
    /**
100
     * Document divider - none
101
     */
102
    public const DOCUMENT_DIVIDER_NONE = 1;
103
104
    /**
105
     * Document divider - new paragraph
106
     */
107
    public const DOCUMENT_DIVIDER_NEW_PARAGRAPH = 2;
108
109
    /**
110
     * Document divider - new section
111
     */
112
    public const DOCUMENT_DIVIDER_NEW_SECTION = 3;
113
114
    /**
115
     * Properties
116
     * -----------------------------------------------------------------------------------------------------------------
117
     */
118
119
    /**
120
     * Backend API key
121
     *
122
     * @var string|null
123
     */
124
    protected $apiKey;
125
126
    /**
127
     * Backend username
128
     *
129
     * @var string|null
130
     */
131
    protected $username;
132
133
    /**
134
     * Backend password
135
     *
136
     * @var string|null
137
     */
138
    protected $password;
139
140
    /**
141
     * When true, API call does not count against quota
142
     * "TEST MODE" watermark is added to document
143
     *
144
     * @var bool|null
145
     */
146
    protected $test;
147
148
    /**
149
     * Backend base URI
150
     *
151
     * @var string|null
152
     */
153
    protected $baseUri;
154
155
    /**
156
     * Backend version string
157
     *
158
     * @var string|null
159
     */
160
    protected $version;
161
162
    /**
163
     * Backend timeout in seconds
164
     *
165
     * @var int|null
166
     */
167
    protected $timeout;
168
169
    /**
170
     * REST client to backend
171
     *
172
     * @var Client|null
173
     */
174
    protected $client;
175
176
    /**
177
     * Debug flag of REST client
178
     *
179
     * @var bool|null
180
     */
181
    protected $debug;
182
183
    /**
184
     * Set and Get Methods
185
     * -----------------------------------------------------------------------------------------------------------------
186
     */
187
188
    /**
189
     * Return the API key
190
     *
191
     * @return string|null
192
     */
193 26
    public function getApiKey(): ?string
194
    {
195 26
        return $this->apiKey;
196
    }
197
198
    /**
199
     * Set the API key
200
     *
201
     * @param string $apiKey
202
     *
203
     * @return AbstractReportingCloud
204
     */
205 65
    public function setApiKey(string $apiKey): self
206
    {
207 65
        $this->apiKey = $apiKey;
208
209 65
        return $this;
210
    }
211
212
    /**
213
     * Return the username
214
     *
215
     * @return string|null
216
     */
217 5
    public function getUsername(): ?string
218
    {
219 5
        return $this->username;
220
    }
221
222
    /**
223
     * Set the username
224
     *
225
     * @param string $username
226
     *
227
     * @return AbstractReportingCloud
228
     */
229 4
    public function setUsername(string $username): self
230
    {
231 4
        $this->username = $username;
232
233 4
        return $this;
234
    }
235
236
    /**
237
     * Return the password
238
     *
239
     * @return string|null
240
     */
241 4
    public function getPassword(): ?string
242
    {
243 4
        return $this->password;
244
    }
245
246
    /**
247
     * Set the password
248
     *
249
     * @param string $password
250
     *
251
     * @return AbstractReportingCloud
252
     */
253 4
    public function setPassword(string $password): self
254
    {
255 4
        $this->password = $password;
256
257 4
        return $this;
258
    }
259
260
    /**
261
     * Return the base URI of the backend web service
262
     *
263
     * @return string|null
264
     */
265 30
    public function getBaseUri(): ?string
266
    {
267 30
        if (null === $this->baseUri) {
268 28
            $this->setBaseUri(self::DEFAULT_BASE_URI);
269
        }
270
271 30
        return $this->baseUri;
272
    }
273
274
    /**
275
     * Set the base URI of the backend web service
276
     *
277
     * @param string $baseUri
278
     *
279
     * @return AbstractReportingCloud
280
     */
281 30
    public function setBaseUri(string $baseUri): self
282
    {
283 30
        $this->baseUri = $baseUri;
284
285 30
        return $this;
286
    }
287
288
    /**
289
     * Get the timeout (in seconds) of the backend web service
290
     *
291
     * @return int|null
292
     */
293 29
    public function getTimeout(): ?int
294
    {
295 29
        if (null === $this->timeout) {
296 27
            $this->setTimeout(self::DEFAULT_TIMEOUT);
297
        }
298
299 29
        return $this->timeout;
300
    }
301
302
    /**
303
     * Set the timeout (in seconds) of the backend web service
304
     *
305
     * @param int $timeout
306
     *
307
     * @return AbstractReportingCloud
308
     */
309 29
    public function setTimeout(int $timeout): self
310
    {
311 29
        $this->timeout = $timeout;
312
313 29
        return $this;
314
    }
315
316
    /**
317
     * Return the debug flag
318
     *
319
     * @return bool|null
320
     */
321 29
    public function getDebug(): ?bool
322
    {
323 29
        if (null === $this->debug) {
324 27
            $this->setDebug(self::DEFAULT_DEBUG);
325
        }
326
327 29
        return $this->debug;
328
    }
329
330
    /**
331
     * Set the debug flag
332
     *
333
     * @param bool $debug Debug flag
334
     *
335
     * @return ReportingCloud
336
     */
337 29
    public function setDebug(bool $debug): self
338
    {
339 29
        $this->debug = $debug;
340
341 29
        return $this;
342
    }
343
344
    /**
345
     * Return the test flag
346
     *
347
     * @return bool|null
348
     */
349 25
    public function getTest(): ?bool
350
    {
351 25
        if (null === $this->test) {
352 23
            $this->setTest(self::DEFAULT_TEST);
353
        }
354
355 25
        return $this->test;
356
    }
357
358
    /**
359
     * Set the test flag
360
     *
361
     * @param bool $test
362
     *
363
     * @return AbstractReportingCloud
364
     */
365 25
    public function setTest(bool $test): self
366
    {
367 25
        $this->test = $test;
368
369 25
        return $this;
370
    }
371
372
    /**
373
     * Get the version string of the backend web service
374
     *
375
     * @return string|null
376
     */
377 27
    public function getVersion(): ?string
378
    {
379 27
        if (null === $this->version) {
380 25
            $this->version = self::DEFAULT_VERSION;
381
        }
382
383 27
        return $this->version;
384
    }
385
386
    /**
387
     * Set the version string of the backend web service
388
     *
389
     * @param string $version
390
     *
391
     * @return AbstractReportingCloud
392
     */
393 2
    public function setVersion(string $version): self
394
    {
395 2
        $this->version = $version;
396
397 2
        return $this;
398
    }
399
400
    /**
401
     * Return the REST client of the backend web service
402
     *
403
     * @return Client
404
     */
405 26
    public function getClient(): Client
406
    {
407 26
        if (!$this->client instanceof Client) {
408
409
            $authorization = function () {
410
411 26
                if (!empty($this->getApiKey())) {
412 24
                    return sprintf('ReportingCloud-APIKey %s', $this->getApiKey());
413
                }
414
415 2
                if (!empty($this->getUsername()) && !empty($this->getPassword())) {
416 1
                    $value = sprintf('%s:%s', $this->getUsername(), $this->getPassword());
417
418 1
                    return sprintf('Basic %s', base64_encode($value));
419
                }
420
421 1
                $message = 'Either the API key, or username and password must be set for authorization';
422 1
                throw new InvalidArgumentException($message);
423 26
            };
424
425
            $options = [
426 26
                'base_uri'              => $this->getBaseUri(),
427 26
                RequestOptions::TIMEOUT => $this->getTimeout(),
428 26
                RequestOptions::DEBUG   => $this->getDebug(),
429 26
                RequestOptions::HEADERS => [
430 26
                    'Authorization' => $authorization(),
431
                ],
432
            ];
433
434 25
            $client = new Client($options);
435
436 25
            $this->setClient($client);
437
        }
438
439 25
        return $this->client;
440
    }
441
442
    /**
443
     * Set the REST client of the backend web service
444
     *
445
     * @param Client $client
446
     *
447
     * @return AbstractReportingCloud
448
     */
449 25
    public function setClient(Client $client): self
450
    {
451 25
        $this->client = $client;
452
453 25
        return $this;
454
    }
455
456
    /**
457
     * Request the URI with options
458
     *
459
     * @param string $method  HTTP method
460
     * @param string $uri     URI
461
     * @param array  $options Options
462
     *
463
     * @return \GuzzleHttp\Psr7\Response
464
     * @throws \TxTextControl\ReportingCloud\Exception\RuntimeException
465
     */
466 24
    protected function request(string $method, string $uri, array $options): Response
467
    {
468 24
        $client = $this->getClient();
469
470
        try {
471 23
            if ($this->getTest()) {
472 1
                $options[RequestOptions::QUERY]['test'] = Filter::filterBooleanToString($this->getTest());
473
            }
474 23
            $response = $client->request($method, $uri, $options);
475 2
        } catch (GuzzleException $e) {
476 2
            $message = (string) $e->getMessage();
477 2
            $code    = (int) $e->getCode();
478 2
            throw new RuntimeException($message, $code);
479
        }
480
481 22
        return $response;
482
    }
483
484
    /**
485
     * Construct URI with version number
486
     *
487
     * @param string $uri URI
488
     *
489
     * @return string
490
     */
491 24
    protected function uri(string $uri): string
492
    {
493 24
        return sprintf('/%s%s', $this->getVersion(), $uri);
494
    }
495
}
496