1 | <?php |
||
21 | abstract class AbstractCoreApiClient implements LoggerAwareInterface |
||
22 | { |
||
23 | use LoggerAwareTrait; |
||
24 | |||
25 | /** |
||
26 | * @const string |
||
27 | */ |
||
28 | const OPT_VIDEO_MANAGER_ID = 'videoManagerId'; |
||
29 | |||
30 | /** |
||
31 | * List of endpoints that may be cached, even though they use POST. |
||
32 | */ |
||
33 | const CACHEABLE_POST_ENDPOINTS = ['search']; |
||
34 | |||
35 | /** |
||
36 | * @var ClientInterface The Guzzle HTTP client |
||
37 | */ |
||
38 | protected $httpClient; |
||
39 | |||
40 | /** |
||
41 | * @var Serializer The JMS Serializer instance |
||
42 | */ |
||
43 | protected $serializer; |
||
44 | |||
45 | /** |
||
46 | * @var CacheItemPoolInterface PSR6 cache pool implementation |
||
47 | */ |
||
48 | protected $cacheItemPool; |
||
49 | |||
50 | /** |
||
51 | * @var mixed time-to-live for cached responses |
||
52 | * The type of this property might be integer, \DateInterval or null |
||
53 | * |
||
54 | * @see CacheItemInterface::expiresAfter() |
||
55 | */ |
||
56 | protected $cacheTtl; |
||
57 | |||
58 | /** |
||
59 | * @var StopwatchInterface |
||
60 | */ |
||
61 | protected $stopwatch; |
||
62 | |||
63 | /** |
||
64 | * ApiClient constructor. |
||
65 | * |
||
66 | * @param ClientInterface $httpClient |
||
67 | * @param Serializer $serializer |
||
68 | * @param CacheItemPoolInterface $cacheItemPool |
||
69 | * @param int $cacheTtl |
||
70 | * @param StopwatchInterface $stopwatch |
||
71 | */ |
||
72 | public function __construct( |
||
85 | |||
86 | /** |
||
87 | * @param CacheItemPoolInterface $cacheItemPool |
||
88 | */ |
||
89 | public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool) |
||
93 | |||
94 | public function disableCaching() |
||
98 | |||
99 | /** |
||
100 | * @return CacheItemPoolInterface |
||
101 | */ |
||
102 | public function getCacheItemPool() |
||
106 | |||
107 | /** |
||
108 | * Perform the actual request in the implementation classes. |
||
109 | * |
||
110 | * @param string $method |
||
111 | * @param string $uri |
||
112 | * @param array $options |
||
113 | * |
||
114 | * @return mixed |
||
115 | */ |
||
116 | abstract protected function _doRequest($method, $uri, $options); |
||
117 | |||
118 | /** |
||
119 | * Make a request to the API and serialize the result according to our |
||
120 | * serialization strategy. |
||
121 | * |
||
122 | * @param string $method |
||
123 | * @param string $uri |
||
124 | * @param array $options |
||
125 | * |
||
126 | * @return object|ResponseInterface |
||
127 | */ |
||
128 | protected function makeRequest($method, $uri, $options) |
||
169 | |||
170 | /** |
||
171 | * Deserialize a response into an instance of it's associated class. |
||
172 | * |
||
173 | * @param string $data |
||
174 | * @param string $serialisationClass |
||
175 | * |
||
176 | * @return object |
||
177 | */ |
||
178 | protected function deserialize($data, $serialisationClass) |
||
182 | |||
183 | /** |
||
184 | * Helper method to build the JSON data array for making a request |
||
185 | * with ::makeRequest(). Optional parameters with empty or null value will be |
||
186 | * omitted from the return value. |
||
187 | * |
||
188 | * Examples: |
||
189 | * |
||
190 | * $this->buildJsonParameters(['title' => 'test'], ['description' => '', 'bla' => 'test']) |
||
191 | * |
||
192 | * Would result in: |
||
193 | * |
||
194 | * [ |
||
195 | * 'title' => 'test', |
||
196 | * 'bla' => 'test', |
||
197 | * ] |
||
198 | * |
||
199 | * @param array $required |
||
200 | * @param array $optional |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | protected function buildJsonParameters(array $required, array $optional) |
||
222 | |||
223 | /** |
||
224 | * Generates the cache key based on the class name, request method, uri and options. |
||
225 | * |
||
226 | * @param string $method |
||
227 | * @param string $uri |
||
228 | * @param array $options |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | private function generateCacheKey($method, $uri, array $options = []) |
||
236 | |||
237 | /** |
||
238 | * Checks if the request may be cached. |
||
239 | * |
||
240 | * @param string $method |
||
241 | * @param string $uri |
||
242 | * @param array $options |
||
243 | * @param mixed $response |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | private function isCacheable($method, $uri, array $options, $response) |
||
270 | |||
271 | /** |
||
272 | * Serializes the provided response to a string, suitable for caching. |
||
273 | * The type of the $response argument varies depending on the guzzle version. |
||
274 | * |
||
275 | * @param mixed $response |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | abstract protected function serializeResponse($response); |
||
280 | |||
281 | /** |
||
282 | * Unserializes the serialized response into a response object. |
||
283 | * The return type varies depending on the guzzle version. |
||
284 | * |
||
285 | * @param string $serialized |
||
286 | * |
||
287 | * @return mixed |
||
288 | * |
||
289 | * @throws Exception |
||
290 | */ |
||
291 | abstract protected function unserializeResponse($serialized); |
||
292 | } |
||
293 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.