You are here

public function Local::geolocate in GeoIP API 8.2

Performs geolocation on an address.

Parameters

string $ip_address: The IP address to geolocate.

Return value

string The geolocated country code, or NULL if not found.

Overrides GeoLocatorInterface::geolocate

File

src/Plugin/GeoLocator/Local.php, line 29

Class

Local
Local geolocation provider.

Namespace

Drupal\geoip\Plugin\GeoLocator

Code

public function geolocate($ip_address) {
  $reader = $this
    ->getReader();

  // If the reader could not be initiated, then back out.
  if (!$reader) {
    return NULL;
  }
  try {
    $record = $reader
      ->country($ip_address);
    if ($this->geoIpConfig
      ->get('debug')) {
      $this->logger
        ->notice($this
        ->t('Discovered %ip_address in the Maxmind local database', [
        '%ip_address' => $ip_address,
      ]));
    }
    return $record->country->isoCode;
  } catch (AddressNotFoundException $e) {
    if ($this->geoIpConfig
      ->get('debug')) {
      $this->logger
        ->notice($this
        ->t('Unable to look up %ip_address in the Maxmind local database', [
        '%ip_address' => $ip_address,
      ]));
    }
    return NULL;
  } catch (InvalidDatabaseException $e) {
    $this->logger
      ->error($this
      ->t('The Maxmind database reader reported an invalid or corrupt database.'));
    return NULL;
  }
}