You are here

public function SmartIpEventSubscriber::processQuery in Smart IP 8.4

Same name in this branch
  1. 8.4 modules/device_geolocation/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\device_geolocation\EventSubscriber\SmartIpEventSubscriber::processQuery()
  2. 8.4 modules/smart_ip_maxmind_geoip2_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_bin_db\EventSubscriber\SmartIpEventSubscriber::processQuery()
  3. 8.4 modules/smart_ip_ipinfodb_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ipinfodb_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()
  4. 8.4 modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ip2location_bin_db\EventSubscriber\SmartIpEventSubscriber::processQuery()
  5. 8.4 modules/smart_ip_abstract_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_abstract_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()
  6. 8.4 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.2 modules/smart_ip_ipinfodb_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ipinfodb_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()
  2. 8.3 modules/smart_ip_ipinfodb_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ipinfodb_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_ipinfodb_web_service/src/EventSubscriber/SmartIpEventSubscriber.php, line 43
Contains \Drupal\smart_ip_ipinfodb_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_ipinfodb_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');
    foreach ($record as &$item) {
      if (strpos($item, 'Please upgrade') !== FALSE || strpos($item, 'Invalid IP address') !== FALSE || $item == '-') {

        // Make the value "This parameter is unavailable in selected .BIN
        // data file. Please upgrade." or "Invalid IP address" or "-" as
        // NULL.
        $item = NULL;
      }
    }
    if ($version == 2) {
      $region = '';
      if (isset($record['RegionCode']) && isset($record['CountryCode'])) {
        $regionResult = smart_ip_get_region_static($record['CountryCode'], $record['RegionCode']);
        $region = $regionResult[$record['CountryCode']][$record['RegionCode']];
      }
      elseif (isset($record['RegionName'])) {
        $region = $record['RegionName'];
      }
      $country = isset($record['CountryName']) ? $record['CountryName'] : '';
      $countryCode = isset($record['CountryCode']) ? $record['CountryCode'] : '';
      $regionCode = isset($record['RegionCode']) ? $record['RegionCode'] : '';
      $city = isset($record['City']) ? $record['City'] : '';
      $zip = isset($record['ZipPostalCode']) ? $record['ZipPostalCode'] : '';
      $latitude = isset($record['Latitude']) ? $record['Latitude'] : '';
      $longitude = isset($record['Longitude']) ? $record['Longitude'] : '';
      $timeZone = isset($record['TimeZone']) ? $record['TimeZone'] : '';
      $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);
    }
    elseif ($version == 3) {
      $country = isset($record['countryName']) ? $record['countryName'] : '';
      $countryCode = isset($record['countryCode']) ? $record['countryCode'] : '';
      $region = isset($record['regionName']) ? $record['regionName'] : '';
      $regionCode = isset($record['regionCode']) ? $record['regionCode'] : '';
      $city = isset($record['cityName']) ? $record['cityName'] : '';
      $zip = isset($record['zipCode']) ? $record['zipCode'] : '';
      $latitude = isset($record['latitude']) ? $record['latitude'] : '';
      $longitude = isset($record['longitude']) ? $record['longitude'] : '';
      $timeZone = isset($record['timeZone']) ? $record['timeZone'] : '';
      $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);
    }
  }
}