Passed
Pull Request — master (#258)
by Alejandro
03:13
created

IpApiLocationResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\IpGeolocation;
5
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\GuzzleException;
8
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
9
use Shlinkio\Shlink\Common\Exception\WrongIpException;
10
use function Shlinkio\Shlink\Common\json_decode;
11
use function sprintf;
12
13
class IpApiLocationResolver implements IpLocationResolverInterface
14
{
15
    private const SERVICE_PATTERN = 'http://ip-api.com/json/%s';
16
17
    /**
18
     * @var Client
19
     */
20
    private $httpClient;
21
22 2
    public function __construct(Client $httpClient)
23
    {
24 2
        $this->httpClient = $httpClient;
25
    }
26
27
    /**
28
     * @param string $ipAddress
29
     * @return array
30
     * @throws WrongIpException
31
     */
32 2
    public function resolveIpLocation(string $ipAddress): array
33
    {
34
        try {
35 2
            $response = $this->httpClient->get(sprintf(self::SERVICE_PATTERN, $ipAddress));
36 1
            return $this->mapFields(json_decode((string) $response->getBody()));
37 1
        } catch (GuzzleException $e) {
38 1
            throw WrongIpException::fromIpAddress($ipAddress, $e);
39
        } catch (InvalidArgumentException $e) {
40
            throw new WrongIpException('IP-API returned invalid body while locating IP address', 0, $e);
41
        }
42
    }
43
44 1
    private function mapFields(array $entry): array
45
    {
46
        return [
47 1
            'country_code' => $entry['countryCode'] ?? '',
48 1
            'country_name' => $entry['country'] ?? '',
49 1
            'region_name' => $entry['regionName'] ?? '',
50 1
            'city' => $entry['city'] ?? '',
51 1
            'latitude' => $entry['lat'] ?? '',
52 1
            'longitude' => $entry['lon'] ?? '',
53 1
            'time_zone' => $entry['timezone'] ?? '',
54
        ];
55
    }
56
}
57