CleanerSessionClearEventSubscriber.php in Cleaner 8.2
File
src/EventSubscriber/CleanerSessionClearEventSubscriber.php
View source
<?php
namespace Drupal\cleaner\EventSubscriber;
use Drupal\cleaner\Event\CleanerRunEvent;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CleanerSessionClearEventSubscriber implements EventSubscriberInterface, ContainerInjectionInterface {
protected $database;
protected $config;
protected $loggerChannel;
protected $requestTime;
public static function getSubscribedEvents() {
$events[CleanerRunEvent::CLEANER_RUN][] = [
'clearSession',
100,
];
return $events;
}
public function __construct(Connection $database, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_channel_factory, TimeInterface $time) {
$this->database = $database;
$this->config = $config_factory
->get('cleaner.settings');
$this->loggerChannel = $logger_channel_factory
->get('cleaner');
$this->requestTime = $time
->getRequestTime();
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('config.factory'), $container
->get('logger.factory'), $container
->get('datetime.time'));
}
public function clearSession() {
if ($this->config
->get('cleaner_clean_sessions')) {
$count = $this->database
->delete('sessions')
->condition('timestamp', $this
->getExpirationTime(), '<')
->execute();
if ($count) {
$this->loggerChannel
->info('Old sessions cleared.');
}
}
}
protected function getExpirationTime() {
return (int) ($this->requestTime - session_get_cookie_params()['lifetime']);
}
}