Passed
Branch development-2.0 (f1893c)
by Jonathan
06:14
created

AbstractReportingCloud::setApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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