| Conditions | 22 |
| Paths | 14 |
| Total Lines | 89 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 119 | private function executeQuery(string $url): AddressCollection |
||
| 120 | { |
||
| 121 | $content = $this->getUrlContents($url); |
||
| 122 | $json = json_decode($content, true); |
||
| 123 | |||
| 124 | if (isset($json['error'])) { |
||
| 125 | switch ($json['error']['code']) { |
||
| 126 | case static::CODE_BAD_IP: |
||
| 127 | throw new InvalidArgument('The API call should include a valid IP address.'); |
||
| 128 | case static::CODE_BAD_KEY: |
||
| 129 | throw new InvalidCredentials('The API call should include a API key parameter.'); |
||
| 130 | case static::CODE_NOT_AUTHORIZED: |
||
| 131 | throw new InvalidCredentials('The API key associated with your request was not recognized.'); |
||
| 132 | case static::CODE_ACCOUNT_INACTIVE: |
||
| 133 | throw new InvalidCredentials('The API key has not been approved or has been disabled.'); |
||
| 134 | case static::CODE_LIMIT_EXCEEDED: |
||
| 135 | throw new QuotaExceeded('The service you have requested is over capacity.'); |
||
| 136 | default: |
||
| 137 | throw new InvalidServerResponse(sprintf( |
||
| 138 | 'GeoIPs error %s%s%s%s - query: %s', |
||
| 139 | $json['error']['code'], |
||
| 140 | isset($json['error']['status']) ? ', '.$json['error']['status'] : '', |
||
| 141 | isset($json['error']['message']) ? ', '.$json['error']['message'] : '', |
||
| 142 | isset($json['error']['notes']) ? ', '.$json['error']['notes'] : '', |
||
| 143 | $url |
||
| 144 | )); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | if (!is_array($json) || empty($json) || empty($json['response']) || empty($json['response']['code'])) { |
||
| 149 | throw InvalidServerResponse::create($url); |
||
| 150 | } |
||
| 151 | |||
| 152 | $response = $json['response']; |
||
| 153 | |||
| 154 | // Check response code |
||
| 155 | switch ($response['code']) { |
||
| 156 | case static::CODE_SUCCESS: |
||
| 157 | // everything is ok |
||
| 158 | break; |
||
| 159 | case static::CODE_NOT_FOUND: |
||
| 160 | return new AddressCollection([]); |
||
| 161 | default: |
||
| 162 | throw new InvalidServerResponse(sprintf( |
||
| 163 | 'The GeoIPs API returned unknown result code "%s" for query: "%s".', |
||
| 164 | $response['code'], |
||
| 165 | $url |
||
| 166 | )); |
||
| 167 | } |
||
| 168 | |||
| 169 | // Make sure that we do have proper result array |
||
| 170 | if (empty($response['location']) || !is_array($response['location'])) { |
||
| 171 | return new AddressCollection([]); |
||
| 172 | } |
||
| 173 | |||
| 174 | $location = array_map(function ($value) { |
||
| 175 | return '' === $value ? null : $value; |
||
| 176 | }, $response['location']); |
||
| 177 | |||
| 178 | $adminLevels = []; |
||
| 179 | |||
| 180 | if (null !== $location['region_name'] || null !== $location['region_code']) { |
||
| 181 | $adminLevels[] = [ |
||
| 182 | 'name' => $location['region_name'], |
||
| 183 | 'code' => $location['region_code'], |
||
| 184 | 'level' => 1, |
||
| 185 | ]; |
||
| 186 | } |
||
| 187 | |||
| 188 | if (null !== $location['county_name']) { |
||
| 189 | $adminLevels[] = [ |
||
| 190 | 'name' => $location['county_name'], |
||
| 191 | 'level' => 2, |
||
| 192 | ]; |
||
| 193 | } |
||
| 194 | |||
| 195 | $results = Address::createFromArray([ |
||
| 196 | 'providedBy' => $this->getName(), |
||
| 197 | 'country' => $location['country_name'], |
||
| 198 | 'countryCode' => $location['country_code'], |
||
| 199 | 'adminLevels' => $adminLevels, |
||
| 200 | 'locality' => $location['city_name'], |
||
| 201 | 'latitude' => $location['latitude'], |
||
| 202 | 'longitude' => $location['longitude'], |
||
| 203 | 'timezone' => $location['timezone'], |
||
| 204 | ]); |
||
| 205 | |||
| 206 | return new AddressCollection([$results]); |
||
| 207 | } |
||
| 208 | } |
||
| 209 |