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