Skip to content

Commit 6b5bbd8

Browse files
author
Jens Wiese
committed
Refactor GeoIP2Provider: Use injection of Maxmind provider.
Instead of using only the database reader of Maxmind, inject the Maxmind provider. Thus both is available: database reader and webservice client.
1 parent d46ca18 commit 6b5bbd8

File tree

7 files changed

+218
-280
lines changed

7 files changed

+218
-280
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Currently, there are the following adapters:
1818
* `GuzzleHttpAdapter` to use [Guzzle](https://github.com/guzzle/guzzle), PHP 5.3+ HTTP client and framework for building RESTful web service clients;
1919
* `SocketHttpAdapter` to use a [socket](http://www.php.net/manual/function.fsockopen.php);
2020
* `ZendHttpAdapter` to use [Zend Http Client](http://framework.zend.com/manual/2.0/en/modules/zend.http.client.html).
21-
* `GeoIP2DatabaseAdapter` to use [GeoIP2 Database Reader by MaxMind](https://github.com/maxmind/GeoIP2-php#database-reader).
21+
* `GeoIP2Adapter` to use [GeoIP2 Database Reader](https://github.com/maxmind/GeoIP2-php#database-reader) or the [Webservice Client](https://github.com/maxmind/GeoIP2-php#web-service-client) by MaxMind.
2222

2323

2424
### Providers ###
@@ -50,7 +50,7 @@ Currently, there are many providers for the following APIs:
5050
* [GeoIPs](http://www.geoips.com/developer/geoips-api) as IP-Based geocoding provider;
5151
* [MaxMind web service](http://dev.maxmind.com/geoip/legacy/web-services) as IP-Based geocoding provider (City/ISP/Org and Omni services);
5252
* [MaxMind binary file](http://dev.maxmind.com/geoip/legacy/downloadable) as IP-Based geocoding provider;
53-
* [MaxMind GeoIP2 database file](http://www.maxmind.com/en/city) as IP-Based geocoding provider;
53+
* [MaxMind GeoIP2](http://www.maxmind.com/en/city) as IP-Based geocoding provider;
5454
* [Geonames](http://www.geonames.org/) as Place-Based geocoding and reverse geocoding provider;
5555
* [IpGeoBase](http://ipgeobase.ru/) as IP-Based geocoding provider (very accurate in Russia);
5656
* [Baidu](http://developer.baidu.com/map/geocoding-api.htm) as Address-Based geocoding and reverse geocoding provider (exclusively in China);
@@ -263,17 +263,20 @@ be used in production. For more information, please read [issue #301](https://gi
263263

264264
### GeoIP2DatabaseProvider ###
265265

266-
The `GeoIP2DatabaseProvider` named `geoip2_database` is able to geocode **IPv4 and IPv6 addresses**
267-
only - it makes use of the MaxMind GeoIP2 databases.
266+
The `GeoIP2Provider` named `maxmind_geoip2` is able to geocode **IPv4 and IPv6 addresses**
267+
only - it makes use of the MaxMind GeoIP2 databases or the webservice.
268268

269-
It requires the [database file](http://dev.maxmind.com/geoip/geoip2/geolite2/), and the [geoip2/geoip2](https://packagist.org/packages/geoip2/geoip2) package must be installed.
269+
It requires either the [database file](http://dev.maxmind.com/geoip/geoip2/geolite2/), or the [webservice](http://dev.maxmind.com/geoip/geoip2/web-services/) - represented by the GeoIP2 Provider, which is injected to the `GeoIP2Adapter`. The [geoip2/geoip2](https://packagist.org/packages/geoip2/geoip2) package must be installed.
270270

271-
This provider will only work with the corresponding `GeoIP2DatabaseAdapter`.
271+
This provider will only work with the corresponding `GeoIP2Adapter`.
272272

273273
**Usage:**
274274

275-
$adapter = new \Geocoder\HttpAdapter\GeoIP2DatabaseAdapter('/path/to/database');
276-
$provider = new \Geocoder\Provider\GeoIP2DatabaseProvider($adapter);
275+
// Maxmind GeoIP2 Provider: e.g. the database reader
276+
$reader = new \GeoIp2\Database\Reader('/path/to/database');
277+
278+
$adapter = new \Geocoder\HttpAdapter\GeoIP2Adapter($reader);
279+
$provider = new \Geocoder\Provider\GeoIP2Provider($adapter);
277280
$geocoder = new \Geocoder\Geocoder($provider);
278281

279282
$result = $geocoder->geocode('74.200.247.59');

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
"guzzle/guzzle": "@stable",
2020
"zendframework/zend-http": "~2.1",
2121
"geoip/geoip": "~1.13",
22-
"geoip2/geoip2": "~0.6",
23-
"mikey179/vfsStream": "v1.2.0"
22+
"geoip2/geoip2": "~0.6"
2423
},
2524
"suggest": {
2625
"kriswallsmith/buzz": "Enabling Buzz allows you to use the BuzzHttpAdapter.",
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\HttpAdapter;
12+
13+
use Geocoder\Exception\InvalidArgumentException;
14+
use Geocoder\Exception\UnsupportedException;
15+
use GeoIp2\ProviderInterface;
16+
17+
/**
18+
* @author Jens Wiese <jens@howtrueisfalse.de>
19+
*/
20+
class GeoIP2Adapter implements HttpAdapterInterface
21+
{
22+
/**
23+
* GeoIP2 models (e.g. city or country)
24+
*/
25+
const GEOIP2_MODEL_CITY = 'city';
26+
const GEOIP2_MODEL_COUNTRY = 'country';
27+
const GEOIP2_MODEL_OMNI = 'omni';
28+
29+
/**
30+
* @var ProviderInterface
31+
*/
32+
protected $geoIp2Provider;
33+
34+
/**
35+
* @var string
36+
*/
37+
protected $geoIP2Model;
38+
39+
/**
40+
* @var string
41+
*/
42+
protected $locale;
43+
44+
/**
45+
* @param \GeoIp2\ProviderInterface $geoIpProvider
46+
* @param string $geoIP2Model (e.g. self::GEOIP2_MODEL_CITY)
47+
* @throws \Geocoder\Exception\UnsupportedException
48+
* @internal param string $dbFile
49+
*/
50+
public function __construct(ProviderInterface $geoIpProvider, $geoIP2Model = self::GEOIP2_MODEL_CITY)
51+
{
52+
$this->geoIp2Provider = $geoIpProvider;
53+
54+
if (false === $this->isSupportedGeoIP2Model($geoIP2Model)) {
55+
throw new UnsupportedException(
56+
sprintf('Model "%s" is not available.', $geoIP2Model)
57+
);
58+
}
59+
60+
$this->geoIP2Model = $geoIP2Model;
61+
}
62+
63+
/**
64+
* @param string $locale
65+
* @return $this
66+
*/
67+
public function setLocale($locale)
68+
{
69+
$this->locale = $locale;
70+
71+
return $this;
72+
}
73+
74+
/**
75+
* @return string
76+
*/
77+
public function getLocale()
78+
{
79+
return $this->locale;
80+
}
81+
82+
/**
83+
* Returns the content fetched from a given resource.
84+
*
85+
* @param string $url (e.g. file://database?127.0.0.1)
86+
* @throws \Geocoder\Exception\UnsupportedException
87+
* @throws \Geocoder\Exception\InvalidArgumentException
88+
* @return string
89+
*/
90+
public function getContent($url)
91+
{
92+
if (false === filter_var($url, FILTER_VALIDATE_URL)) {
93+
throw new InvalidArgumentException(
94+
sprintf('"%s" must be called with a valid url. Got "%s" instead.', __METHOD__, $url)
95+
);
96+
}
97+
98+
$ipAddress = parse_url($url, PHP_URL_QUERY);
99+
100+
if (false === filter_var($ipAddress, FILTER_VALIDATE_IP)) {
101+
throw new InvalidArgumentException('URL must contain a valid query-string (an IP address, 127.0.0.1 for instance)');
102+
}
103+
104+
$result = $this->geoIp2Provider
105+
->{$this->geoIP2Model}($ipAddress)
106+
->jsonSerialize();
107+
108+
return json_encode($result);
109+
}
110+
111+
/**
112+
* Returns the name of the Adapter.
113+
*
114+
* @return string
115+
*/
116+
public function getName()
117+
{
118+
return 'maxmind_geoip2';
119+
}
120+
121+
/**
122+
* Returns whether method is supported by GeoIP2
123+
*
124+
* @param string $method
125+
* @return bool
126+
*/
127+
protected function isSupportedGeoIP2Model($method)
128+
{
129+
$availableMethods = array(
130+
self::GEOIP2_MODEL_CITY,
131+
self::GEOIP2_MODEL_COUNTRY,
132+
self::GEOIP2_MODEL_OMNI
133+
);
134+
135+
return in_array($method, $availableMethods);
136+
}
137+
}

src/Geocoder/HttpAdapter/GeoIP2DatabaseAdapter.php

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)