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'
1 service uses IpGeoLocSubscriber
File
- src/
EventSubscriber/ IpGeoLocSubscriber.php, line 17
Namespace
Drupal\ip_geoloc\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
IpGeoLocSubscriber:: |
protected | property | ||
IpGeoLocSubscriber:: |
protected | property | ||
IpGeoLocSubscriber:: |
protected | property | ||
IpGeoLocSubscriber:: |
protected | property | ||
IpGeoLocSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
IpGeoLocSubscriber:: |
public | function | This method is called whenever the KernelEvents::REQUEST event is dispatched. | |
IpGeoLocSubscriber:: |
public | function | Constructor for fependency injection. |