1 | <?php |
||
16 | class GeoIPLocation |
||
17 | { |
||
18 | /** |
||
19 | * Indices for getting user IP Address |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $remotes = array( |
||
24 | 'REMOTE_ADDR', |
||
25 | 'HTTP_X_FORWARDED_FOR', |
||
26 | 'HTTP_CLIENT_IP', |
||
27 | 'HTTP_X_FORWARDED', |
||
28 | 'HTTP_FORWARDED_FOR', |
||
29 | 'HTTP_FORWARDED', |
||
30 | 'HTTP_X_CLUSTER_CLIENT_IP', |
||
31 | ); |
||
32 | |||
33 | /** |
||
34 | * Return geo location data based on the user's Ip Address |
||
35 | * |
||
36 | * Example: ip-address => {"status": "success", "country": "COUNTRY", "countryCode": "COUNTRY CODE", "region": "REGION CODE"} |
||
37 | * |
||
38 | * @return Location |
||
39 | */ |
||
40 | 5 | public function getGeoLocation() |
|
41 | { |
||
42 | try { |
||
43 | 5 | $response = (new Client())->get( |
|
44 | 5 | $this->getRequestUrl($this->getIpAddress()), |
|
45 | 5 | $this->getRequestOptions() |
|
46 | ); |
||
47 | |||
48 | 4 | if (\in_array($response->getStatusCode(), range(200, 299))) { |
|
49 | |||
50 | 3 | return $this->getMappedLocation($response); |
|
51 | } |
||
52 | |||
53 | 1 | return $this->handleError($this->getResponsePhrase($response)); |
|
54 | |||
55 | 1 | } catch (RequestException $e) { |
|
56 | |||
57 | 1 | return $this->handleError($e->getMessage()); |
|
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Return Ip address of the user |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | 5 | public function getIpAddress(): string |
|
80 | |||
81 | /** |
||
82 | * Check if ip address is valid or not |
||
83 | * |
||
84 | * @param string $ipAddress Ip Address |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | 5 | public function isIpAddressValid(string $ipAddress): bool |
|
96 | |||
97 | /** |
||
98 | * Return request Url |
||
99 | * |
||
100 | * Important: access modifier is public for unit testing purpose. |
||
101 | * |
||
102 | * @param string $ipAddress Ip Address |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 3 | public function getRequestUrl(string $ipAddress): string |
|
115 | |||
116 | /** |
||
117 | * Return options array for http request |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | 5 | private function getRequestOptions(): array |
|
129 | |||
130 | /** |
||
131 | * Return mapped Location Object |
||
132 | * |
||
133 | * @param ResponseInterface $response Response |
||
134 | * |
||
135 | * @return Location |
||
136 | */ |
||
137 | 3 | private function getMappedLocation(ResponseInterface $response): Location |
|
154 | |||
155 | /** |
||
156 | * Return status |
||
157 | * |
||
158 | * @param string $status Status string |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | 3 | private function getStatus(string $status): bool |
|
166 | |||
167 | /** |
||
168 | * Return response phrase with corresponding http status code |
||
169 | * |
||
170 | * @param ResponseInterface $response Response |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 1 | private function getResponsePhrase(ResponseInterface $response): string |
|
182 | |||
183 | /** |
||
184 | * Handle error |
||
185 | * |
||
186 | * @param string $message Message |
||
187 | * |
||
188 | * @return Location |
||
189 | */ |
||
190 | 2 | private function handleError(string $message): Location |
|
196 | |||
197 | /** |
||
198 | * Map object property |
||
199 | * |
||
200 | * @param \stdClass $object Object |
||
201 | * @param string $property Property |
||
202 | * |
||
203 | * @return null|string |
||
204 | */ |
||
205 | 3 | private function mapProperty(\stdClass $object, string $property): ?string |
|
213 | } |
||
214 |