1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stevebauman\Location\Drivers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GeoIp2\Database\Reader; |
7
|
|
|
use GeoIp2\WebService\Client; |
8
|
|
|
use Illuminate\Support\Fluent; |
9
|
|
|
use Stevebauman\Location\Position; |
10
|
|
|
|
11
|
|
|
class MaxMind extends Driver |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
public function url($ip) |
17
|
|
|
{ |
18
|
|
|
return; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
protected function hydrate(Position $position, Fluent $location) |
25
|
|
|
{ |
26
|
|
|
$position->countryName = $location->country; |
27
|
|
|
$position->countryCode = $location->country_code; |
28
|
|
|
$position->isoCode = $location->country_code; |
29
|
|
|
$position->cityName = $location->city; |
30
|
|
|
$position->postalCode = $location->postal; |
31
|
|
|
$position->metroCode = $location->metro_code; |
32
|
|
|
$position->latitude = $location->latitude; |
33
|
|
|
$position->longitude = $location->longitude; |
34
|
|
|
|
35
|
|
|
return $position; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
protected function process($ip) |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
$record = $this->fetchLocation($ip); |
45
|
|
|
|
46
|
|
|
return new Fluent([ |
47
|
|
|
'country' => $record->country->name, |
48
|
|
|
'country_code' => $record->country->isoCode, |
49
|
|
|
'city' => $record->city->name, |
50
|
|
|
'postal' => $record->postal->code, |
51
|
|
|
'latitude' => (string) $record->location->latitude, |
52
|
|
|
'longitude' => (string) $record->location->longitude, |
53
|
|
|
'metro_code' => (string) $record->location->metroCode, |
54
|
|
|
]); |
55
|
|
|
} catch (Exception $e) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Attempt to fetch the location model from Maxmind. |
62
|
|
|
* |
63
|
|
|
* @param string $ip |
64
|
|
|
* |
65
|
|
|
* @return \GeoIp2\Model\City |
66
|
|
|
* |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
|
|
protected function fetchLocation($ip) |
70
|
|
|
{ |
71
|
|
|
$maxmind = $this->isWebServiceEnabled() |
72
|
|
|
? $this->newClient($this->getUserId(), $this->getLicenseKey(), $this->getOptions()) |
73
|
|
|
: $this->newReader($this->getDatabasePath()); |
74
|
|
|
|
75
|
|
|
return $maxmind->city($ip); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns a new MaxMind web service client. |
80
|
|
|
* |
81
|
|
|
* @param string $userId |
82
|
|
|
* @param string $licenseKey |
83
|
|
|
* @param array $options |
84
|
|
|
* |
85
|
|
|
* @return Client |
86
|
|
|
*/ |
87
|
|
|
protected function newClient($userId, $licenseKey, array $options = []) |
88
|
|
|
{ |
89
|
|
|
return new Client($userId, $licenseKey, $options); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns a new MaxMind reader client with |
94
|
|
|
* the specified database file path. |
95
|
|
|
* |
96
|
|
|
* @param string $path |
97
|
|
|
* |
98
|
|
|
* @return \GeoIp2\Database\Reader |
99
|
|
|
*/ |
100
|
|
|
protected function newReader($path) |
101
|
|
|
{ |
102
|
|
|
return new Reader($path); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns true / false if the MaxMind web service is enabled. |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
protected function isWebServiceEnabled() |
111
|
|
|
{ |
112
|
|
|
return config('location.maxmind.web.enabled', false); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the configured MaxMind web user ID. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function getUserId() |
121
|
|
|
{ |
122
|
|
|
return config('location.maxmind.web.user_id'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the configured MaxMind web license key. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
protected function getLicenseKey() |
131
|
|
|
{ |
132
|
|
|
return config('location.maxmind.web.license_key'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns the configured MaxMind web option array. |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
protected function getOptions() |
141
|
|
|
{ |
142
|
|
|
return config('location.maxmind.web.options', []); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the MaxMind database file path. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
protected function getDatabasePath() |
151
|
|
|
{ |
152
|
|
|
return config('location.maxmind.local.path', database_path('maxmind/GeoLite2-City.mmdb')); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|