You are here

class GeoIpHandlerV2 in GeoIP API 7.2

Hierarchy

Expanded class hierarchy of GeoIpHandlerV2

File

src/GeoIpHandlerV2.php, line 11
The GeoIP API handler for version 2.

Namespace

Drupal\geoip
View source
class GeoIpHandlerV2 implements GeoIpHandlerInterface {

  /**
   * The reader for this configuration.
   *
   * @var \GeoIp2\Database\Reader
   */
  protected $reader;

  /**
   * Path to the db file to use.
   *
   * @var string
   */
  protected $dbFile;

  /**
   * Since the Maxmind API doesn't expose an unified method to read a record but
   * database typ related ones we store the method to use here once.
   *
   * @see GeoIpHandlerV2::record()
   *
   * @var string
   */
  protected $readMethod;

  /**
   * Load the required library.
   *
   * Does its best to find a library to use.
   *
   * @throws \Exception
   */
  public function __construct() {

    // Load the required library, throw Exception on failure.
    if (($library = libraries_load('GeoIP2-php')) && empty($library['loaded'])) {
      if (($library = libraries_load('GeoIP2-phar')) && empty($library['loaded'])) {
        throw new \Exception('Unable to load GeoIP2 API.');
      }
    }
  }

  /**
   * Creates a single reader instance for this file.
   *
   * @return \GeoIp2\Database\Reader
   *   The reader to use.
   */
  public function getReader() {
    if (!isset($this->reader)) {
      $this->reader = new Reader($this->dbFile);

      // Set the read method to use for this type of db.
      $database_type = $this->reader
        ->metadata()->databaseType;
      $this->readMethod = strtolower(substr($database_type, strrpos($database_type, '-') + 1));
    }
    return $this->reader;
  }

  /**
   * {@inheritdoc}
   */
  public function setDbFile($db_file) {
    $this->dbFile = $db_file;

    // Unset reader to ensure the new file is taken in account.
    $this->reader = NULL;
  }

  /**
   * Returns the record matching the ip.
   *
   * @param string $ip
   *   The ip to process.
   *
   * @return \GeoIp2\Model\AbstractModel
   *   The record object found for this IP.
   */
  public function record($ip = NULL) {
    $ip = empty($ip) ? ip_address() : $ip;
    return $this
      ->getReader()
      ->{$this->readMethod}($ip);
  }

  /**
   * {@inheritdoc}
   */
  public function rawRecord($ip = NULL) {
    return $this
      ->record($ip)->raw;
  }

  /**
   * {@inheritdoc}
   */
  public function continentCode($ip = NULL) {
    try {
      return strtoupper($this
        ->record($ip)->continent->code);
    } catch (\Exception $e) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function continentName($ip = NULL) {
    try {
      return $this
        ->record($ip)->continent->name;
    } catch (\Exception $e) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function countryCode($ip = NULL) {
    try {
      return strtoupper($this
        ->record($ip)->country->isoCode);
    } catch (\Exception $e) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function countryName($ip = NULL) {
    try {
      return $this
        ->record($ip)->country->name;
    } catch (\Exception $e) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function regionCode($ip = NULL) {
    try {
      $subdivisions = $this
        ->record($ip)->subdivisions;
      if ($subdivision = end($subdivisions)) {
        return strtoupper($subdivision->isoCode);
      }
    } catch (\Exception $e) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function regionName($ip = NULL) {
    try {
      $subdivisions = $this
        ->record($ip)->subdivisions;
      if ($subdivision = end($subdivisions)) {
        return $subdivision->name;
      }
    } catch (\Exception $e) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function cityName($ip = NULL) {
    try {
      return $this
        ->record($ip)->city->name;
    } catch (\Exception $e) {
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeoIpHandlerV2::$dbFile protected property Path to the db file to use.
GeoIpHandlerV2::$reader protected property The reader for this configuration.
GeoIpHandlerV2::$readMethod protected property Since the Maxmind API doesn't expose an unified method to read a record but database typ related ones we store the method to use here once.
GeoIpHandlerV2::cityName public function The city name for a given IP. Overrides GeoIpHandlerInterface::cityName
GeoIpHandlerV2::continentCode public function Returns the continent code for a given IP. Overrides GeoIpHandlerInterface::continentCode
GeoIpHandlerV2::continentName public function Returns the continent name for a given IP. Overrides GeoIpHandlerInterface::continentName
GeoIpHandlerV2::countryCode public function Returns the ISO 3166-2 country code for a given IP. Overrides GeoIpHandlerInterface::countryCode
GeoIpHandlerV2::countryName public function Returns the country name for a given IP. Overrides GeoIpHandlerInterface::countryName
GeoIpHandlerV2::getReader public function Creates a single reader instance for this file.
GeoIpHandlerV2::rawRecord public function Returns an array of all information related the ip. Overrides GeoIpHandlerInterface::rawRecord
GeoIpHandlerV2::record public function Returns the record matching the ip.
GeoIpHandlerV2::regionCode public function The region code for a given IP. Overrides GeoIpHandlerInterface::regionCode
GeoIpHandlerV2::regionName public function The region name for a given IP. Overrides GeoIpHandlerInterface::regionName
GeoIpHandlerV2::setDbFile public function Set the db file to use. Overrides GeoIpHandlerInterface::setDbFile
GeoIpHandlerV2::__construct public function Load the required library.