@@ -181,33 +181,7 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string
181181 {
182182 $ url = $ this ->buildQuery ($ url , $ locale , $ region );
183183 $ content = $ this ->getUrlContents ($ url );
184-
185- // Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider
186- if (strpos ($ content , "Provided 'signature' is not valid for the provided client ID " ) !== false ) {
187- throw new InvalidCredentials (sprintf ('Invalid client ID / API Key %s ' , $ url ));
188- }
189-
190- $ json = json_decode ($ content );
191-
192- // API error
193- if (!isset ($ json )) {
194- throw InvalidServerResponse::create ($ url );
195- }
196-
197- if ('REQUEST_DENIED ' === $ json ->status && 'The provided API key is invalid. ' === $ json ->error_message ) {
198- throw new InvalidCredentials (sprintf ('API key is invalid %s ' , $ url ));
199- }
200-
201- if ('REQUEST_DENIED ' === $ json ->status ) {
202- throw new InvalidServerResponse (
203- sprintf ('API access denied. Request: %s - Message: %s ' , $ url , $ json ->error_message )
204- );
205- }
206-
207- // you are over your quota
208- if ('OVER_QUERY_LIMIT ' === $ json ->status ) {
209- throw new QuotaExceeded (sprintf ('Daily quota exceeded %s ' , $ url ));
210- }
184+ $ json = $ this ->validateResponse ($ url , $ content );
211185
212186 // no result
213187 if (!isset ($ json ->results ) || !count ($ json ->results ) || 'OK ' !== $ json ->status ) {
@@ -217,6 +191,7 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string
217191 $ results = [];
218192 foreach ($ json ->results as $ result ) {
219193 $ builder = new AddressBuilder ($ this ->getName ());
194+ $ this ->parseCoordinates ($ builder , $ result );
220195
221196 // update address components
222197 foreach ($ result ->address_components as $ component ) {
@@ -225,34 +200,6 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string
225200 }
226201 }
227202
228- // update coordinates
229- $ coordinates = $ result ->geometry ->location ;
230- $ builder ->setCoordinates ($ coordinates ->lat , $ coordinates ->lng );
231-
232- if (isset ($ result ->geometry ->bounds )) {
233- $ builder ->setBounds (
234- $ result ->geometry ->bounds ->southwest ->lat ,
235- $ result ->geometry ->bounds ->southwest ->lng ,
236- $ result ->geometry ->bounds ->northeast ->lat ,
237- $ result ->geometry ->bounds ->northeast ->lng
238- );
239- } elseif (isset ($ result ->geometry ->viewport )) {
240- $ builder ->setBounds (
241- $ result ->geometry ->viewport ->southwest ->lat ,
242- $ result ->geometry ->viewport ->southwest ->lng ,
243- $ result ->geometry ->viewport ->northeast ->lat ,
244- $ result ->geometry ->viewport ->northeast ->lng
245- );
246- } elseif ('ROOFTOP ' === $ result ->geometry ->location_type ) {
247- // Fake bounds
248- $ builder ->setBounds (
249- $ coordinates ->lat ,
250- $ coordinates ->lng ,
251- $ coordinates ->lat ,
252- $ coordinates ->lng
253- );
254- }
255-
256203 /** @var GoogleAddress $address */
257204 $ address = $ builder ->build (GoogleAddress::class);
258205 if (isset ($ result ->geometry ->location_type )) {
@@ -377,4 +324,84 @@ private function signQuery(string $query): string
377324
378325 return sprintf ('%s&signature=%s ' , $ query , $ encodedSignature );
379326 }
327+
328+ /**
329+ * Decode the response content and validate it to make sure it does not have any errors.
330+ *
331+ * @param string $url
332+ * @param string $content
333+ *
334+ * @return mixed result form json_decode()
335+ *
336+ * @throws InvalidCredentials
337+ * @throws InvalidServerResponse
338+ * @throws QuotaExceeded
339+ */
340+ private function validateResponse (string $ url , $ content )
341+ {
342+ // Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider
343+ if (strpos ($ content , "Provided 'signature' is not valid for the provided client ID " ) !== false ) {
344+ throw new InvalidCredentials (sprintf ('Invalid client ID / API Key %s ' , $ url ));
345+ }
346+
347+ $ json = json_decode ($ content );
348+
349+ // API error
350+ if (!isset ($ json )) {
351+ throw InvalidServerResponse::create ($ url );
352+ }
353+
354+ if ('REQUEST_DENIED ' === $ json ->status && 'The provided API key is invalid. ' === $ json ->error_message ) {
355+ throw new InvalidCredentials (sprintf ('API key is invalid %s ' , $ url ));
356+ }
357+
358+ if ('REQUEST_DENIED ' === $ json ->status ) {
359+ throw new InvalidServerResponse (
360+ sprintf ('API access denied. Request: %s - Message: %s ' , $ url , $ json ->error_message )
361+ );
362+ }
363+
364+ // you are over your quota
365+ if ('OVER_QUERY_LIMIT ' === $ json ->status ) {
366+ throw new QuotaExceeded (sprintf ('Daily quota exceeded %s ' , $ url ));
367+ }
368+
369+ return $ json ;
370+ }
371+
372+ /**
373+ * Parse coordinats and bounds.
374+ *
375+ * @param AddressBuilder $builder
376+ * @param $result
377+ */
378+ private function parseCoordinates (AddressBuilder $ builder , $ result )
379+ {
380+ $ coordinates = $ result ->geometry ->location ;
381+ $ builder ->setCoordinates ($ coordinates ->lat , $ coordinates ->lng );
382+
383+ if (isset ($ result ->geometry ->bounds )) {
384+ $ builder ->setBounds (
385+ $ result ->geometry ->bounds ->southwest ->lat ,
386+ $ result ->geometry ->bounds ->southwest ->lng ,
387+ $ result ->geometry ->bounds ->northeast ->lat ,
388+ $ result ->geometry ->bounds ->northeast ->lng
389+ );
390+ } elseif (isset ($ result ->geometry ->viewport )) {
391+ $ builder ->setBounds (
392+ $ result ->geometry ->viewport ->southwest ->lat ,
393+ $ result ->geometry ->viewport ->southwest ->lng ,
394+ $ result ->geometry ->viewport ->northeast ->lat ,
395+ $ result ->geometry ->viewport ->northeast ->lng
396+ );
397+ } elseif ('ROOFTOP ' === $ result ->geometry ->location_type ) {
398+ // Fake bounds
399+ $ builder ->setBounds (
400+ $ coordinates ->lat ,
401+ $ coordinates ->lng ,
402+ $ coordinates ->lat ,
403+ $ coordinates ->lng
404+ );
405+ }
406+ }
380407}
0 commit comments