| Total Complexity | 50 |
| Total Lines | 384 |
| 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 DEFAULT_GEOCODE_MODE = self::GEOCODE_MODE_FIND; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | const DEFAULT_FIELDS = 'formatted_address,geometry,icon,name,permanently_closed,photos,place_id,plus_code,types'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string|null |
||
| 77 | */ |
||
| 78 | private $apiKey; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param HttpClient $client An HTTP adapter |
||
| 82 | * @param string $apiKey Google Maps Places API Key |
||
| 83 | */ |
||
| 84 | public function __construct(HttpClient $client, string $apiKey) |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param GeocodeQuery $query |
||
| 93 | * |
||
| 94 | * @return Collection |
||
| 95 | * |
||
| 96 | * @throws UnsupportedOperation |
||
| 97 | * @throws InvalidArgument |
||
| 98 | */ |
||
| 99 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
| 100 | { |
||
| 101 | if (filter_var($query->getText(), FILTER_VALIDATE_IP)) { |
||
| 102 | throw new UnsupportedOperation('The GoogleMapsPlaces provider does not support IP addresses'); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (self::GEOCODE_MODE_FIND === $query->getData('mode', self::DEFAULT_GEOCODE_MODE)) { |
||
| 106 | return $this->fetchUrl(self::FIND_ENDPOINT_URL_SSL, $this->buildFindPlaceQuery($query)); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (self::GEOCODE_MODE_SEARCH === $query->getData('mode', self::DEFAULT_GEOCODE_MODE)) { |
||
| 110 | return $this->fetchUrl(self::SEARCH_ENDPOINT_URL_SSL, $this->buildPlaceSearchQuery($query)); |
||
| 111 | } |
||
| 112 | |||
| 113 | throw new InvalidArgument('Mode must be one of `%s, %s`', self::GEOCODE_MODE_FIND, self::GEOCODE_MODE_SEARCH); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param ReverseQuery $query |
||
| 118 | * |
||
| 119 | * @return Collection |
||
| 120 | * |
||
| 121 | * @throws InvalidArgument |
||
| 122 | */ |
||
| 123 | public function reverseQuery(ReverseQuery $query): Collection |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritdoc} |
||
| 130 | */ |
||
| 131 | public function getName(): string |
||
| 132 | { |
||
| 133 | return 'google_maps_places'; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Build query for the find place API |
||
| 138 | * |
||
| 139 | * @param GeocodeQuery $geocodeQuery |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | private function buildFindPlaceQuery(GeocodeQuery $geocodeQuery): array |
||
| 144 | { |
||
| 145 | $query = [ |
||
| 146 | 'input' => $geocodeQuery->getText(), |
||
| 147 | 'inputtype' => 'textquery', |
||
| 148 | 'fields' => self::DEFAULT_FIELDS, |
||
| 149 | ]; |
||
| 150 | |||
| 151 | if (null !== $geocodeQuery->getLocale()) { |
||
| 152 | $query['language'] = $geocodeQuery->getLocale(); |
||
| 153 | } |
||
| 154 | |||
| 155 | // If query has bounds, set location bias to those bounds |
||
| 156 | if (null !== $bounds = $geocodeQuery->getBounds()) { |
||
| 157 | $query['locationbias'] = sprintf( |
||
| 158 | 'rectangle:%s,%s|%s,%s', |
||
| 159 | $bounds->getSouth(), |
||
| 160 | $bounds->getWest(), |
||
| 161 | $bounds->getNorth(), |
||
| 162 | $bounds->getEast() |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | if (null !== $geocodeQuery->getData('fields')) { |
||
| 167 | $query['fields'] = $geocodeQuery->getData('fields'); |
||
| 168 | } |
||
| 169 | |||
| 170 | return $query; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Build query for the place search API |
||
| 175 | * |
||
| 176 | * @param GeocodeQuery $geocodeQuery |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | private function buildPlaceSearchQuery(GeocodeQuery $geocodeQuery): array |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Build query for the nearby search api |
||
| 208 | * |
||
| 209 | * @param ReverseQuery $reverseQuery |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | private function buildNearbySearchQuery(ReverseQuery $reverseQuery): array |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param Query $query |
||
| 251 | * @param array $request |
||
| 252 | * @param array $keys |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | private function applyDataFromQuery(Query $query, array $request, array $keys) |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $url |
||
| 271 | * @param array $query |
||
| 272 | * |
||
| 273 | * @return AddressCollection |
||
| 274 | */ |
||
| 275 | private function fetchUrl(string $url, array $query): AddressCollection |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Decode the response content and validate it to make sure it does not have any errors. |
||
| 367 | * |
||
| 368 | * @param string $url |
||
| 369 | * @param string $content |
||
| 370 | * |
||
| 371 | * @return \StdClass |
||
| 372 | * |
||
| 373 | * @throws InvalidCredentials |
||
| 374 | * @throws InvalidServerResponse |
||
| 375 | * @throws QuotaExceeded |
||
| 376 | */ |
||
| 377 | private function validateResponse(string $url, $content): StdClass |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Parse coordinates and bounds. |
||
| 407 | * |
||
| 408 | * @param AddressBuilder $builder |
||
| 409 | * @param StdClass $result |
||
| 410 | */ |
||
| 411 | private function parseCoordinates(AddressBuilder $builder, StdClass $result) |
||
| 426 |