Fixed GeoIP lookup causing an exception when no MMDB is available.

This commit is contained in:
flash 2022-06-06 16:42:38 +02:00
parent 01b3f41742
commit ba4f03cefa
2 changed files with 8 additions and 0 deletions

View file

@ -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);
}

View file

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