ip_geoloc-2.0.0-alpha0/src/EventSubscriber/IpGeoLocSubscriber.php

src/EventSubscriber/IpGeoLocSubscriber.php
<?php

namespace Drupal\ip_geoloc\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Drupal\ip_geoloc\Services\IpGeoLocSession;
use Drupal\ip_geoloc\Services\IpGeoLocGlobal;
use Drupal\ip_geoloc\Services\IpGeoLocAPI;

use Drupal\Core\Path\CurrentPathStack;

/**
 * Subscribe to KernelEvents::REQUEST events and redirect if site is currently in maintenance mode.
 */
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(ResponseEvent $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);
    }

  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc