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