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) {