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