|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PulkitJalan\GeoIP\Drivers; |
| 4 | + |
| 5 | +use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException; |
| 6 | +use GeoIp2\Exception\AddressNotFoundException; |
| 7 | +use GeoIp2\WebService\Client; |
| 8 | +use GeoIp2\Database\Reader; |
| 9 | + |
| 10 | +class MaxmindDriver extends AbstractGeoIPDriver |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var \GeoIp2\WebService\Client|\GeoIp2\Database\Reader |
| 14 | + */ |
| 15 | + protected $maxmind; |
| 16 | + |
| 17 | + /** |
| 18 | + * @param array $config |
| 19 | + */ |
| 20 | + public function __construct(array $config) |
| 21 | + { |
| 22 | + parent::__construct($config); |
| 23 | + |
| 24 | + $this->maxmind = $this->create(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Get array of data using Maxmind |
| 29 | + * |
| 30 | + * @param string $ip |
| 31 | + * @return array |
| 32 | + */ |
| 33 | + public function get($ip) |
| 34 | + { |
| 35 | + try { |
| 36 | + $data = $this->maxmind->city($ip); |
| 37 | + } catch (AddressNotFoundException $e) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
| 41 | + return [ |
| 42 | + 'city' => $data->city->name, |
| 43 | + 'country' => $data->country->name, |
| 44 | + 'countryCode' => $data->country->isoCode, |
| 45 | + 'latitude' => $data->location->latitude, |
| 46 | + 'longitude' => $data->location->longitude, |
| 47 | + 'region' => $data->mostSpecificSubdivision->name, |
| 48 | + 'regionCode' => $data->mostSpecificSubdivision->isoCode, |
| 49 | + 'timezone' => $data->location->timeZone, |
| 50 | + 'postalCode' => $data->postal->code, |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create the maxmind driver based on config |
| 56 | + * |
| 57 | + * @return mixed |
| 58 | + * @throws \PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException |
| 59 | + */ |
| 60 | + protected function create() |
| 61 | + { |
| 62 | + if (array_get($this->config, 'user_id', false)) { |
| 63 | + return $this->createWebClient(); |
| 64 | + } |
| 65 | + |
| 66 | + if (array_get($this->config, 'database', false)) { |
| 67 | + return $this->createDatabase(); |
| 68 | + } |
| 69 | + |
| 70 | + throw new InvalidCredentialsException(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Create the maxmind web client |
| 75 | + * |
| 76 | + * @return \GeoIp2\WebService\Client |
| 77 | + * @throws \PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException |
| 78 | + */ |
| 79 | + protected function createWebClient() |
| 80 | + { |
| 81 | + $userId = array_get($this->config, 'user_id', false); |
| 82 | + $licenseKey = array_get($this->config, 'license_key', false); |
| 83 | + |
| 84 | + // check and make sure they are set |
| 85 | + if (!$userId || !$licenseKey) { |
| 86 | + throw new InvalidCredentialsException(); |
| 87 | + } |
| 88 | + |
| 89 | + return new Client($userId, $licenseKey); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Create the maxmind database reader |
| 94 | + * |
| 95 | + * @return \GeoIp2\Database\Reader |
| 96 | + * @throws \PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException |
| 97 | + */ |
| 98 | + protected function createDatabase() |
| 99 | + { |
| 100 | + $database = array_get($this->config, 'database', false); |
| 101 | + |
| 102 | + // check if file exists first |
| 103 | + if (!$database || !file_exists($database)) { |
| 104 | + throw new InvalidCredentialsException(); |
| 105 | + } |
| 106 | + |
| 107 | + return new Reader($database); |
| 108 | + } |
| 109 | +} |
0 commit comments