1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\Client\VMPro\ApiClient; |
4
|
|
|
|
5
|
|
|
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Guzzle6ApiClient. |
11
|
|
|
* |
12
|
|
|
* @author Ruben Knol <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Guzzle6ApiClient extends AbstractApiClient implements ApiClientInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Guzzle6 Client implementation for making HTTP requests with |
18
|
|
|
* the appropriate options. |
19
|
|
|
* |
20
|
|
|
* @param string $method |
21
|
|
|
* @param string $uri |
22
|
|
|
* @param array $options |
23
|
|
|
* |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
protected function _doRequest($method, $uri, $options) |
27
|
|
|
{ |
28
|
|
|
return $this->httpClient->request($method, $uri, $options); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
* |
34
|
|
|
* @param ResponseInterface $response |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
protected function serializeResponse($response) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
/** @var ResponseInterface $response */ |
41
|
|
|
$serialized = serialize([ |
42
|
|
|
$response->getStatusCode(), |
43
|
|
|
$response->getHeaders(), |
44
|
|
|
$response->getBody()->getContents(), |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
//subsequent calls need to access the stream from the beginning |
48
|
|
|
$response->getBody()->rewind(); |
49
|
|
|
|
50
|
|
|
return $serialized; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
* |
56
|
|
|
* @param string $serialized |
57
|
|
|
* |
58
|
|
|
* @return ResponseInterface |
59
|
|
|
* |
60
|
|
|
* @throws Exception |
61
|
|
|
*/ |
62
|
|
|
protected function unserializeResponse($serialized) |
63
|
|
|
{ |
64
|
|
|
$array = unserialize($serialized); |
65
|
|
|
if (!is_array($array) || count($array) !== 3) { |
66
|
|
|
throw new Exception(sprintf('Error unserializing response: %s', $serialized)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return new Response($array[0], $array[1], $array[2]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.