-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpInfoService.php
58 lines (50 loc) · 1.43 KB
/
IpInfoService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* Copyright notice:
* This file is part of a private project.
*/
namespace App\Service;
use App\Converter\SypexGeoConverter;
use App\Entity\IpRequestObject;
use App\Model\IpGeoDataDTO;
use HostBrook\SypexGeo\SypexGeo;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Uses https://packagist.org/packages/ipinfo/ipinfo to retrieve
*/
class IpInfoService
{
/**
* @var SypexGeo
*/
protected SypexGeo $geoReader;
public function __construct(private ValidatorInterface $validator)
{
$this->geoReader = new SypexGeo('SxGeoCity.dat');
}
public function validateRequestedIp(mixed $ip): ConstraintViolationListInterface
{
$requestedIp = new IpRequestObject();
$requestedIp->setIp($ip);
return $this->validator->validate($requestedIp);
}
private function toDto(string $ip, $geoData): IpGeoDataDTO
{
$ipGeoData = new IpGeoDataDTO($ip);
if (is_array($geoData['city'])) {
$ipGeoData->setCity($geoData['city']['name_en'] ?? '');
$ipGeoData->setCountry($geoData['country']['iso'] ?? '');
}
return $ipGeoData;
}
/**
* @param string $ip
*
* @return IpGeoDataDTO
*/
public function getGeoInformation(string $ip): IpGeoDataDTO
{
return $this->toDto($ip, $this->geoReader->getCity($ip));
}
}