1 | <?php |
||
20 | abstract class AbstractCoreApiClient implements LoggerAwareInterface |
||
21 | { |
||
22 | use LoggerAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * @const string |
||
26 | */ |
||
27 | const OPT_VIDEO_MANAGER_ID = 'videoManagerId'; |
||
28 | |||
29 | /** |
||
30 | * @var ClientInterface The Guzzle HTTP client |
||
31 | */ |
||
32 | protected $httpClient; |
||
33 | |||
34 | /** |
||
35 | * @var Serializer The JMS Serializer instance |
||
36 | */ |
||
37 | protected $serializer; |
||
38 | |||
39 | /** |
||
40 | * @var CacheItemPoolInterface PSR6 cache pool implementation |
||
41 | */ |
||
42 | protected $cacheItemPool; |
||
43 | |||
44 | /** |
||
45 | * @var mixed time-to-live for cached responses |
||
46 | * The type of this property might be integer, \DateInterval or null. |
||
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 integer $cacheTtl |
||
58 | */ |
||
59 | public function __construct( |
||
70 | |||
71 | /** |
||
72 | * Perform the actual request in the implementation classes. |
||
73 | * |
||
74 | * @param string $method |
||
75 | * @param string $uri |
||
76 | * @param array $options |
||
77 | * |
||
78 | * @return mixed |
||
79 | */ |
||
80 | abstract protected function _doRequest($method, $uri, $options); |
||
81 | |||
82 | /** |
||
83 | * Make a request to the API and serialize the result according to our |
||
84 | * serialization strategy. |
||
85 | * |
||
86 | * @param string $method |
||
87 | * @param string $uri |
||
88 | * @param array $options |
||
89 | * |
||
90 | * @return object|ResponseInterface |
||
91 | */ |
||
92 | protected function makeRequest($method, $uri, $options) |
||
128 | |||
129 | /** |
||
130 | * Deserialize a response into an instance of it's associated class. |
||
131 | * |
||
132 | * @param string $data |
||
133 | * @param string $serialisationClass |
||
134 | * |
||
135 | * @return object |
||
136 | */ |
||
137 | protected function deserialize($data, $serialisationClass) |
||
141 | |||
142 | /** |
||
143 | * Helper method to build the JSON data array for making a request |
||
144 | * with ::makeRequest(). Optional parameters with empty or null value will be |
||
145 | * omitted from the return value. |
||
146 | * |
||
147 | * Examples: |
||
148 | * |
||
149 | * $this->buildJsonParameters(['title' => 'test'], ['description' => '', 'bla' => 'test']) |
||
150 | * |
||
151 | * Would result in: |
||
152 | * |
||
153 | * [ |
||
154 | * 'title' => 'test', |
||
155 | * 'bla' => 'test', |
||
156 | * ] |
||
157 | * |
||
158 | * @param array $required |
||
159 | * @param array $optional |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | protected function buildJsonParameters(array $required, array $optional) |
||
181 | |||
182 | /** |
||
183 | * Generates the cache key based on request method, uri and options. |
||
184 | * @param string $method |
||
185 | * @param string $uri |
||
186 | * @param array $options |
||
187 | * @return string |
||
188 | */ |
||
189 | private function generateCacheKey(string $method, string $uri, array $options = []) |
||
193 | |||
194 | /** |
||
195 | * Serializes the provided response to a string, suitable for caching. |
||
196 | * @param ResponseInterface $response |
||
197 | * @return string |
||
198 | */ |
||
199 | private function serializeResponse(ResponseInterface $response) |
||
212 | |||
213 | /** |
||
214 | * Unserializes the serialized response into a ResponseInterface instance |
||
215 | * @param string $serialized |
||
216 | * @return ResponseInterface |
||
217 | * @throws Exception |
||
218 | */ |
||
219 | private function unserializeResponse(string $serialized): ResponseInterface |
||
229 | |||
230 | } |
||
231 |