IpGeoLocSubscriber.php in IP Geolocation Views & Maps 8
File
src/EventSubscriber/IpGeoLocSubscriber.php
View source
<?php
namespace Drupal\ip_geoloc\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Drupal\ip_geoloc\Services\IpGeoLocSession;
use Drupal\ip_geoloc\Services\IpGeoLocGlobal;
use Drupal\ip_geoloc\Services\IpGeoLocAPI;
use Drupal\Core\Path\CurrentPathStack;
class IpGeoLocSubscriber implements EventSubscriberInterface {
protected $currentPath;
protected $ipGeolocSession;
protected $geolocGlobal;
protected $api;
public function __construct(CurrentPathStack $currentPath, IpGeoLocSession $ipGeolocSession, IpGeoLocGlobal $geolocGlobal, IpGeoLocAPI $api) {
$this->currentPath = $currentPath;
$this->ipGeolocSession = $ipGeolocSession;
$this->geolocGlobal = $geolocGlobal;
$this->api = $api;
}
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = [
'initIpGeoLoc',
20,
];
return $events;
}
public function initIpGeoLoc(FilterResponseEvent $event) {
$response = $event
->getResponse();
$currentPath = $this->currentPath
->getPath();
$path_args = explode('/', $currentPath);
foreach ($path_args as $arg) {
if ($arg == 'erase-location') {
$location = $this->api
->getVisitorLocation();
if (empty($location['is_updated'])) {
$this->ipGeolocSession
->setSessionValue('location', NULL);
$_GET['q'] = str_replace('/erase-location', '', $_GET['q']);
return;
}
}
}
$location = $this->api
->getVisitorLocation();
$reverse_geocode_client_timeout = $this->geolocGlobal
->reverseGeocodeTimeout();
if ($reverse_geocode_client_timeout || $this->geolocGlobal
->isFirstClick()) {
$this->geolocGlobal
->reinitLocation($location, $reverse_geocode_client_timeout);
$this->api
->storeLocation($location);
$this->ipGeolocSession
->setSessionValue('location', $location);
}
}
}