You are here

Ip2CountryLookup.php in IP-based Determination of a Visitor's Country 8

Namespace

Drupal\ip2country

File

src/Ip2CountryLookup.php
View source
<?php

namespace Drupal\ip2country;

use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * The ip2country.lookup service.
 */
class Ip2CountryLookup implements Ip2CountryLookupInterface {

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

  /**
   * The database connection to use.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs an Ip2CountryLookup object.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct(RequestStack $requestStack, Connection $connection) {
    $this->currentRequest = $requestStack
      ->getCurrentRequest();
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public function getCountry($ip_address = NULL) {
    $ip_address = isset($ip_address) ? $ip_address : $this->currentRequest
      ->getClientIp();
    $ipl = ip2long($ip_address);
    if (is_int($ip_address)) {
      $ipl = $ip_address;
    }

    // Locate IP within range.
    $sql = "SELECT country FROM {ip2country}\n            WHERE (:start >= ip_range_first AND :end <= ip_range_last) LIMIT 1";
    $result = $this->connection
      ->query($sql, [
      ':start' => $ipl,
      ':end' => $ipl,
    ])
      ->fetchField();
    return $result;
  }

}

Classes

Namesort descending Description
Ip2CountryLookup The ip2country.lookup service.