From ba4f03cefa1ee48aac59f6676f99880a9b8395a8 Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 6 Jun 2022 16:42:38 +0200 Subject: [PATCH] Fixed GeoIP lookup causing an exception when no MMDB is available. --- src/Net/GeoIP.php | 5 +++++ src/Net/IPAddress.php | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/Net/GeoIP.php b/src/Net/GeoIP.php index 6cdb5728..8df751a8 100644 --- a/src/Net/GeoIP.php +++ b/src/Net/GeoIP.php @@ -17,6 +17,11 @@ final class GeoIP { return self::$geoip; } + public static function isAvailable(): bool { + return !empty(self::$geoipDbPath) + && is_file(self::$geoipDbPath); + } + public static function resolveCountry(string $ipAddress) { return self::getReader()->country($ipAddress); } diff --git a/src/Net/IPAddress.php b/src/Net/IPAddress.php index a7d758be..62d00f10 100644 --- a/src/Net/IPAddress.php +++ b/src/Net/IPAddress.php @@ -23,6 +23,9 @@ final class IPAddress { } public static function country(string $address, string $fallback = 'XX'): string { + if(!GeoIP::isAvailable()) + return $fallback; + try { return GeoIP::resolveCountry($address)->country->isoCode ?? $fallback; } catch(AddressNotFoundException $e) {