Completed
Push — master ( 3a75ac...d68dc3 )
by Alejandro
23s
created

IpApiLocationResolver::getApiLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
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\Service;
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 4
    public function __construct(Client $httpClient)
23
    {
24 4
        $this->httpClient = $httpClient;
25 4
    }
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) {
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...
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
    /**
58
     * Returns the interval in seconds that needs to be waited when the API limit is reached
59
     *
60
     * @return int
61
     */
62 1
    public function getApiInterval(): int
63
    {
64 1
        return 65; // ip-api interval is 1 minute. Return 5 extra seconds just in case
65
    }
66
67
    /**
68
     * Returns the limit of requests that can be performed to the API in a specific interval, or null if no limit exists
69
     *
70
     * @return int|null
71
     */
72 1
    public function getApiLimit(): ?int
73
    {
74 1
        return 145; // ip-api limit is 150 requests per minute. Leave 5 less requests just in case
75
    }
76
}
77