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 | * @see CacheItemInterface::expiresAfter() |
||
47 | */ |
||
48 | protected $cacheTtl; |
||
49 | |||
50 | /** |
||
51 | * ApiClient constructor. |
||
52 | * |
||
53 | * @param ClientInterface $httpClient |
||
54 | * @param Serializer $serializer |
||
55 | * @param CacheItemPoolInterface $cacheItemPool |
||
56 | * @param integer $cacheTtl |
||
57 | */ |
||
58 | public function __construct( |
||
69 | |||
70 | /** |
||
71 | * Perform the actual request in the implementation classes. |
||
72 | * |
||
73 | * @param string $method |
||
74 | * @param string $uri |
||
75 | * @param array $options |
||
76 | * |
||
77 | * @return mixed |
||
78 | */ |
||
79 | abstract protected function _doRequest($method, $uri, $options); |
||
80 | |||
81 | /** |
||
82 | * Make a request to the API and serialize the result according to our |
||
83 | * serialization strategy. |
||
84 | * |
||
85 | * @param string $method |
||
86 | * @param string $uri |
||
87 | * @param array $options |
||
88 | * |
||
89 | * @return object|ResponseInterface |
||
90 | */ |
||
91 | protected function makeRequest($method, $uri, $options) |
||
127 | |||
128 | /** |
||
129 | * Deserialize a response into an instance of it's associated class. |
||
130 | * |
||
131 | * @param string $data |
||
132 | * @param string $serialisationClass |
||
133 | * |
||
134 | * @return object |
||
135 | */ |
||
136 | protected function deserialize($data, $serialisationClass) |
||
140 | |||
141 | /** |
||
142 | * Helper method to build the JSON data array for making a request |
||
143 | * with ::makeRequest(). Optional parameters with empty or null value will be |
||
144 | * omitted from the return value. |
||
145 | * |
||
146 | * Examples: |
||
147 | * |
||
148 | * $this->buildJsonParameters(['title' => 'test'], ['description' => '', 'bla' => 'test']) |
||
149 | * |
||
150 | * Would result in: |
||
151 | * |
||
152 | * [ |
||
153 | * 'title' => 'test', |
||
154 | * 'bla' => 'test', |
||
155 | * ] |
||
156 | * |
||
157 | * @param array $required |
||
158 | * @param array $optional |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | protected function buildJsonParameters(array $required, array $optional) |
||
180 | |||
181 | /** |
||
182 | * Generates the cache key based on request method, uri and options. |
||
183 | * @param string $method |
||
184 | * @param string $uri |
||
185 | * @param array $options |
||
186 | * @return string |
||
187 | */ |
||
188 | private function generateCacheKey($method, $uri, array $options = []) |
||
192 | |||
193 | /** |
||
194 | * Serializes the provided response to a string, suitable for caching. |
||
195 | * The type of the $response argument varies depending on the guzzle version. |
||
196 | * @param mixed $response |
||
197 | * @return string |
||
198 | */ |
||
199 | abstract protected function serializeResponse($response); |
||
200 | |||
201 | /** |
||
202 | * Unserializes the serialized response into a response object. |
||
203 | * The return type varies depending on the guzzle version. |
||
204 | * @param string $serialized |
||
205 | * @return mixed |
||
206 | * @throws Exception |
||
207 | */ |
||
208 | abstract protected function unserializeResponse($serialized); |
||
209 | } |
||
210 |