Total Complexity | 60 |
Total Lines | 430 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
Complex classes like GoogleMapsPlaces often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GoogleMapsPlaces, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | final class GoogleMapsPlaces extends AbstractHttpProvider implements Provider |
||
39 | { |
||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | const SEARCH_ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/place/textsearch/json'; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | const FIND_ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json'; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | const NEARBY_ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json'; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | const GEOCODE_MODE_FIND = 'find'; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | const GEOCODE_MODE_SEARCH = 'search'; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | const GEOCODE_MODE_NEARBY = 'nearby'; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | const DEFAULT_GEOCODE_MODE = self::GEOCODE_MODE_FIND; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | const DEFAULT_FIELDS = 'formatted_address,geometry,icon,name,permanently_closed,photos,place_id,plus_code,types'; |
||
79 | |||
80 | /** |
||
81 | * @var string|null |
||
82 | */ |
||
83 | private $apiKey; |
||
84 | |||
85 | /** |
||
86 | * @param HttpClient $client An HTTP adapter |
||
87 | * @param string $apiKey Google Maps Places API Key |
||
88 | */ |
||
89 | public function __construct(HttpClient $client, string $apiKey) |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param GeocodeQuery $query |
||
98 | * |
||
99 | * @return Collection |
||
100 | * |
||
101 | * @throws UnsupportedOperation |
||
102 | * @throws InvalidArgument |
||
103 | */ |
||
104 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
105 | { |
||
106 | if (filter_var($query->getText(), FILTER_VALIDATE_IP)) { |
||
107 | throw new UnsupportedOperation('The GoogleMapsPlaces provider does not support IP addresses'); |
||
108 | } |
||
109 | |||
110 | if (self::GEOCODE_MODE_FIND === $query->getData('mode', self::DEFAULT_GEOCODE_MODE)) { |
||
111 | return $this->fetchUrl(self::FIND_ENDPOINT_URL_SSL, $this->buildFindPlaceQuery($query)); |
||
112 | } |
||
113 | |||
114 | if (self::GEOCODE_MODE_SEARCH === $query->getData('mode', self::DEFAULT_GEOCODE_MODE)) { |
||
115 | return $this->fetchUrl(self::SEARCH_ENDPOINT_URL_SSL, $this->buildPlaceSearchQuery($query)); |
||
116 | } |
||
117 | |||
118 | throw new InvalidArgument(sprintf('Mode must be one of `%s, %s`', self::GEOCODE_MODE_FIND, self::GEOCODE_MODE_SEARCH)); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param ReverseQuery $query |
||
123 | * |
||
124 | * @return Collection |
||
125 | * |
||
126 | * @throws InvalidArgument |
||
127 | */ |
||
128 | public function reverseQuery(ReverseQuery $query): Collection |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function getName(): string |
||
144 | { |
||
145 | return 'google_maps_places'; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Build query for the find place API. |
||
150 | * |
||
151 | * @param GeocodeQuery $geocodeQuery |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | private function buildFindPlaceQuery(GeocodeQuery $geocodeQuery): array |
||
156 | { |
||
157 | $query = [ |
||
158 | 'input' => $geocodeQuery->getText(), |
||
159 | 'inputtype' => 'textquery', |
||
160 | 'fields' => self::DEFAULT_FIELDS, |
||
161 | ]; |
||
162 | |||
163 | if (null !== $geocodeQuery->getLocale()) { |
||
164 | $query['language'] = $geocodeQuery->getLocale(); |
||
165 | } |
||
166 | |||
167 | // If query has bounds, set location bias to those bounds |
||
168 | if (null !== $bounds = $geocodeQuery->getBounds()) { |
||
169 | $query['locationbias'] = sprintf( |
||
170 | 'rectangle:%s,%s|%s,%s', |
||
171 | $bounds->getSouth(), |
||
172 | $bounds->getWest(), |
||
173 | $bounds->getNorth(), |
||
174 | $bounds->getEast() |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | if (null !== $geocodeQuery->getData('fields')) { |
||
179 | $query['fields'] = $geocodeQuery->getData('fields'); |
||
180 | } |
||
181 | |||
182 | return $query; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Build query for the place search API. |
||
187 | * |
||
188 | * @param GeocodeQuery $geocodeQuery |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | private function buildPlaceSearchQuery(GeocodeQuery $geocodeQuery): array |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Build query for the nearby search api. |
||
220 | * |
||
221 | * @param ReverseQuery $reverseQuery |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | private function buildNearbySearchQuery(ReverseQuery $reverseQuery): array |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param Query $query |
||
293 | * @param array $request |
||
294 | * @param array $keys |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | private function applyDataFromQuery(Query $query, array $request, array $keys) |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param string $url |
||
313 | * @param array $query |
||
314 | * |
||
315 | * @return AddressCollection |
||
316 | */ |
||
317 | private function fetchUrl(string $url, array $query): AddressCollection |
||
318 | { |
||
319 | $query['key'] = $this->apiKey; |
||
320 | |||
321 | $url = sprintf('%s?%s', $url, http_build_query($query)); |
||
322 | |||
323 | $content = $this->getUrlContents($url); |
||
324 | $json = $this->validateResponse($url, $content); |
||
325 | |||
326 | if (empty($json->candidates) && empty($json->results) || 'OK' !== $json->status) { |
||
327 | return new AddressCollection([]); |
||
328 | } |
||
329 | |||
330 | $results = []; |
||
331 | |||
332 | $apiResults = isset($json->results) ? $json->results : $json->candidates; |
||
333 | |||
334 | foreach ($apiResults as $result) { |
||
335 | $builder = new AddressBuilder($this->getName()); |
||
336 | $this->parseCoordinates($builder, $result); |
||
337 | |||
338 | if (isset($result->place_id)) { |
||
339 | $builder->setValue('id', $result->place_id); |
||
340 | } |
||
341 | |||
342 | /** @var GooglePlace $address */ |
||
343 | $address = $builder->build(GooglePlace::class); |
||
344 | $address = $address->withId($builder->getValue('id')); |
||
345 | |||
346 | if (isset($result->name)) { |
||
347 | $address = $address->withName($result->name); |
||
348 | } |
||
349 | |||
350 | if (isset($result->formatted_address)) { |
||
351 | $address = $address->withFormattedAddress($result->formatted_address); |
||
352 | } |
||
353 | |||
354 | if (isset($result->vicinity)) { |
||
355 | $address = $address->withVicinity($result->vicinity); |
||
356 | } |
||
357 | |||
358 | if (isset($result->types)) { |
||
359 | $address = $address->withType($result->types); |
||
360 | } |
||
361 | |||
362 | if (isset($result->icon)) { |
||
363 | $address = $address->withIcon($result->icon); |
||
364 | } |
||
365 | |||
366 | if (isset($result->plus_code)) { |
||
367 | $address = $address->withPlusCode(new PlusCode( |
||
368 | $result->plus_code->global_code, |
||
369 | $result->plus_code->compound_code |
||
370 | )); |
||
371 | } |
||
372 | |||
373 | if (isset($result->photos)) { |
||
374 | $address = $address->withPhotos(Photo::getPhotosFromResult($result->photos)); |
||
375 | } |
||
376 | |||
377 | if (isset($result->price_level)) { |
||
378 | $address = $address->withPriceLevel($result->price_level); |
||
379 | } |
||
380 | |||
381 | if (isset($result->rating)) { |
||
382 | $address = $address->withRating((float) $result->rating); |
||
383 | } |
||
384 | |||
385 | if (isset($result->formatted_phone_number)) { |
||
386 | $address = $address->withFormattedPhoneNumber($result->formatted_phone_number); |
||
387 | } |
||
388 | |||
389 | if (isset($result->international_phone_number)) { |
||
390 | $address = $address->withInternationalPhoneNumber($result->international_phone_number); |
||
391 | } |
||
392 | |||
393 | if (isset($result->website)) { |
||
394 | $address = $address->withWebsite($result->website); |
||
395 | } |
||
396 | |||
397 | if (isset($result->opening_hours)) { |
||
398 | $address = $address->withOpeningHours(OpeningHours::fromResult($result->opening_hours)); |
||
399 | } |
||
400 | |||
401 | if (isset($result->permanently_closed)) { |
||
402 | $address = $address->setPermanentlyClosed(); |
||
403 | } |
||
404 | |||
405 | $results[] = $address; |
||
406 | } |
||
407 | |||
408 | return new AddressCollection($results); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Decode the response content and validate it to make sure it does not have any errors. |
||
413 | * |
||
414 | * @param string $url |
||
415 | * @param string $content |
||
416 | * |
||
417 | * @return \StdClass |
||
418 | * |
||
419 | * @throws InvalidCredentials |
||
420 | * @throws InvalidServerResponse |
||
421 | * @throws QuotaExceeded |
||
422 | */ |
||
423 | private function validateResponse(string $url, $content): StdClass |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Parse coordinates and bounds. |
||
453 | * |
||
454 | * @param AddressBuilder $builder |
||
455 | * @param StdClass $result |
||
456 | */ |
||
457 | private function parseCoordinates(AddressBuilder $builder, StdClass $result) |
||
468 | ); |
||
469 | } |
||
470 | } |
||
471 | } |
||
472 |