1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\Client\VMPro\ApiClient; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use JMS\Serializer\Serializer; |
7
|
|
|
use MovingImage\Client\VMPro\Exception; |
8
|
|
|
use MovingImage\Util\Logging\Traits\LoggerAwareTrait; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Log\LoggerAwareInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AbstractCoreApiClient. |
14
|
|
|
* |
15
|
|
|
* @author Ruben Knol <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractCoreApiClient implements LoggerAwareInterface |
18
|
|
|
{ |
19
|
|
|
use LoggerAwareTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ClientInterface The Guzzle HTTP client |
23
|
|
|
*/ |
24
|
|
|
protected $httpClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Serializer The JMS Serializer instance |
28
|
|
|
*/ |
29
|
|
|
protected $serializer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ApiClient constructor. |
33
|
|
|
* |
34
|
|
|
* @param ClientInterface $httpClient |
35
|
|
|
* @param Serializer $serializer |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
ClientInterface $httpClient, |
39
|
|
|
Serializer $serializer |
40
|
|
|
) { |
41
|
|
|
$this->httpClient = $httpClient; |
42
|
|
|
$this->serializer = $serializer; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Perform the actual request in the implementation classes. |
47
|
|
|
* |
48
|
|
|
* @param string $method |
49
|
|
|
* @param string $uri |
50
|
|
|
* @param array $options |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
abstract protected function _doRequest($method, $uri, $options); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Make a request to the API and serialize the result according to our |
58
|
|
|
* serialization strategy. |
59
|
|
|
* |
60
|
|
|
* @param string $method |
61
|
|
|
* @param string $uri |
62
|
|
|
* @param array $options |
63
|
|
|
* @param object $serialisationClass |
|
|
|
|
64
|
|
|
* |
65
|
|
|
* @return object|ResponseInterface |
66
|
|
|
*/ |
67
|
|
|
protected function makeRequest($method, $uri, $options) |
68
|
|
|
{ |
69
|
|
|
$logger = $this->getLogger(); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
// Automagically replace '%videoManagerId%' with the appropriate |
73
|
|
|
// value if it' present in the options |
74
|
|
|
if (strpos($uri, '%videoManagerId%') !== false && isset($options['videoManagerId'])) { |
75
|
|
|
$uri = str_replace('%videoManagerId%', $options['videoManagerId'], $uri); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$logger->info(sprintf('Making API %s request to %s', $method, $uri), [$uri]); |
79
|
|
|
|
80
|
|
|
$response = $this->_doRequest($method, $uri, $options); |
81
|
|
|
|
82
|
|
|
$logger->debug('Response from HTTP call was status code:', [$response->getStatusCode()]); |
83
|
|
|
$logger->debug('Response JSON was:', [$response->getBody()]); |
84
|
|
|
|
85
|
|
|
return $response; |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
throw $e; // Just rethrow for now |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Deserialize a response into an instance of it's associated class. |
93
|
|
|
* |
94
|
|
|
* @param string $data |
95
|
|
|
* @param string $serialisationClass |
96
|
|
|
* |
97
|
|
|
* @return object |
98
|
|
|
*/ |
99
|
|
|
protected function deserialize($data, $serialisationClass) |
100
|
|
|
{ |
101
|
|
|
return $this->serializer->deserialize($data, $serialisationClass, 'json'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Helper method to build the JSON data array for making a request |
106
|
|
|
* with ::makeRequest(). Optional parameters with empty or null value will be |
107
|
|
|
* omitted from the return value. |
108
|
|
|
* |
109
|
|
|
* Examples: |
110
|
|
|
* |
111
|
|
|
* $this->buildJsonParameters(['title' => 'test'], ['description' => '', 'bla' => 'test']) |
112
|
|
|
* |
113
|
|
|
* Would result in: |
114
|
|
|
* |
115
|
|
|
* [ |
116
|
|
|
* 'title' => 'test', |
117
|
|
|
* 'bla' => 'test', |
118
|
|
|
* ] |
119
|
|
|
* |
120
|
|
|
* @param array $required |
121
|
|
|
* @param array $optional |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function buildJsonParameters(array $required, array $optional) |
126
|
|
|
{ |
127
|
|
|
foreach ($required as $key => $value) { |
128
|
|
|
if (empty($value)) { |
129
|
|
|
throw new Exception(sprintf('Required parameter \'%s\' is missing..', $key)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$json = $required; |
134
|
|
|
|
135
|
|
|
foreach ($optional as $key => $value) { |
136
|
|
|
if (!empty($value) || $value === false) { |
137
|
|
|
$json[$key] = $value; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $json; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.