Conditions | 6 |
Paths | 10 |
Total Lines | 22 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
16 | public function getCountryCode(string $ip): ?string |
||
17 | { |
||
18 | if ($ip === '') { |
||
19 | return null; |
||
20 | } |
||
21 | $urlService = 'http://www.geoplugin.net/php.gp?ip=' . $ip; |
||
22 | try { |
||
23 | $request = $this->requestFactory->createRequest('GET', $urlService); |
||
24 | $response = $this->getClient()->send($request); |
||
25 | |||
26 | if ($response->getStatusCode() !== 200) { |
||
27 | throw new IpLocationException('Missing information in response', 123781); |
||
28 | } |
||
29 | $result = (array)unserialize((string)$response->getBody(), ['allowed_classes' => false]); |
||
30 | |||
31 | if (empty($result) || (int)$result['geoplugin_status'] === 404) { |
||
32 | throw new IpLocationException('No valid data', 162378); |
||
33 | } |
||
34 | |||
35 | return $result['geoplugin_countryCode'] ?? null; |
||
36 | } catch (IpLocationException $exc) { |
||
37 | return null; |
||
38 | } |
||
49 |