|
1
|
|
|
<?php namespace Cornford\Packtpublr; |
|
2
|
|
|
|
|
3
|
|
|
use Cornford\Packtpublr\Contracts\RequestableInterface; |
|
4
|
|
|
use Cornford\Packtpublr\Contracts\OutputableInterface; |
|
5
|
|
|
use Cornford\Packtpublr\Exceptions\PacktpublrArgumentException; |
|
6
|
|
|
use Cornford\Packtpublr\Exceptions\PacktpublrRequestException; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use GuzzleHttp\Client; |
|
9
|
|
|
use GuzzleHttp\Cookie\CookieJar; |
|
10
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
|
11
|
|
|
use GuzzleHttp\Subscriber\Cookie; |
|
12
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
13
|
|
|
|
|
14
|
|
|
abstract class PacktpublrBase implements RequestableInterface, OutputableInterface |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
const REQUEST_TIMEOUT = 15; |
|
18
|
|
|
|
|
19
|
|
|
const REQUEST_METHOD_GET = 0; |
|
20
|
|
|
const REQUEST_METHOD_POST = 1; |
|
21
|
|
|
|
|
22
|
|
|
const REQUEST_CONTENT_PLAIN = 0; |
|
23
|
|
|
const REQUEST_CONTENT_XML = 1; |
|
24
|
|
|
const REQUEST_CONTENT_JSON = 2; |
|
25
|
|
|
|
|
26
|
|
|
const RESPONSE_CODE_OK = 200; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Guzzle Http client. |
|
30
|
|
|
* |
|
31
|
|
|
* @var Client |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $httpClient; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Guzzle cookie subscriber. |
|
37
|
|
|
* |
|
38
|
|
|
* @var Cookie |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $cookieSubscriber; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Symfony console output. |
|
44
|
|
|
* |
|
45
|
|
|
* @var ConsoleOutput |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $consoleOutput; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The request type. |
|
51
|
|
|
* |
|
52
|
|
|
* @var integer |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $requestType = self::REQUEST_CONTENT_PLAIN; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The base URL. |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $baseUrl; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Response body. |
|
65
|
|
|
* |
|
66
|
|
|
* @var string|object |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $responseBody; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Response code. |
|
72
|
|
|
* |
|
73
|
|
|
* @var integer |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $responseCode; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Response headers. |
|
79
|
|
|
* |
|
80
|
|
|
* @var array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $responseHeaders; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Construct Packtpublr |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $options |
|
88
|
|
|
* |
|
89
|
|
|
* @throws PacktpublrArgumentException |
|
90
|
|
|
*/ |
|
91
|
|
|
public function __construct(array $options = []) |
|
92
|
|
|
{ |
|
93
|
|
|
if (empty($options)) { |
|
94
|
|
|
throw new PacktpublrArgumentException('Packtpublr requires both email and password as arguments.'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
View Code Duplication |
if (!isset($options['email']) || isset($options['email']) && !is_string($options['email'])) { |
|
98
|
|
|
throw new PacktpublrArgumentException('Packtpublr email address is required.'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
View Code Duplication |
if (!isset($options['password']) || isset($options['password']) && !is_string($options['password'])) { |
|
102
|
|
|
throw new PacktpublrArgumentException('Packtpublr password is required.'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->email = $options['email']; |
|
106
|
|
|
$this->password = $options['password']; |
|
107
|
|
|
|
|
108
|
|
|
$this->httpClient = new Client([ |
|
109
|
|
|
'base_url' => $this->baseUrl, |
|
110
|
|
|
'defaults' => [ |
|
111
|
|
|
'connect_timeout' => self::REQUEST_TIMEOUT, |
|
112
|
|
|
'timeout' => self::REQUEST_TIMEOUT |
|
113
|
|
|
] |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
$this->cookieSubscriber = new Cookie(new CookieJar()); |
|
117
|
|
|
$this->httpClient->getEmitter()->attach($this->cookieSubscriber); |
|
118
|
|
|
|
|
119
|
|
|
$this->consoleOutput = new ConsoleOutput(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Create request. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $url |
|
126
|
|
|
* @param array $parameters |
|
127
|
|
|
* @param integer $requestMethod |
|
128
|
|
|
* @param integer $requestContent |
|
129
|
|
|
* |
|
130
|
|
|
* @throws PacktpublrRequestException |
|
131
|
|
|
* |
|
132
|
|
|
* @return boolean |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function createRequest( |
|
135
|
|
|
$url, |
|
136
|
|
|
array $parameters = [], |
|
137
|
|
|
$requestMethod = self::REQUEST_METHOD_GET, |
|
138
|
|
|
$requestContent = self::REQUEST_CONTENT_PLAIN |
|
139
|
|
|
) { |
|
140
|
|
|
try { |
|
141
|
|
|
if ($requestMethod === self::REQUEST_METHOD_GET) { |
|
142
|
|
|
$url = isset($parameters['url']) ? str_replace(array_keys($parameters['url']), $parameters['url'], $url) : $url; |
|
143
|
|
|
unset($parameters['url']); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $this->sendRequest($url, $parameters, $requestMethod, $requestContent); |
|
147
|
|
|
} catch (Exception $exception) { |
|
148
|
|
|
throw new PacktpublrRequestException('An error occurred during the request.'); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Send request. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $url |
|
156
|
|
|
* @param array $parameters |
|
157
|
|
|
* @param integer $requestMethod |
|
158
|
|
|
* @param integer $requestContent |
|
159
|
|
|
* |
|
160
|
|
|
* @return boolean |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function sendRequest( |
|
163
|
|
|
$url, |
|
164
|
|
|
array $parameters = [], |
|
165
|
|
|
$requestMethod = self::REQUEST_METHOD_GET, |
|
166
|
|
|
$requestContent = self::REQUEST_CONTENT_PLAIN |
|
167
|
|
|
) { |
|
168
|
|
|
$request = null; |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
switch ($requestMethod) { |
|
171
|
|
|
case self::REQUEST_METHOD_POST: |
|
172
|
|
|
switch ($requestContent) { |
|
173
|
|
View Code Duplication |
case self::REQUEST_CONTENT_XML: |
|
174
|
|
|
$request = $this->getHttpClient() |
|
175
|
|
|
->post($url, ['headers' => ['content-type' => 'application/xml'], 'body' => $parameters]); |
|
176
|
|
|
break; |
|
177
|
|
View Code Duplication |
case self::REQUEST_CONTENT_JSON: |
|
178
|
|
|
$request = $this->getHttpClient() |
|
179
|
|
|
->post($url, ['headers' => ['content-type' => 'application/json'], 'body' => $parameters]); |
|
180
|
|
|
break; |
|
181
|
|
|
case self::REQUEST_CONTENT_PLAIN: |
|
182
|
|
|
default: |
|
183
|
|
|
$request = $this->getHttpClient() |
|
184
|
|
|
->post($url, ['body' => $parameters]); |
|
185
|
|
|
} |
|
186
|
|
|
break; |
|
187
|
|
|
case self::REQUEST_METHOD_GET: |
|
188
|
|
|
default: |
|
189
|
|
|
$request = $this->getHttpClient() |
|
190
|
|
|
->get($url); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if ($request === null) { |
|
194
|
|
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $this->parseResponse($request, $requestContent); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Parse response. |
|
202
|
|
|
* |
|
203
|
|
|
* @param ResponseInterface $response |
|
204
|
|
|
* @param integer $requestContent |
|
205
|
|
|
* |
|
206
|
|
|
* @return boolean |
|
207
|
|
|
*/ |
|
208
|
|
|
protected function parseResponse(ResponseInterface $response, $requestContent = self::REQUEST_CONTENT_PLAIN) |
|
209
|
|
|
{ |
|
210
|
|
|
$this->setResponseCode((integer) $response->getStatusCode()); |
|
211
|
|
|
$this->setResponseHeaders((array) $response->getHeaders()); |
|
212
|
|
|
|
|
213
|
|
|
switch ($requestContent) { |
|
214
|
|
|
case self::REQUEST_CONTENT_XML: |
|
215
|
|
|
$this->setResponseBody($response->xml()); |
|
216
|
|
|
break; |
|
217
|
|
|
case self::REQUEST_CONTENT_JSON: |
|
218
|
|
|
$this->setResponseBody($response->json()); |
|
219
|
|
|
break; |
|
220
|
|
|
case self::REQUEST_CONTENT_PLAIN: |
|
221
|
|
|
default: |
|
222
|
|
|
$this->setResponseBody($response->getBody()->__toString()); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
if ($this->getResponseCode() !== self::RESPONSE_CODE_OK) { |
|
226
|
|
|
return false; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return true; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get Http client. |
|
234
|
|
|
* |
|
235
|
|
|
* @return Client |
|
236
|
|
|
*/ |
|
237
|
|
|
public function getHttpClient() |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->httpClient; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Set Http client. |
|
244
|
|
|
* |
|
245
|
|
|
* @param Client $client |
|
246
|
|
|
* |
|
247
|
|
|
* @return void |
|
248
|
|
|
*/ |
|
249
|
|
|
public function setHttpClient(Client $client) |
|
250
|
|
|
{ |
|
251
|
|
|
$this->httpClient = $client; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Get cookie subscriber |
|
256
|
|
|
* |
|
257
|
|
|
* @return Cookie |
|
258
|
|
|
*/ |
|
259
|
|
|
public function getCookieSubscriber() |
|
260
|
|
|
{ |
|
261
|
|
|
return $this->cookieSubscriber; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Set cookie subscriber. |
|
266
|
|
|
* |
|
267
|
|
|
* @param Cookie $cookieSubscriber |
|
268
|
|
|
* |
|
269
|
|
|
* @return void |
|
270
|
|
|
*/ |
|
271
|
|
|
public function setCookieSubscriber(Cookie $cookieSubscriber) |
|
272
|
|
|
{ |
|
273
|
|
|
$this->getHttpClient()->getEmitter()->detach($this->cookieSubscriber); |
|
274
|
|
|
$this->cookieSubscriber = $cookieSubscriber; |
|
275
|
|
|
$this->getHttpClient()->getEmitter()->attach($this->cookieSubscriber); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Get console output. |
|
280
|
|
|
* |
|
281
|
|
|
* @return ConsoleOutput |
|
282
|
|
|
*/ |
|
283
|
|
|
public function getConsoleOutput() |
|
284
|
|
|
{ |
|
285
|
|
|
return $this->consoleOutput; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Set console output. |
|
290
|
|
|
* |
|
291
|
|
|
* @param ConsoleOutput $consoleOutput |
|
292
|
|
|
* |
|
293
|
|
|
* @return void |
|
294
|
|
|
*/ |
|
295
|
|
|
public function setConsoleOutput(ConsoleOutput $consoleOutput) |
|
296
|
|
|
{ |
|
297
|
|
|
$this->consoleOutput = $consoleOutput; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Write a string as standard output. |
|
302
|
|
|
* |
|
303
|
|
|
* @param string $string |
|
304
|
|
|
* |
|
305
|
|
|
* @return void |
|
306
|
|
|
*/ |
|
307
|
|
|
public function line($string) |
|
308
|
|
|
{ |
|
309
|
|
|
$this->consoleOutput->writeln($string); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Write a string as information output. |
|
314
|
|
|
* |
|
315
|
|
|
* @param string $string |
|
316
|
|
|
* |
|
317
|
|
|
* @return void |
|
318
|
|
|
*/ |
|
319
|
|
|
public function info($string) |
|
320
|
|
|
{ |
|
321
|
|
|
$this->consoleOutput->writeln("<fg=cyan>$string</>"); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Write a string as success output. |
|
326
|
|
|
* |
|
327
|
|
|
* @param string $string |
|
328
|
|
|
* |
|
329
|
|
|
* @return void |
|
330
|
|
|
*/ |
|
331
|
|
|
public function success($string) |
|
332
|
|
|
{ |
|
333
|
|
|
$this->consoleOutput->writeln("<fg=green>$string</>"); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* Write a string as warning output. |
|
338
|
|
|
* |
|
339
|
|
|
* @param string $string |
|
340
|
|
|
* |
|
341
|
|
|
* @return void |
|
342
|
|
|
*/ |
|
343
|
|
|
public function warning($string) |
|
344
|
|
|
{ |
|
345
|
|
|
$this->consoleOutput->writeln("<fg=yellow>$string</>"); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Write a string as error output. |
|
350
|
|
|
* |
|
351
|
|
|
* @param string $string |
|
352
|
|
|
* |
|
353
|
|
|
* @return void |
|
354
|
|
|
*/ |
|
355
|
|
|
public function error($string) |
|
356
|
|
|
{ |
|
357
|
|
|
$this->consoleOutput->writeln("<fg=red>$string</>"); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* Get response body. |
|
362
|
|
|
* |
|
363
|
|
|
* @return string|object |
|
364
|
|
|
*/ |
|
365
|
|
|
public function getResponseBody() |
|
366
|
|
|
{ |
|
367
|
|
|
return $this->responseBody; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* Set response code. |
|
372
|
|
|
* |
|
373
|
|
|
* @param string|object $body |
|
374
|
|
|
* |
|
375
|
|
|
* @return void |
|
376
|
|
|
*/ |
|
377
|
|
|
protected function setResponseBody($body) |
|
378
|
|
|
{ |
|
379
|
|
|
$this->responseBody = $body; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Get response code. |
|
384
|
|
|
* |
|
385
|
|
|
* @return integer |
|
386
|
|
|
*/ |
|
387
|
|
|
public function getResponseCode() |
|
388
|
|
|
{ |
|
389
|
|
|
return $this->responseCode; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Set response code. |
|
394
|
|
|
* |
|
395
|
|
|
* @param integer $code |
|
396
|
|
|
* |
|
397
|
|
|
* @return void |
|
398
|
|
|
*/ |
|
399
|
|
|
protected function setResponseCode($code) |
|
400
|
|
|
{ |
|
401
|
|
|
$this->responseCode = $code; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* Get response headers. |
|
406
|
|
|
* |
|
407
|
|
|
* @return array |
|
408
|
|
|
*/ |
|
409
|
|
|
public function getResponseHeaders() |
|
410
|
|
|
{ |
|
411
|
|
|
return $this->responseHeaders; |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* Set response headers. |
|
416
|
|
|
* |
|
417
|
|
|
* @param array $headers |
|
418
|
|
|
* |
|
419
|
|
|
* @return void |
|
420
|
|
|
*/ |
|
421
|
|
|
protected function setResponseHeaders($headers) |
|
422
|
|
|
{ |
|
423
|
|
|
$this->responseHeaders = $headers; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.