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_abstract_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_abstract_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_abstract_web_service/src/EventSubscriber/SmartIpEventSubscriber.php, line 43
Contains \Drupal\smart_ip_abstract_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_abstract_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);
    $config = \Drupal::config(self::configName());
    $version = $config
      ->get('version');
    if (!empty($record) && !isset($record['error'])) {
      if ($version == 1) {
        $country = isset($record['country']) ? $record['country'] : '';
        $countryCode = isset($record['country_code']) ? $record['country_code'] : '';
        $region = isset($record['region']) ? $record['region'] : '';
        $regionCode = isset($record['region_iso_code']) ? $record['region_iso_code'] : '';
        $city = isset($record['city']) ? $record['city'] : '';
        $zip = isset($record['postal_code']) ? $record['postal_code'] : '';
        $latitude = isset($record['latitude']) ? $record['latitude'] : '';
        $longitude = isset($record['longitude']) ? $record['longitude'] : '';
        $timeZone = isset($record['timezone']['name']) ? $record['timezone']['name'] : '';
        $isEuCountry = isset($record['isEuCountry']) ? $record['isEuCountry'] : '';
        $isGdprCountry = isset($record['isGdprCountry']) ? $record['isGdprCountry'] : '';
        $location
          ->set('originalData', $record)
          ->set('country', $country)
          ->set('countryCode', mb_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);
      }
    }
  }
}