You are here

public function SmartIpEventSubscriber::processQuery in Smart IP 8.3

Same name in this branch
  1. 8.3 modules/device_geolocation/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\device_geolocation\EventSubscriber\SmartIpEventSubscriber::processQuery()
  2. 8.3 modules/smart_ip_maxmind_geoip2_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_bin_db\EventSubscriber\SmartIpEventSubscriber::processQuery()
  3. 8.3 modules/smart_ip_ipinfodb_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ipinfodb_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()
  4. 8.3 modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ip2location_bin_db\EventSubscriber\SmartIpEventSubscriber::processQuery()
  5. 8.3 modules/smart_ip_abstract_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_abstract_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()
  6. 8.3 modules/smart_ip_maxmind_geoip2_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()
Same name and namespace in other branches
  1. 8.4 modules/smart_ip_maxmind_geoip2_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()

Act on \Drupal\smart_ip\SmartIp::query() when executed and if selected as Smart IP data source, query the IP address against its database.

Parameters

\Drupal\smart_ip\GetLocationEvent $event: Smart IP query location override event for event listeners.

Overrides SmartIpDataSourceInterface::processQuery

File

modules/smart_ip_maxmind_geoip2_web_service/src/EventSubscriber/SmartIpEventSubscriber.php, line 45
Contains \Drupal\smart_ip_maxmind_geoip2_web_service\EventSubscriber\SmartIpEventSubscriber.

Class

SmartIpEventSubscriber
Core functionalty of this Smart IP data source module. Listens to Smart IP override events.

Namespace

Drupal\smart_ip_maxmind_geoip2_web_service\EventSubscriber

Code

public function processQuery(GetLocationEvent $event) {
  if ($event
    ->getDataSource() == self::sourceId()) {
    $location = $event
      ->getLocation();
    $ipAddress = $location
      ->get('ipAddress');
    $record = WebServiceUtility::getGeolocation($ipAddress);
    $lang = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
    if (!isset($record['country']['names'][$lang])) {

      // The current language is not yet supported by MaxMind, use English as
      // default language.
      $lang = 'en';
    }
    $country = isset($record['country']['names'][$lang]) ? $record['country']['names'][$lang] : '';
    $countryCode = isset($record['country']['iso_code']) ? $record['country']['iso_code'] : '';
    $region = isset($record['subdivisions'][0]['names'][$lang]) ? $record['subdivisions'][0]['names'][$lang] : '';
    $regionCode = isset($record['subdivisions'][0]['iso_code']) ? $record['subdivisions'][0]['iso_code'] : '';
    $city = isset($record['city']['names'][$lang]) ? $record['city']['names'][$lang] : '';
    $zip = isset($record['postal']['code']) ? $record['postal']['code'] : '';
    $latitude = isset($record['location']['latitude']) ? $record['location']['latitude'] : '';
    $longitude = isset($record['location']['longitude']) ? $record['location']['longitude'] : '';
    $timeZone = isset($record['location']['time_zone']) ? $record['location']['time_zone'] : '';
    $isEuCountry = isset($record['country']['is_in_european_union']) ? $record['country']['is_in_european_union'] : '';
    $isGdprCountry = isset($record['country']['isGdprCountry']) ? $record['country']['isGdprCountry'] : '';
    $location
      ->set('originalData', $record)
      ->set('country', $country)
      ->set('countryCode', Unicode::strtoupper($countryCode))
      ->set('region', $region)
      ->set('regionCode', $regionCode)
      ->set('city', $city)
      ->set('zip', $zip)
      ->set('latitude', $latitude)
      ->set('longitude', $longitude)
      ->set('timeZone', $timeZone)
      ->set('isEuCountry', $isEuCountry)
      ->set('isGdprCountry', $isGdprCountry);
  }
}