remoteAddress = $remoteAddress; $this->geoIpCache = $geoIpCache; $this->mmdbReader = $mmdbReader; $this->cacheKeyPostfixes = $cacheKeyPostfixes; } /** * @inheritDoc */ public function getGeoLocation(?string $ip): ?array { if (! $ip) { $ip = $this->remoteAddress->getRemoteAddress(); } if ($ip) { $cacheKey = $ip . $this->cacheKeyPostfixes[$this->getShortMethodName(__METHOD__)]; $geoIpCache = $this->geoIpCache->get(); if (isset($geoIpCache[$cacheKey])) { return $geoIpCache[$cacheKey]; } try { $result = $this->mmdbReader->get($ip); } catch (InvalidDatabaseException $e) { $result = null; } $geoIpCache[$cacheKey] = $result; $this->geoIpCache->set($geoIpCache); } return $result ?? null; } /** * @inheritDoc */ public function getCountryCode(?string $ip): ?string { if (! $ip) { $ip = $this->remoteAddress->getRemoteAddress(); } if ($ip) { $cacheKey = $ip . $this->cacheKeyPostfixes[$this->getShortMethodName(__METHOD__)]; $geoIpCache = $this->geoIpCache->get(); if (isset($geoIpCache[$cacheKey])) { return $geoIpCache[$cacheKey]; } $result = $this->getGeoLocation($ip); $result = $result['country_code'] ?? null; $geoIpCache[$cacheKey] = $result; $this->geoIpCache->set($geoIpCache); } return $result ?? null; } /** * @inheritDoc */ public function getCurrentCountryState(?string $ip): ?string { if (! $ip) { $ip = $this->remoteAddress->getRemoteAddress(); } if ($ip) { $cacheKey = $ip . $this->cacheKeyPostfixes[$this->getShortMethodName(__METHOD__)]; $geoIpCache = $this->geoIpCache->get(); if (isset($geoIpCache[$cacheKey])) { return $geoIpCache[$cacheKey]; } $result = $this->getGeoLocation($ip); $result = $result['subdivision_1_name'] ?? null; $geoIpCache[$cacheKey] = $result; $this->geoIpCache->set($geoIpCache); } return $result ?? null; } /** * @param string $method * @return string */ private function getShortMethodName(string $method): string { return substr($method, strpos($method, '::') + 2); } }