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
|
|
|
trait SetGetTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Backend username |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $username; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Backend password |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $password; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Backend API key |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $apiKey; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* When true, backend prints "TEST MODE" water mark into output document, and API call does not count against quota |
45
|
|
|
* |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
protected $test; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Backend base URI |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $baseUri; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Backend version string |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $version; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Backend timeout in seconds |
66
|
|
|
* |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
protected $timeout; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* REST client to backend |
73
|
|
|
* |
74
|
|
|
* @var Client |
75
|
|
|
*/ |
76
|
|
|
protected $client; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Debug flag of REST client |
80
|
|
|
* |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
protected $debug; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the REST client of the backend web service |
87
|
|
|
* |
88
|
|
|
* @return \GuzzleHttp\Client |
89
|
|
|
*/ |
90
|
23 |
|
public function getClient() |
91
|
|
|
{ |
92
|
23 |
|
if (null === $this->client) { |
93
|
|
|
|
94
|
23 |
|
$authorization = call_user_func(function () { |
95
|
|
|
|
96
|
23 |
|
if (null !== $this->getApiKey()) { |
97
|
1 |
|
return sprintf('ReportingCloud-APIKey %s', $this->getApiKey()); |
98
|
|
|
} |
99
|
|
|
|
100
|
23 |
|
if (null !== $this->getUsername() && null !== $this->getPassword()) { |
101
|
23 |
|
$value = sprintf('%s:%s', $this->getUsername(), $this->getPassword()); |
102
|
23 |
|
return sprintf('Basic %s', base64_encode($value)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$message = 'Either the API key, or username and password must be set for authorization'; |
106
|
|
|
throw new InvalidArgumentException($message); |
107
|
23 |
|
}); |
108
|
|
|
|
109
|
|
|
$options = [ |
110
|
23 |
|
'base_uri' => $this->getBaseUri(), |
111
|
23 |
|
RequestOptions::TIMEOUT => $this->getTimeout(), |
112
|
23 |
|
RequestOptions::DEBUG => $this->getDebug(), |
113
|
23 |
|
RequestOptions::HEADERS => [ |
114
|
23 |
|
'Authorization' => $authorization, |
115
|
23 |
|
], |
116
|
23 |
|
]; |
117
|
|
|
|
118
|
23 |
|
$client = new Client($options); |
119
|
|
|
|
120
|
23 |
|
$this->setClient($client); |
121
|
23 |
|
} |
122
|
|
|
|
123
|
23 |
|
return $this->client; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set the REST client of the backend web service |
128
|
|
|
* |
129
|
|
|
* @param Client $client REST client |
130
|
|
|
* |
131
|
|
|
* @return ReportingCloud |
132
|
|
|
*/ |
133
|
23 |
|
public function setClient(Client $client) |
134
|
|
|
{ |
135
|
23 |
|
$this->client = $client; |
136
|
|
|
|
137
|
23 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return the username |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
26 |
|
public function getUsername() |
146
|
|
|
{ |
147
|
26 |
|
return $this->username; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Set the username |
152
|
|
|
* |
153
|
|
|
* @param string $username Username |
154
|
|
|
* |
155
|
|
|
* @return ReportingCloud |
156
|
|
|
*/ |
157
|
65 |
|
public function setUsername($username) |
158
|
|
|
{ |
159
|
65 |
|
$this->username = $username; |
160
|
|
|
|
161
|
65 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return the password |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
26 |
|
public function getPassword() |
170
|
|
|
{ |
171
|
26 |
|
return $this->password; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set the password |
176
|
|
|
* |
177
|
|
|
* @param string $password Password |
178
|
|
|
* |
179
|
|
|
* @return ReportingCloud |
180
|
|
|
*/ |
181
|
65 |
|
public function setPassword($password) |
182
|
|
|
{ |
183
|
65 |
|
$this->password = $password; |
184
|
|
|
|
185
|
65 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Return the API key |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
23 |
|
public function getApiKey() |
194
|
|
|
{ |
195
|
23 |
|
return $this->apiKey; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Set the API key |
200
|
|
|
* |
201
|
|
|
* @param string $apiKey API key |
202
|
|
|
* |
203
|
|
|
* @return ReportingCloud |
204
|
|
|
*/ |
205
|
1 |
|
public function setApiKey($apiKey) |
206
|
|
|
{ |
207
|
1 |
|
$this->apiKey = $apiKey; |
208
|
|
|
|
209
|
1 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return the base URI of the backend web service |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
27 |
|
public function getBaseUri() |
218
|
|
|
{ |
219
|
27 |
|
if (null === $this->baseUri) { |
220
|
25 |
|
$this->setBaseUri(self::DEFAULT_BASE_URI); |
221
|
25 |
|
} |
222
|
|
|
|
223
|
27 |
|
return $this->baseUri; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Set the base URI of the backend web service |
228
|
|
|
* |
229
|
|
|
* @param string $baseUri Base URI |
230
|
|
|
* |
231
|
|
|
* @return ReportingCloud |
232
|
|
|
*/ |
233
|
27 |
|
public function setBaseUri($baseUri) |
234
|
|
|
{ |
235
|
27 |
|
$this->baseUri = $baseUri; |
236
|
|
|
|
237
|
27 |
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get the timeout (in seconds) of the backend web service |
242
|
|
|
* |
243
|
|
|
* @return int |
244
|
|
|
*/ |
245
|
26 |
|
public function getTimeout() |
246
|
|
|
{ |
247
|
26 |
|
if (null === $this->timeout) { |
248
|
24 |
|
$this->setTimeout(self::DEFAULT_TIMEOUT); |
249
|
24 |
|
} |
250
|
|
|
|
251
|
26 |
|
return $this->timeout; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set the timeout (in seconds) of the backend web service |
256
|
|
|
* |
257
|
|
|
* @param int $timeout Timeout |
258
|
|
|
* |
259
|
|
|
* @return ReportingCloud |
260
|
|
|
*/ |
261
|
26 |
|
public function setTimeout($timeout) |
262
|
|
|
{ |
263
|
26 |
|
$this->timeout = (int) $timeout; |
264
|
|
|
|
265
|
26 |
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Return the debug flag |
270
|
|
|
* |
271
|
|
|
* @return mixed |
272
|
|
|
*/ |
273
|
26 |
|
public function getDebug() |
274
|
|
|
{ |
275
|
26 |
|
if (null === $this->debug) { |
276
|
24 |
|
$this->setDebug(self::DEFAULT_DEBUG); |
277
|
24 |
|
} |
278
|
|
|
|
279
|
26 |
|
return $this->debug; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Set the debug flag |
284
|
|
|
* |
285
|
|
|
* @param bool $debug Debug flag |
286
|
|
|
* |
287
|
|
|
* @return ReportingCloud |
288
|
|
|
*/ |
289
|
26 |
|
public function setDebug($debug) |
290
|
|
|
{ |
291
|
26 |
|
$this->debug = (bool) $debug; |
292
|
|
|
|
293
|
26 |
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Return the test flag |
298
|
|
|
* |
299
|
|
|
* @return mixed |
300
|
|
|
*/ |
301
|
24 |
|
public function getTest() |
302
|
|
|
{ |
303
|
24 |
|
if (null === $this->test) { |
304
|
22 |
|
$this->setTest(self::DEFAULT_TEST); |
305
|
22 |
|
} |
306
|
|
|
|
307
|
24 |
|
return $this->test; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Set the test flag |
312
|
|
|
* |
313
|
|
|
* @param bool $test Test flag |
314
|
|
|
* |
315
|
|
|
* @return ReportingCloud |
316
|
|
|
*/ |
317
|
24 |
|
public function setTest($test) |
318
|
|
|
{ |
319
|
24 |
|
$this->test = (bool) $test; |
320
|
|
|
|
321
|
24 |
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get the version string of the backend web service |
326
|
|
|
* |
327
|
|
|
* @return string |
328
|
|
|
*/ |
329
|
25 |
|
public function getVersion() |
330
|
|
|
{ |
331
|
25 |
|
if (null === $this->version) { |
332
|
23 |
|
$this->version = self::DEFAULT_VERSION; |
333
|
23 |
|
} |
334
|
|
|
|
335
|
25 |
|
return $this->version; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Set the version string of the backend web service |
340
|
|
|
* |
341
|
|
|
* @param string $version Version string |
342
|
|
|
* |
343
|
|
|
* @return ReportingCloud |
344
|
|
|
*/ |
345
|
2 |
|
public function setVersion($version) |
346
|
|
|
{ |
347
|
2 |
|
$this->version = $version; |
348
|
|
|
|
349
|
2 |
|
return $this; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|