/
GeoService.php
135 lines (128 loc) · 4.12 KB
/
GeoService.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Created by PhpStorm.
* User: mfrancis
* Date: 2018-06-10
* Time: 16:03
*/
namespace App\Service;
use App\Utils\Rxx;
use Exception;
use GeoIp2\Exception\AddressNotFoundException;
use GpsLab\Bundle\GeoIP2Bundle\Reader\ReaderFactory;
use MaxMind\Db\Reader\InvalidDatabaseException;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Class GeoService
* @package App\Service
*/
class GeoService
{
/**
* @var Visitor
*/
private $visitor;
private $reader;
private $dbPath;
/**
* GeoService constructor.
* @param \App\Service\Visitor $visitor
* @param ReaderFactory
*/
public function __construct(KernelInterface $kernel, Visitor $visitor, ReaderFactory $factory)
{
$this->visitor = $visitor;
$this->dbPath = $kernel->getCacheDir() . '/GeoLite2-City.mmdb';
try {
$this->reader = $factory->create('default');
} catch (Exception $e) {
$binPath = substr(dirname(__DIR__), 0, -4) . "/bin/";
die(
"<h1>GeoIP Error</h1>\n"
. "Please run this command to download the latest GeoIP Database:</p>"
. "<pre>{$binPath}console geoip2:update</pre>"
);
}
}
/**
* @return string|void|null
*/
public function getContinent()
{
$ip = $this->visitor->getIpAddress();
if (!$ip) {
return 'NA';
}
try {
$record = $this->reader->city($ip);
return $record->continent->code;
} catch (AddressNotFoundException $e) {
return 'NA';
} catch (InvalidDatabaseException $e) {
return 'NA';
}
}
/**
* @param $ip
* @return array
*/
public function getDetailsForIp($ip)
{
if (!$ip) {
return [ 'Result' => 'No IP address given' ];
}
try {
$record = $this->reader->city($ip);
return [
'IP' => $ip,
'City' => $record->city->name,
'Subdivision' => $record->mostSpecificSubdivision->name,
'ISOCode' => $record->mostSpecificSubdivision->isoCode,
'Country' => $record->country->name,
'Continent' => $record->continent->name,
'Postal' => $record->postal->code,
'Coords' => $record->location->latitude . ' / ' . $record->location->longitude,
'GSQ' => Rxx::convertDegreesToGSQ($record->location->latitude, $record->location->longitude),
'System' => strtoupper($this->getDefaultSystem()),
'GeoIP2DB' => $this->dbPath,
'GeoIP2Age' => date('Y-m-d H:i:s', filemtime($this->dbPath))
];
} catch (AddressNotFoundException $e) {
return [
'IP' => $ip,
'Result' => 'Address not found',
'GeoIP2DB' => $this->dbPath,
'GeoIP2Age' => date('Y-m-d H:i:s', filemtime($this->dbPath))
];
} catch (InvalidDatabaseException $e) {
return [
'IP' => $ip,
'Result' => 'Invalid Database',
'GeoIP2DB' => $this->dbPath,
'GeoIP2Age' => date('Y-m-d H:i:s', filemtime($this->dbPath))
];
} catch (Exception $e) {
return [
'IP' => $ip,
'Result' => 'Invalid Request',
'GeoIP2DB' => $this->dbPath,
'GeoIP2Age' => date('Y-m-d h:i:s', filemtime($this->dbPath))
];
}
}
/**
* @return string - reu|rna|rww - depending on where visitor's IP address originates
*/
public function getDefaultSystem()
{
$continent = $this->getContinent();
switch ($continent) {
case 'NA':
return 'rna';
case 'EU':
return 'reu';
default:
return 'rww';
}
}
}