|
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) { |
|
|
|
|
|
|
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
|
|
|
|
Scrutinizer analyzes your
composer.json/composer.lockfile 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.