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