diff --git a/src/Service/IpGeolocation.php b/src/Service/IpGeolocation.php index 1bb01ff68..bdfce1495 100644 --- a/src/Service/IpGeolocation.php +++ b/src/Service/IpGeolocation.php @@ -73,32 +73,11 @@ final class IpGeolocation $this->initialize(); } - $reader = $this->reader; - if (null === $reader) { - throw new RuntimeException('No IP Geolocation reader available.'); - } - $cacheKey = $this->readerShortName . '_' . str_replace([':', '.'], '_', $ip); - $cacheItem = $this->psr6Cache->getItem($cacheKey); if (!$cacheItem->isHit()) { - try { - $ipInfo = $reader->get($ip); - if (!empty($ipInfo)) { - return $ipInfo; - } - - $cacheItem->set([ - 'status' => 'error', - 'message' => 'Internal/Reserved IP', - ]); - } catch (Exception $e) { - $cacheItem->set([ - 'status' => 'error', - 'message' => $e->getMessage(), - ]); - } + $cacheItem->set($this->getUncachedLocationInfo($ip)); /** @noinspection SummerTimeUnsafeTimeManipulationInspection */ $cacheItem->expiresAfter(86400 * 7); @@ -110,4 +89,29 @@ final class IpGeolocation return IpGeolocator\IpResult::fromIpInfo($ip, $ipInfo); } + + private function getUncachedLocationInfo(string $ip): array + { + $reader = $this->reader; + if (null === $reader) { + throw new RuntimeException('No IP Geolocation reader available.'); + } + + try { + $ipInfo = $reader->get($ip); + if (!empty($ipInfo)) { + return $ipInfo; + } + + return [ + 'status' => 'error', + 'message' => 'Internal/Reserved IP', + ]; + } catch (Exception $e) { + return [ + 'status' => 'error', + 'message' => $e->getMessage(), + ]; + } + } }