1 | <?php namespace Arcanedev\GeoIP\Drivers; |
||
12 | class MaxmindDatabaseDriver extends AbstractDriver |
||
13 | { |
||
14 | /* ------------------------------------------------------------------------------------------------ |
||
15 | | Properties |
||
16 | | ------------------------------------------------------------------------------------------------ |
||
17 | */ |
||
18 | /** |
||
19 | * Service reader instance. |
||
20 | * |
||
21 | * @var \GeoIp2\Database\Reader |
||
22 | */ |
||
23 | protected $reader; |
||
24 | |||
25 | /* ------------------------------------------------------------------------------------------------ |
||
26 | | Getters & Setters |
||
27 | | ------------------------------------------------------------------------------------------------ |
||
28 | */ |
||
29 | /** |
||
30 | * Get the database path. |
||
31 | * |
||
32 | * @return string|null |
||
33 | */ |
||
34 | 24 | private function getDatabasePath() |
|
38 | |||
39 | /** |
||
40 | * Get the locales. |
||
41 | * |
||
42 | * @return array |
||
43 | */ |
||
44 | 24 | private function getLocales() |
|
48 | |||
49 | /* ------------------------------------------------------------------------------------------------ |
||
50 | | Main Functions |
||
51 | | ------------------------------------------------------------------------------------------------ |
||
52 | */ |
||
53 | /** |
||
54 | * Init the driver. |
||
55 | */ |
||
56 | 24 | protected function init() |
|
65 | |||
66 | /** |
||
67 | * Locate the ip address. |
||
68 | * |
||
69 | * @param string $ipAddress |
||
70 | * |
||
71 | * @return \Arcanedev\GeoIP\Location |
||
72 | */ |
||
73 | 12 | public function locate($ipAddress) |
|
74 | { |
||
75 | 12 | $record = $this->reader->city($ipAddress); |
|
76 | |||
77 | 6 | return $this->hydrate([ |
|
78 | 6 | 'ip' => $ipAddress, |
|
79 | 6 | 'iso_code' => $record->country->isoCode, |
|
80 | 6 | 'country' => $record->country->name, |
|
81 | 6 | 'city' => $record->city->name, |
|
82 | 6 | 'state' => $record->mostSpecificSubdivision->name, |
|
83 | 6 | 'state_code' => $record->mostSpecificSubdivision->isoCode, |
|
84 | 6 | 'postal_code' => $record->postal->code, |
|
85 | 6 | 'latitude' => $record->location->latitude, |
|
86 | 6 | 'longitude' => $record->location->longitude, |
|
87 | 6 | 'timezone' => $record->location->timeZone, |
|
88 | 6 | 'continent' => $record->continent->code, |
|
89 | 1 | ]); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Update function for service. |
||
94 | * |
||
95 | * @return bool |
||
96 | * |
||
97 | * @throws \Exception |
||
98 | */ |
||
99 | public function update() |
||
100 | { |
||
101 | return DownloadMaxmindDatabaseTask::run( |
||
102 | $this->getOption('update-url'), |
||
103 | $this->getDatabasePath() |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | /* ------------------------------------------------------------------------------------------------ |
||
108 | | Other Functions |
||
109 | | ------------------------------------------------------------------------------------------------ |
||
110 | */ |
||
111 | /** |
||
112 | * Check if the database exists. |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | 24 | private function isDatabaseExists() |
|
120 | |||
121 | /** |
||
122 | * Check if database file exists, otherwise copy the dummy one. |
||
123 | */ |
||
124 | 24 | private function checkDatabaseFile() |
|
136 | } |
||
137 |