Completed
Push — master ( 975260...d54b82 )
by Alejandro
05:50 queued 10s
created

IpApiLocationResolver::resolveIpLocation()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 3
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Service;
5
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\GuzzleException;
8
use Shlinkio\Shlink\Common\Exception\WrongIpException;
9
10
class IpApiLocationResolver implements IpLocationResolverInterface
11
{
12
    private const SERVICE_PATTERN = 'http://ip-api.com/json/%s';
13
14
    /**
15
     * @var Client
16
     */
17
    private $httpClient;
18
19 2
    public function __construct(Client $httpClient)
20
    {
21 2
        $this->httpClient = $httpClient;
22 2
    }
23
24
    /**
25
     * @param string $ipAddress
26
     * @return array
27
     * @throws WrongIpException
28
     */
29 2
    public function resolveIpLocation(string $ipAddress): array
30
    {
31
        try {
32 2
            $response = $this->httpClient->get(\sprintf(self::SERVICE_PATTERN, $ipAddress));
33 1
            return $this->mapFields(\json_decode((string) $response->getBody(), true));
34 1
        } catch (GuzzleException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\GuzzleException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
35 1
            throw WrongIpException::fromIpAddress($ipAddress, $e);
36
        }
37
    }
38
39 1
    private function mapFields(array $entry): array
40
    {
41
        return [
42 1
            'country_code' => $entry['countryCode'] ?? '',
43 1
            'country_name' => $entry['country'] ?? '',
44 1
            'region_name' => $entry['regionName'] ?? '',
45 1
            'city' => $entry['city'] ?? '',
46 1
            'latitude' => $entry['lat'] ?? '',
47 1
            'longitude' => $entry['lon'] ?? '',
48 1
            'time_zone' => $entry['timezone'] ?? '',
49
        ];
50
    }
51
}
52