You are here

class Local in GeoIP API 8.2

Local geolocation provider.

Plugin annotation


@GeoLocator(
  id = "local",
  label = "Local dataset",
  description = "Uses local MaxmindDB dataset for geolocation",
  weight = 0
)

Hierarchy

Expanded class hierarchy of Local

File

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

Namespace

Drupal\geoip\Plugin\GeoLocator
View source
class Local extends GeoLocatorBase {
  const GEOLITE_CITY_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz';
  const GEOLITE_COUNTRY_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz';
  protected $scheme = 'public';

  /**
   * {@inheritdoc}
   */
  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;
    }
  }

  /**
   * Get a dataset reader.
   *
   * @return \GeoIp2\Database\Reader|null
   *   Reader that can parse Mindmax datasets.
   */
  protected function getReader() {
    $city_uri = $this
      ->getScheme() . '://GeoLite2-City.mmdb';
    $country_uri = $this
      ->getScheme() . '://GeoLite2-Country.mmdb';
    if (file_exists($city_uri)) {
      $reader = new Reader($city_uri);
    }
    elseif (file_exists($country_uri)) {
      $reader = new Reader($country_uri);
    }
    else {

      // No dataset is installed.
      return NULL;
    }
    return $reader;
  }

  /**
   * Get the current file scheme.
   *
   * @return string
   *   The file scheme.
   */
  public function getScheme() {
    return $this->scheme;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GeoLocatorBase::$geoIpConfig protected property The GeoIP module configuration.
GeoLocatorBase::$logger protected property The logger.
GeoLocatorBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
GeoLocatorBase::getDescription public function Get the plugin's description. Overrides GeoLocatorInterface::getDescription
GeoLocatorBase::getId public function Get the plugin's ID. Overrides GeoLocatorInterface::getId
GeoLocatorBase::getLabel public function Get the plugin's label. Overrides GeoLocatorInterface::getLabel
GeoLocatorBase::__construct public function Constructs a PluginBase object. Overrides PluginBase::__construct
Local::$scheme protected property
Local::GEOLITE_CITY_DB constant
Local::GEOLITE_COUNTRY_DB constant
Local::geolocate public function Performs geolocation on an address. Overrides GeoLocatorInterface::geolocate
Local::getReader protected function Get a dataset reader.
Local::getScheme public function Get the current file scheme.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.