You are here

class GeoLocation in GeoIP API 8.2

Service to interact with the default geolocator plugin for geolocation.

Hierarchy

Expanded class hierarchy of GeoLocation

1 file declares its use of GeoLocation
GeoLocationTest.php in tests/src/Unit/GeoLocationTest.php
1 string reference to 'GeoLocation'
geoip.services.yml in ./geoip.services.yml
geoip.services.yml
1 service uses GeoLocation
geoip.geolocation in ./geoip.services.yml
Drupal\geoip\GeoLocation

File

src/GeoLocation.php, line 14

Namespace

Drupal\geoip
View source
class GeoLocation {
  use UseCacheBackendTrait;

  /**
   * Plugin manager for GeoLocator plugins.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $geoLocatorManager;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;
  protected $cacheKey = 'geolocated_ips';
  protected $cacheTags = [
    'geoip',
  ];
  protected $locatedAddresses = [];
  protected $config = [];

  /**
   * Constructs a new GeoLocation object.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $geolocators_manager
   *   The geolocation locator plugin manager service to use.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   */
  public function __construct(PluginManagerInterface $geolocators_manager, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache_backend) {
    $this->geoLocatorManager = $geolocators_manager;
    $this->configFactory = $config_factory;
    $this->cacheBackend = $cache_backend;
    $this->config = $this->configFactory
      ->get('geoip.geolocation');
  }

  /**
   * Gets the identifier of the default geolocator plugin.
   *
   * @return string
   *   Identifier of the default geolocator plugin.
   */
  public function getGeoLocatorId() {
    return $this->config
      ->get('plugin_id');
  }

  /**
   * Gets an instance of the default geolocator plugin.
   *
   * @return \Drupal\geoip\Plugin\GeoLocator\GeoLocatorInterface
   *   Instance of the default geolocator plugin.
   */
  public function getGeoLocator() {
    return $this->geoLocatorManager
      ->createInstance($this->config
      ->get('plugin_id'));
  }

  /**
   * Geolocate an IP address.
   *
   * @param string $ip_address
   *   The IP address to geo locate.
   *
   * @return string|null
   *   The geolocated country code, or NULL if not found.
   */
  public function geolocate($ip_address) {
    if (!isset($this->locatedAddresses[$ip_address])) {
      if ($cache = $this->cacheBackend
        ->get($this->cacheKey . ':' . $ip_address)) {
        $this->locatedAddresses[$ip_address] = $cache->data;
      }
      else {
        $geolocator = $this
          ->getGeoLocator();
        $result = $geolocator
          ->geolocate($ip_address);
        $this->locatedAddresses[$ip_address] = $result;
        $this->cacheBackend
          ->set($this->cacheKey . ':' . $ip_address, $this->locatedAddresses[$ip_address], Cache::PERMANENT, $this->cacheTags);
      }
    }
    return $this->locatedAddresses[$ip_address];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeoLocation::$cacheKey protected property
GeoLocation::$cacheTags protected property
GeoLocation::$config protected property
GeoLocation::$configFactory protected property The configuration factory.
GeoLocation::$geoLocatorManager protected property Plugin manager for GeoLocator plugins.
GeoLocation::$locatedAddresses protected property
GeoLocation::geolocate public function Geolocate an IP address.
GeoLocation::getGeoLocator public function Gets an instance of the default geolocator plugin.
GeoLocation::getGeoLocatorId public function Gets the identifier of the default geolocator plugin.
GeoLocation::__construct public function Constructs a new GeoLocation object.
UseCacheBackendTrait::$cacheBackend protected property Cache backend instance.
UseCacheBackendTrait::$useCaches protected property Flag whether caches should be used or skipped.
UseCacheBackendTrait::cacheGet protected function Fetches from the cache backend, respecting the use caches flag. 1
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.