| Conditions | 5 |
| Paths | 8 |
| Total Lines | 29 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 23 | public function geolocate($ip, $params = []) |
||
| 24 | { |
||
| 25 | $url = str_replace('{ip}', $ip, self::API_URL); |
||
| 26 | if (!empty($params)) { |
||
| 27 | $url .= '?' . http_build_query($params); |
||
| 28 | } |
||
| 29 | $result = file_get_contents($url); |
||
| 30 | if (!$result) { |
||
| 31 | throw new Exception("The api returned no result"); |
||
| 32 | } |
||
| 33 | |||
| 34 | $data = json_decode($result, true); |
||
| 35 | |||
| 36 | if (!$data) { |
||
| 37 | throw new Exception("Failed to decode api results"); |
||
| 38 | } |
||
| 39 | |||
| 40 | if ($data['status'] != 'success') { |
||
| 41 | throw new Exception("Api returned an error"); |
||
| 42 | } |
||
| 43 | |||
| 44 | $country = new Country($data['countryCode'], $data['country']); |
||
| 45 | $coordinates = new Coordinates($data['lat'], $data['lon']); |
||
| 46 | |||
| 47 | $addressData = [ |
||
| 48 | 'postalCode' => $data['zip'], |
||
| 49 | 'locality' => $data['city'], |
||
| 50 | ]; |
||
| 51 | return new Address($addressData, $country, $coordinates); |
||
| 52 | } |
||
| 54 |