1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © 2017 Toan Nguyen. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Gojira\Api\Client; |
10
|
|
|
|
11
|
|
|
use Gojira\Api\Authentication\AuthenticationInterface; |
12
|
|
|
use Gojira\Api\Request\HttpMethod; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Abstract base class for the Api client |
16
|
|
|
* |
17
|
|
|
* @package Gojira\Api\Client |
18
|
|
|
* @author Toan Nguyen <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
abstract class BaseClient |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var AuthenticationInterface |
24
|
|
|
*/ |
25
|
|
|
protected $authentication; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $baseUrl; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $endpoint; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $endpointParameters = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $body = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $httpMethod = HttpMethod::GET; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
protected $debug = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $useCache = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var mixed |
64
|
|
|
*/ |
65
|
|
|
protected $data; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
protected $headers = []; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Constructor. |
74
|
|
|
* |
75
|
|
|
* @param string $baseUrl JIRA base url |
76
|
|
|
* @param AuthenticationInterface $authentication Authentication object |
77
|
|
|
* @param bool $debug Debug mode |
78
|
|
|
* @param bool $useCache Cache mode |
79
|
|
|
*/ |
80
|
|
|
public function __construct($baseUrl, AuthenticationInterface $authentication, $debug = false, $useCache = false) |
81
|
|
|
{ |
82
|
|
|
$this->setBaseUrl($baseUrl); |
83
|
|
|
$this->setAuthentication($authentication); |
84
|
|
|
$this->setDebug($debug); |
85
|
|
|
$this->setUseCache($useCache); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set authentication for current session |
90
|
|
|
* |
91
|
|
|
* @param \Gojira\Api\Authentication\AuthenticationInterface $authentication |
92
|
|
|
*/ |
93
|
|
|
public function setAuthentication(AuthenticationInterface $authentication) |
94
|
|
|
{ |
95
|
|
|
$this->authentication = $authentication; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get authentication object instance |
100
|
|
|
* |
101
|
|
|
* @return \Gojira\Api\Authentication\AuthenticationInterface |
102
|
|
|
*/ |
103
|
|
|
public function getAuthentication() |
104
|
|
|
{ |
105
|
|
|
return $this->authentication; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set JIRA base url |
110
|
|
|
* |
111
|
|
|
* @param string $baseUrl |
112
|
|
|
*/ |
113
|
|
|
public function setBaseUrl($baseUrl) |
114
|
|
|
{ |
115
|
|
|
$this->baseUrl = $baseUrl; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get JIRA base url |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getBaseUrl() |
124
|
|
|
{ |
125
|
|
|
return $this->baseUrl; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set JIRA endpoint |
130
|
|
|
* |
131
|
|
|
* @param string $endpoint |
132
|
|
|
*/ |
133
|
|
|
public function setEndpoint($endpoint) |
134
|
|
|
{ |
135
|
|
|
$this->endpoint = $endpoint; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get JIRA endpoint |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getEndpoint() |
144
|
|
|
{ |
145
|
|
|
return $this->endpoint; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set endpoint parameters |
150
|
|
|
* |
151
|
|
|
* @param array $endpointParameters |
152
|
|
|
*/ |
153
|
|
|
public function setEndpointParameters(array $endpointParameters) |
154
|
|
|
{ |
155
|
|
|
$this->endpointParameters = $endpointParameters; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get endpoint parameters |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function getEndpointParameters() |
164
|
|
|
{ |
165
|
|
|
return $this->endpointParameters; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set request body |
170
|
|
|
* |
171
|
|
|
* @param string $body |
172
|
|
|
*/ |
173
|
|
|
public function setBody($body) |
174
|
|
|
{ |
175
|
|
|
$this->body = $body; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get request body |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function getBody() |
184
|
|
|
{ |
185
|
|
|
return $this->body; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set HTTP method |
190
|
|
|
* |
191
|
|
|
* @param string $httpMethod |
192
|
|
|
*/ |
193
|
|
|
public function setHttpMethod($httpMethod) |
194
|
|
|
{ |
195
|
|
|
$this->httpMethod = $httpMethod; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get HTTP method |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
public function getHttpMethod() |
204
|
|
|
{ |
205
|
|
|
return $this->httpMethod; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set debug mode |
210
|
|
|
* |
211
|
|
|
* @param boolean $debug |
212
|
|
|
*/ |
213
|
|
|
public function setDebug($debug) |
214
|
|
|
{ |
215
|
|
|
$this->debug = $debug; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Check is debug mode |
220
|
|
|
* |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function isDebug() |
224
|
|
|
{ |
225
|
|
|
return $this->debug; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set use cache mode |
230
|
|
|
* |
231
|
|
|
* @param boolean $isUseCache |
232
|
|
|
*/ |
233
|
|
|
public function setUseCache($isUseCache) |
234
|
|
|
{ |
235
|
|
|
$this->useCache = $isUseCache; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Check use cache mode |
240
|
|
|
* |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
public function isUseCache() |
244
|
|
|
{ |
245
|
|
|
return $this->useCache; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Set data for the current object |
250
|
|
|
* |
251
|
|
|
* @param mixed $data |
252
|
|
|
*/ |
253
|
|
|
public function setData($data) |
254
|
|
|
{ |
255
|
|
|
$this->data = $data; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Get data |
260
|
|
|
* |
261
|
|
|
* @return mixed |
262
|
|
|
*/ |
263
|
|
|
public function getData() |
264
|
|
|
{ |
265
|
|
|
return $this->data; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Get deserialize response data (object data) |
270
|
|
|
* |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
|
|
public function getResult() |
274
|
|
|
{ |
275
|
|
|
return $this->data; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Set request headers |
280
|
|
|
* |
281
|
|
|
* @param array $headers |
282
|
|
|
*/ |
283
|
|
|
public function setHeaders($headers) |
284
|
|
|
{ |
285
|
|
|
$this->headers = $headers; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Get request headers |
290
|
|
|
* |
291
|
|
|
* @return array |
292
|
|
|
*/ |
293
|
|
|
public function getHeaders() |
294
|
|
|
{ |
295
|
|
|
return $this->headers; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Set header "Content-Type" |
300
|
|
|
* |
301
|
|
|
* @param string $contentType |
302
|
|
|
*/ |
303
|
|
|
public function setContentType($contentType) |
304
|
|
|
{ |
305
|
|
|
$this->headers['Content-Type'] = $contentType; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Get header "Content-Type" |
310
|
|
|
* |
311
|
|
|
* @return string|null |
312
|
|
|
*/ |
313
|
|
|
public function getContentType() |
314
|
|
|
{ |
315
|
|
|
if (isset($this->headers['Content-Type'])) { |
316
|
|
|
return $this->headers['Content-Type']; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return null; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Set header "Accept" |
324
|
|
|
* |
325
|
|
|
* @param string $accept |
326
|
|
|
*/ |
327
|
|
|
public function setAccept($accept) |
328
|
|
|
{ |
329
|
|
|
$this->headers['Accept'] = $accept; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Get header "Accept" |
334
|
|
|
* |
335
|
|
|
* @return string|null |
336
|
|
|
*/ |
337
|
|
|
public function getAccept() |
338
|
|
|
{ |
339
|
|
|
if (isset($this->headers['Accept'])) { |
340
|
|
|
return $this->headers['Accept']; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return null; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Set header "Authorization" |
348
|
|
|
* |
349
|
|
|
* @param string $token |
350
|
|
|
*/ |
351
|
|
|
public function setAuthorization($token) |
352
|
|
|
{ |
353
|
|
|
$this->headers['Authorization'] = 'Basic ' . $token; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Get header "Authorization" |
358
|
|
|
* |
359
|
|
|
* @return string|null |
360
|
|
|
*/ |
361
|
|
|
public function getAuthorization() |
362
|
|
|
{ |
363
|
|
|
if (isset($this->headers['Authorization'])) { |
364
|
|
|
return $this->headers['Authorization']; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
return null; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|