1 | <?php |
||
19 | abstract class AbstractCoreApiClient implements LoggerAwareInterface |
||
20 | { |
||
21 | use LoggerAwareTrait; |
||
22 | |||
23 | /** |
||
24 | * @const string |
||
25 | */ |
||
26 | const OPT_VIDEO_MANAGER_ID = 'videoManagerId'; |
||
27 | |||
28 | /** |
||
29 | * @var ClientInterface The Guzzle HTTP client |
||
30 | */ |
||
31 | protected $httpClient; |
||
32 | |||
33 | /** |
||
34 | * @var Serializer The JMS Serializer instance |
||
35 | */ |
||
36 | protected $serializer; |
||
37 | |||
38 | /** |
||
39 | * @var CacheItemPoolInterface PSR6 cache pool implementation |
||
40 | */ |
||
41 | protected $cacheItemPool; |
||
42 | |||
43 | /** |
||
44 | * @var mixed time-to-live for cached responses |
||
45 | * The type of this property might be integer, \DateInterval or null |
||
46 | * |
||
47 | * @see CacheItemInterface::expiresAfter() |
||
48 | */ |
||
49 | protected $cacheTtl; |
||
50 | |||
51 | /** |
||
52 | * ApiClient constructor. |
||
53 | * |
||
54 | * @param ClientInterface $httpClient |
||
55 | * @param Serializer $serializer |
||
56 | * @param CacheItemPoolInterface $cacheItemPool |
||
57 | * @param int $cacheTtl |
||
58 | */ |
||
59 | public function __construct( |
||
70 | |||
71 | /** |
||
72 | * @param CacheItemPoolInterface $cacheItemPool |
||
73 | */ |
||
74 | public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool) |
||
78 | |||
79 | /** |
||
80 | * @return CacheItemPoolInterface |
||
81 | */ |
||
82 | public function getCacheItemPool() |
||
86 | |||
87 | /** |
||
88 | * Perform the actual request in the implementation classes. |
||
89 | * |
||
90 | * @param string $method |
||
91 | * @param string $uri |
||
92 | * @param array $options |
||
93 | * |
||
94 | * @return mixed |
||
95 | */ |
||
96 | abstract protected function _doRequest($method, $uri, $options); |
||
97 | |||
98 | /** |
||
99 | * Make a request to the API and serialize the result according to our |
||
100 | * serialization strategy. |
||
101 | * |
||
102 | * @param string $method |
||
103 | * @param string $uri |
||
104 | * @param array $options |
||
105 | * |
||
106 | * @return object|ResponseInterface |
||
107 | */ |
||
108 | protected function makeRequest($method, $uri, $options) |
||
146 | |||
147 | /** |
||
148 | * Deserialize a response into an instance of it's associated class. |
||
149 | * |
||
150 | * @param string $data |
||
151 | * @param string $serialisationClass |
||
152 | * |
||
153 | * @return object |
||
154 | */ |
||
155 | protected function deserialize($data, $serialisationClass) |
||
159 | |||
160 | /** |
||
161 | * Helper method to build the JSON data array for making a request |
||
162 | * with ::makeRequest(). Optional parameters with empty or null value will be |
||
163 | * omitted from the return value. |
||
164 | * |
||
165 | * Examples: |
||
166 | * |
||
167 | * $this->buildJsonParameters(['title' => 'test'], ['description' => '', 'bla' => 'test']) |
||
168 | * |
||
169 | * Would result in: |
||
170 | * |
||
171 | * [ |
||
172 | * 'title' => 'test', |
||
173 | * 'bla' => 'test', |
||
174 | * ] |
||
175 | * |
||
176 | * @param array $required |
||
177 | * @param array $optional |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | protected function buildJsonParameters(array $required, array $optional) |
||
199 | |||
200 | /** |
||
201 | * Generates the cache key based on the class name, request method, uri and options. |
||
202 | * |
||
203 | * @param string $method |
||
204 | * @param string $uri |
||
205 | * @param array $options |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | private function generateCacheKey($method, $uri, array $options = []) |
||
213 | |||
214 | /** |
||
215 | * Checks if the request may be cached. |
||
216 | * |
||
217 | * @param string $method |
||
218 | * @param string $uri |
||
219 | * @param array $options |
||
220 | * @param mixed $response |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | private function isCachable($method, $uri, array $options, $response) |
||
231 | |||
232 | /** |
||
233 | * Serializes the provided response to a string, suitable for caching. |
||
234 | * The type of the $response argument varies depending on the guzzle version. |
||
235 | * |
||
236 | * @param mixed $response |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | abstract protected function serializeResponse($response); |
||
241 | |||
242 | /** |
||
243 | * Unserializes the serialized response into a response object. |
||
244 | * The return type varies depending on the guzzle version. |
||
245 | * |
||
246 | * @param string $serialized |
||
247 | * |
||
248 | * @return mixed |
||
249 | * |
||
250 | * @throws Exception |
||
251 | */ |
||
252 | abstract protected function unserializeResponse($serialized); |
||
253 | } |
||
254 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.