Skip to content

Commit f2bd096

Browse files
committed
use native gzdecode
1 parent ca48525 commit f2bd096

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

src/GeoIPUpdater.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,22 @@ public function update()
4848
*/
4949
protected function updateMaxmindDatabase()
5050
{
51-
$maxmindDatabaseUrl = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz';
51+
$maxmindDatabaseUrl = array_get($this->config, 'maxmind.download', 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz');
5252

5353
$database = array_get($this->config, 'maxmind.database', false);
5454

5555
if (! file_exists($dir = pathinfo($database, PATHINFO_DIRNAME))) {
5656
mkdir($dir, 0777, true);
5757
}
5858

59-
$file = $this->guzzle->get($maxmindDatabaseUrl)->getBody();
60-
6159
try {
62-
file_put_contents($database, $this->gzdecode($file));
60+
$file = $this->guzzle->get($maxmindDatabaseUrl)->getBody();
61+
62+
file_put_contents($database, gzdecode($file));
6363
} catch (Exception $e) {
6464
return false;
6565
}
6666

6767
return $database;
6868
}
69-
70-
/**
71-
* gzdecode function.
72-
*
73-
* @param \Psr\Http\Message\StreamInterface $data
74-
* @return string
75-
*/
76-
protected function gzdecode($data)
77-
{
78-
if (!function_exists('gzdecode')) {
79-
return gzinflate(substr($data, 10, -8));
80-
}
81-
82-
return gzdecode($data);
83-
}
8469
}

tests/GeoIPTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public function test_maxmind_web_api_authentication_exception()
103103
$geoip = $geoip->setIp($this->validIp);
104104

105105
$geoip->get();
106+
107+
$this->setExpectedException(GeoIPException::class);
108+
109+
$geoip->getRaw();
106110
}
107111

108112
public function test_get_random_ipaddress()
@@ -196,6 +200,10 @@ public function test_ip_api_pro_exception()
196200
$geoip = $geoip->setIp($this->validIp);
197201

198202
$geoip->get();
203+
204+
$this->setExpectedException(GeoIPException::class);
205+
206+
$geoip->getRaw();
199207
}
200208

201209
public function test_ip_api()

tests/GeoIPUpdaterTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ public function test_maxmind_updater()
3737
unlink($database);
3838
}
3939

40+
public function test_maxmind_updater_invalid_url()
41+
{
42+
$database = __DIR__.'/data/GeoLite2-City.mmdb';
43+
$config = [
44+
'driver' => 'maxmind',
45+
'maxmind' => [
46+
'database' => $database,
47+
'download' => 'http://example.com/maxmind_database.mmdb.gz'
48+
],
49+
];
50+
51+
$geoipUpdater = new GeoIPUpdater($config);
52+
53+
$this->assertFalse($geoipUpdater->update());
54+
}
55+
4056
public function test_maxmind_updater_dir_not_exist()
4157
{
4258
$database = __DIR__.'/data/new_dir/GeoLite2-City.mmdb';

0 commit comments

Comments
 (0)