You are here

class IpGeoLocSubscriber in IP Geolocation Views & Maps 8

Subscribe to KernelEvents::REQUEST events and redirect if site is currently in maintenance mode.

Hierarchy

  • class \Drupal\ip_geoloc\EventSubscriber\IpGeoLocSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of IpGeoLocSubscriber

1 string reference to 'IpGeoLocSubscriber'
ip_geoloc.services.yml in ./ip_geoloc.services.yml
ip_geoloc.services.yml
1 service uses IpGeoLocSubscriber
ip_geoloc.initsubscriber in ./ip_geoloc.services.yml
Drupal\ip_geoloc\EventSubscriber\IpGeoLocSubscriber

File

src/EventSubscriber/IpGeoLocSubscriber.php, line 17

Namespace

Drupal\ip_geoloc\EventSubscriber
View source
class IpGeoLocSubscriber implements EventSubscriberInterface {
  protected $currentPath;
  protected $ipGeolocSession;
  protected $geolocGlobal;
  protected $api;

  /**
   * Constructor for fependency injection.
   */
  public function __construct(CurrentPathStack $currentPath, IpGeoLocSession $ipGeolocSession, IpGeoLocGlobal $geolocGlobal, IpGeoLocAPI $api) {
    $this->currentPath = $currentPath;
    $this->ipGeolocSession = $ipGeolocSession;
    $this->geolocGlobal = $geolocGlobal;
    $this->api = $api;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'initIpGeoLoc',
      20,
    ];
    return $events;
  }

  /**
   * This method is called whenever the KernelEvents::REQUEST event is dispatched.
   */
  public function initIpGeoLoc(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();
    $currentPath = $this->currentPath
      ->getPath();
    $path_args = explode('/', $currentPath);

    // @ TODO check this views functionality
    foreach ($path_args as $arg) {

      // Only works on Views pages that have a (dummy) contextual filter defined.
      if ($arg == 'erase-location') {
        $location = $this->api
          ->getVisitorLocation();
        if (empty($location['is_updated'])) {

          // Wipe the current visitor location.
          $this->ipGeolocSession
            ->setSessionValue('location', NULL);

          // ... now pretend to everyone it never happened.
          $_GET['q'] = str_replace('/erase-location', '', $_GET['q']);
          return;
        }
      }
    }

    // @TODO review this   $this->geolocGlobal->logErrors();
    $location = $this->api
      ->getVisitorLocation();
    $reverse_geocode_client_timeout = $this->geolocGlobal
      ->reverseGeocodeTimeout();
    if ($reverse_geocode_client_timeout || $this->geolocGlobal
      ->isFirstClick()) {

      // Not convinced this is desirable.
      $this->geolocGlobal
        ->reinitLocation($location, $reverse_geocode_client_timeout);
      $this->api
        ->storeLocation($location);
      $this->ipGeolocSession
        ->setSessionValue('location', $location);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IpGeoLocSubscriber::$api protected property
IpGeoLocSubscriber::$currentPath protected property
IpGeoLocSubscriber::$geolocGlobal protected property
IpGeoLocSubscriber::$ipGeolocSession protected property
IpGeoLocSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
IpGeoLocSubscriber::initIpGeoLoc public function This method is called whenever the KernelEvents::REQUEST event is dispatched.
IpGeoLocSubscriber::__construct public function Constructor for fependency injection.