1 | <?php |
||
17 | class GeoIPLocation |
||
18 | { |
||
19 | /** |
||
20 | * @var MockHandler |
||
21 | */ |
||
22 | private $mockHandler; |
||
23 | |||
24 | /** |
||
25 | * Set mock handler (Unit testing purpose) |
||
26 | * |
||
27 | * @param MockHandler $mockHandler Mock handler |
||
28 | * |
||
29 | * @return GeoIPLocation |
||
30 | */ |
||
31 | 5 | public function setMockHandler(MockHandler $mockHandler): GeoIPLocation |
|
37 | |||
38 | /** |
||
39 | * Indices for getting user IP Address |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $remotes = array( |
||
44 | 'REMOTE_ADDR', |
||
45 | 'HTTP_X_FORWARDED_FOR', |
||
46 | 'HTTP_CLIENT_IP', |
||
47 | 'HTTP_X_FORWARDED', |
||
48 | 'HTTP_FORWARDED_FOR', |
||
49 | 'HTTP_FORWARDED', |
||
50 | 'HTTP_X_CLUSTER_CLIENT_IP', |
||
51 | ); |
||
52 | |||
53 | /** |
||
54 | * Return geo location data based on the user's Ip Address |
||
55 | * |
||
56 | * Example: ip-address => {"status": "success", "country": "COUNTRY", "countryCode": "COUNTRY CODE", "region": "REGION CODE"} |
||
57 | * |
||
58 | * @return Location |
||
59 | */ |
||
60 | 5 | public function getGeoLocation() |
|
80 | |||
81 | /** |
||
82 | * Return Ip address of the user |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 5 | public function getIpAddress(): string |
|
100 | |||
101 | /** |
||
102 | * Check if ip address is valid or not |
||
103 | * |
||
104 | * @param string $ipAddress Ip Address |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | 5 | public function isIpAddressValid(string $ipAddress): bool |
|
112 | |||
113 | /** |
||
114 | * Return http client |
||
115 | * |
||
116 | * @return Client |
||
117 | */ |
||
118 | 5 | private function getHttpClient(): Client |
|
127 | |||
128 | /** |
||
129 | * Return request Url |
||
130 | * |
||
131 | * @param string $ipAddress Ip Address |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | 5 | private function getRequestUrl(string $ipAddress): string |
|
144 | |||
145 | /** |
||
146 | * Return options array for http request |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | 5 | private function getRequestOptions(): array |
|
158 | |||
159 | /** |
||
160 | * Return mapped Location Object |
||
161 | * |
||
162 | * @param ResponseInterface $response Response |
||
163 | * |
||
164 | * @return Location |
||
165 | */ |
||
166 | 3 | private function getMappedLocation(ResponseInterface $response): Location |
|
183 | |||
184 | /** |
||
185 | * Return status |
||
186 | * |
||
187 | * @param string $status Status string |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | 3 | private function getStatus(string $status): bool |
|
195 | |||
196 | /** |
||
197 | * Return response phrase with corresponding http status code |
||
198 | * |
||
199 | * @param ResponseInterface $response Response |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 1 | private function getResponsePhrase(ResponseInterface $response): string |
|
211 | |||
212 | /** |
||
213 | * Handle error |
||
214 | * |
||
215 | * @param string $message Message |
||
216 | * |
||
217 | * @return Location |
||
218 | */ |
||
219 | 2 | private function handleError(string $message): Location |
|
225 | |||
226 | /** |
||
227 | * Map object property |
||
228 | * |
||
229 | * @param \stdClass $object Object |
||
230 | * @param string $property Property |
||
231 | * |
||
232 | * @return null|string |
||
233 | */ |
||
234 | 3 | private function mapProperty(\stdClass $object, string $property): ?string |
|
242 | } |
||
243 |