-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoFinder.php
206 lines (184 loc) · 4.38 KB
/
GeoFinder.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace yiicod\geo;
use Exception;
use Yii;
use yii\base\Component;
use yiicod\geo\base\GeoAdapterInterface;
use yiicod\geo\base\GeoDataInterface;
use yiicod\geo\base\GeoFinderInterface;
use yiicod\geo\storages\StorageInterface;
/**
* Class GeoManager
*
* @package yiicod\geo\components
*
* @author Dmitry Turchanin
* @author Alexey Orlov
*/
class GeoFinder extends Component implements GeoFinderInterface
{
/**
* @var array
*/
public $gettersList = [
[
'class' => \yiicod\geo\adapters\geoIp2\GeoIp2CityAdapter::class,
],
[
'class' => \yiicod\geo\adapters\geoPlugin\GeoPluginAdapter::class,
],
[
'class' => \yiicod\geo\adapters\ipstack\IpstackAdapter::class,
],
[
// 'class' => \yiicod\geo\adapters\freeGeoIp\FreeGeoIpLocator::class
],
];
/**
* Cache expire value
*
* @var int
*/
public $duration = 604800;
/**
* Storage instance
*
* @var StorageInterface
*/
private $storage;
/**
* Prepared country data found in the one request
*
* @var array
*/
private static $result = [];
/**
* GeoFinder constructor.
*
* @param StorageInterface $storage
* @param array $config
*/
public function __construct(StorageInterface $storage, $config = [])
{
parent::__construct($config);
$this->storage = $storage;
}
/**
* Gets country code by IP
*
* @param string|null
*
* @return string
*/
public function getCountryCode(string $ip): ?string
{
return $this->getPreparedCountryData($ip)->getCountryCode();
}
/**
* Gets country code by IP
*
* @param string|null
*
* @return string
*/
public function getCountryName(string $ip): ?string
{
return $this->getPreparedCountryData($ip)->getCountryName();
}
/**
* Gets region by IP
*
* @param string $ip
*
* @return null|string
*/
public function getRegion(string $ip): ?string
{
return $this->getPreparedCountryData($ip)->getRegion();
}
/**
* Gets region name by IP
*
* @param string $ip
*
* @return null|string
*/
public function getRegionName(string $ip): ?string
{
return $this->getPreparedCountryData($ip)->getRegionName();
}
/**
* Gets region code by IP
*
* @param string $ip
*
* @return null|string
*/
public function getRegionCode(string $ip): ?string
{
return $this->getPreparedCountryData($ip)->getRegionCode();
}
/**
* Gets city by IP
*
* @param string $ip
*
* @return null|string
*/
public function getCity(string $ip): ?string
{
return $this->getPreparedCountryData($ip)->getCity();
}
/**
* Gets latitude by IP
*
* @param string $ip
*
* @return null|string
*/
public function getLatitude(string $ip): ?string
{
return $this->getPreparedCountryData($ip)->getLatitude();
}
/**
* Gets longitude by IP
*
* @param string $ip
*
* @return null|string
*/
public function getLongitude(string $ip): ?string
{
return $this->getPreparedCountryData($ip)->getLongitude();
}
/**
* Gets prepared country data by IP (country code, country name)
*
* @param null|string|mixed $ip
*
* @return GeoDataInterface
*/
public function getPreparedCountryData($ip): GeoDataInterface
{
if (true === isset(self::$result[$ip])) {
return self::$result[$ip];
}
self::$result[$ip] = $this->storage->getOrSet($ip, function () use ($ip) {
foreach ($this->gettersList as $key => $config) {
try {
$adapter = Yii::createObject($config);
if (!($adapter instanceof GeoAdapterInterface)) {
unset($key);
continue;
}
return $adapter->getLocationData($ip);
} catch (Exception $e) {
unset($key);
continue;
}
}
return new GeoData(['ip' => $ip]);
}, $this->duration);
return self::$result[$ip];
}
}