1 | <?php |
||
16 | abstract class AbstractCoreApiClient implements LoggerAwareInterface |
||
17 | { |
||
18 | use LoggerAwareTrait; |
||
19 | |||
20 | protected const OPT_VIDEO_MANAGER_ID = 'videoManagerId'; |
||
21 | |||
22 | /** |
||
23 | * List of endpoints that may be cached, even though they use POST. |
||
24 | */ |
||
25 | protected const CACHEABLE_POST_ENDPOINTS = ['search']; |
||
26 | |||
27 | /** |
||
28 | * @var ClientInterface The Guzzle HTTP client |
||
29 | */ |
||
30 | protected $httpClient; |
||
31 | |||
32 | /** |
||
33 | * @var Serializer The JMS Serializer instance |
||
34 | */ |
||
35 | protected $serializer; |
||
36 | |||
37 | /** |
||
38 | * @var CacheItemPoolInterface PSR6 cache pool implementation |
||
39 | */ |
||
40 | protected $cacheItemPool; |
||
41 | |||
42 | /** |
||
43 | * @var mixed time-to-live for cached responses |
||
44 | * The type of this property might be integer, \DateInterval or null |
||
45 | * |
||
46 | * @see CacheItemInterface::expiresAfter() |
||
47 | */ |
||
48 | protected $cacheTtl; |
||
49 | |||
50 | public function __construct( |
||
61 | |||
62 | public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool): void |
||
66 | |||
67 | public function getCacheItemPool(): CacheItemPoolInterface |
||
71 | |||
72 | /** |
||
73 | * Perform the actual request in the implementation classes. |
||
74 | * |
||
75 | * @return mixed |
||
76 | */ |
||
77 | abstract protected function _doRequest(string $method, string $uri, array $options); |
||
78 | |||
79 | /** |
||
80 | * Make a request to the API and serialize the result according to our |
||
81 | * serialization strategy. |
||
82 | * |
||
83 | * @return object|ResponseInterface |
||
84 | * @throws \Exception |
||
85 | */ |
||
86 | protected function makeRequest(string $method, string $uri, array $options) |
||
124 | |||
125 | /** |
||
126 | * Deserialize a response into an instance of it's associated class. |
||
127 | * |
||
128 | * @return object |
||
129 | */ |
||
130 | protected function deserialize(string $data, string $serialisationClass) |
||
134 | |||
135 | /** |
||
136 | * Helper method to build the JSON data array for making a request |
||
137 | * with ::makeRequest(). Optional parameters with empty or null value will be |
||
138 | * omitted from the return value. |
||
139 | * |
||
140 | * Examples: |
||
141 | * |
||
142 | * $this->buildJsonParameters(['title' => 'test'], ['description' => '', 'bla' => 'test']) |
||
143 | * |
||
144 | * Would result in: |
||
145 | * |
||
146 | * [ |
||
147 | * 'title' => 'test', |
||
148 | * 'bla' => 'test', |
||
149 | * ] |
||
150 | * |
||
151 | * @throws Exception |
||
152 | */ |
||
153 | protected function buildJsonParameters(array $required, array $optional): array |
||
171 | |||
172 | /** |
||
173 | * Generates the cache key based on the class name, request method, uri and options. |
||
174 | */ |
||
175 | private function generateCacheKey(string $method, string $uri, array $options): string |
||
179 | |||
180 | /** |
||
181 | * Checks if the request may be cached. |
||
182 | */ |
||
183 | private function isCacheable(string $method, string $uri, array $options, $response): bool |
||
206 | |||
207 | /** |
||
208 | * Serializes the provided response to a string, suitable for caching. |
||
209 | * The type of the $response argument varies depending on the guzzle version. |
||
210 | * |
||
211 | * @param mixed $response |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | abstract protected function serializeResponse($response); |
||
216 | |||
217 | /** |
||
218 | * Unserializes the serialized response into a response object. |
||
219 | * The return type varies depending on the guzzle version. |
||
220 | * |
||
221 | * @param string $serialized |
||
222 | * |
||
223 | * @return mixed |
||
224 | * |
||
225 | * @throws Exception |
||
226 | */ |
||
227 | abstract protected function unserializeResponse($serialized); |
||
228 | } |
||
229 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.