1 | <?php |
||
21 | class Client |
||
22 | { |
||
23 | const BASE_URL = 'https://api.yproximite.fr'; |
||
24 | |||
25 | /** |
||
26 | * @var string|null |
||
27 | */ |
||
28 | private $baseUrl; |
||
29 | |||
30 | /** |
||
31 | * @var string|null |
||
32 | */ |
||
33 | private $apiKey; |
||
34 | |||
35 | /** |
||
36 | * @var HttpClient |
||
37 | */ |
||
38 | private $httpClient; |
||
39 | |||
40 | /** |
||
41 | * @var MessageFactory|null |
||
42 | */ |
||
43 | private $messageFactory; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $apiToken; |
||
49 | |||
50 | /** |
||
51 | * Used to determine if token was used. In some cases the token could be invalidated during the usage of the API. |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | private $apiTokenFresh = true; |
||
56 | |||
57 | /** |
||
58 | * Client constructor. |
||
59 | * |
||
60 | * @param HttpClient $httpClient |
||
61 | * @param string|null $apiKey |
||
62 | * @param string|null $baseUrl |
||
63 | * @param MessageFactory|null $messageFactory |
||
64 | * |
||
65 | * @throws LogicException |
||
66 | */ |
||
67 | public function __construct( |
||
82 | |||
83 | /** |
||
84 | * @param null|string $baseUrl |
||
85 | */ |
||
86 | public function setBaseUrl(string $baseUrl = null) |
||
90 | |||
91 | /** |
||
92 | * @param null|string $apiKey |
||
93 | */ |
||
94 | public function setApiKey(string $apiKey = null) |
||
100 | |||
101 | /** |
||
102 | * Sends a request |
||
103 | * |
||
104 | * @param string $method |
||
105 | * @param string $path |
||
106 | * @param array|resource|string|StreamInterface|null $body |
||
107 | * @param bool $withAuthorization |
||
108 | * |
||
109 | * @return array|null |
||
110 | * @throws InvalidResponseException |
||
111 | */ |
||
112 | public function sendRequest(string $method, string $path, $body = null, bool $withAuthorization = true) |
||
113 | { |
||
114 | $uri = $this->getSafeBaseUrl(); |
||
115 | $uri .= $path; |
||
116 | |||
117 | $rawData = is_array($body) ? http_build_query($body) : $body; |
||
118 | $body = null; |
||
119 | |||
120 | if (in_array($method, $this->getQueryMethods())) { |
||
121 | if (is_string($rawData) && $rawData !== '') { |
||
122 | $uri .= '?'.$rawData; |
||
123 | } |
||
124 | } else { |
||
125 | $body = $rawData; |
||
126 | } |
||
127 | |||
128 | $content = $withAuthorization |
||
129 | ? $this->sendRequestWithAuthorization($method, $uri, $body) |
||
130 | : $this->doSendRequest($method, $uri, $body, false) |
||
131 | ; |
||
132 | |||
133 | if (empty($content)) { |
||
134 | return null; |
||
135 | } |
||
136 | |||
137 | $data = json_decode($content, true); |
||
138 | |||
139 | if (json_last_error() !== JSON_ERROR_NONE) { |
||
140 | throw new InvalidResponseException(sprintf('Could not decode the response of "%s %s".', $method, $path)); |
||
141 | } |
||
142 | |||
143 | return $data; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | private function getSafeBaseUrl(): string |
||
153 | |||
154 | /** |
||
155 | * Sends a request with authorization and tries to renew the api token in case of error of authentication. |
||
156 | * |
||
157 | * @param string $method |
||
158 | * @param string $uri |
||
159 | * @param resource|string|StreamInterface|null $body |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | private function sendRequestWithAuthorization(string $method, string $uri, $body): string |
||
179 | |||
180 | /** |
||
181 | * @param string $method |
||
182 | * @param string $uri |
||
183 | * @param resource|string|StreamInterface|null $body |
||
184 | * @param bool $withAuthorization |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | private function doSendRequest(string $method, string $uri, $body, bool $withAuthorization = true): string |
||
210 | |||
211 | /** |
||
212 | * @return HttpClient |
||
213 | */ |
||
214 | private function getHttpClient(): HttpClient |
||
218 | |||
219 | /** |
||
220 | * @return MessageFactory |
||
221 | */ |
||
222 | private function getMessageFactory(): MessageFactory |
||
230 | |||
231 | /** |
||
232 | * Returns all methods that uses query string to transfer a request data |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | private function getQueryMethods(): array |
||
240 | |||
241 | /** |
||
242 | * @return string |
||
243 | * @throws LogicException |
||
244 | */ |
||
245 | private function getApiToken(): string |
||
255 | |||
256 | private function updateApiToken() |
||
274 | |||
275 | private function resetApiToken() |
||
280 | |||
281 | /** |
||
282 | * @return string |
||
283 | */ |
||
284 | private function getAuthorizationHeader(): string |
||
288 | } |
||
289 |