Completed
Pull Request — master (#176)
by Alejandro
03:36
created

IpApiLocationResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveIpLocation() 0 9 2
A mapFields() 0 12 1
A getApiInterval() 0 4 1
A getApiLimit() 0 4 1
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 4
    public function __construct(Client $httpClient)
20
    {
21 4
        $this->httpClient = $httpClient;
22 4
    }
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
    /**
53
     * Returns the interval in seconds that needs to be waited when the API limit is reached
54
     *
55
     * @return int
56
     */
57 1
    public function getApiInterval(): int
58
    {
59 1
        return 65; // ip-api interval is 1 minute. Return 5 extra seconds just in case
60
    }
61
62
    /**
63
     * Returns the limit of requests that can be performed to the API in a specific interval, or null if no limit exists
64
     *
65
     * @return int|null
66
     */
67 1
    public function getApiLimit(): ?int
68
    {
69 1
        return 145; // ip-api limit is 150 requests per minute. Leave 5 less requests just in case
70
    }
71
}
72