You are here

class CleanerWatchdogClearEventSubscriber in Cleaner 8.2

Class CleanerWatchdogClearEventSubscriber.

@package Drupal\cleaner\EventSubscriber

Hierarchy

Expanded class hierarchy of CleanerWatchdogClearEventSubscriber

1 file declares its use of CleanerWatchdogClearEventSubscriber
CleanerKernelTests.php in tests/src/Kernel/CleanerKernelTests.php
1 string reference to 'CleanerWatchdogClearEventSubscriber'
cleaner.services.yml in ./cleaner.services.yml
cleaner.services.yml
1 service uses CleanerWatchdogClearEventSubscriber
cleaner.watchdog_clear_subscriber in ./cleaner.services.yml
Drupal\cleaner\EventSubscriber\CleanerWatchdogClearEventSubscriber

File

src/EventSubscriber/CleanerWatchdogClearEventSubscriber.php, line 18

Namespace

Drupal\cleaner\EventSubscriber
View source
class CleanerWatchdogClearEventSubscriber implements EventSubscriberInterface, ContainerInjectionInterface {

  /**
   * Database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Config object.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Logger channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $loggerChannel;

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[CleanerRunEvent::CLEANER_RUN][] = [
      'clearWatchdog',
      100,
    ];
    return $events;
  }

  /**
   * CleanerWatchdogClearEventSubscriber constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   Database connection.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_channel_factory
   *   Logger channel factory.
   */
  public function __construct(Connection $database, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_channel_factory) {
    $this->database = $database;
    $this->config = $config_factory
      ->get('cleaner.settings');
    $this->loggerChannel = $logger_channel_factory
      ->get('cleaner');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'), $container
      ->get('config.factory'), $container
      ->get('logger.factory'));
  }

  /**
   * Cleaner tables clearing.
   */
  public function clearWatchdog() {
    if ($this->config
      ->get('cleaner_empty_watchdog')) {
      if (!$this->database
        ->schema()
        ->tableExists('watchdog')) {
        $this->loggerChannel
          ->error("Something going wrong - watchdog logs cannot be cleared.");
      }
      else {
        $this->database
          ->query('TRUNCATE {watchdog}')
          ->execute();
        $this->loggerChannel
          ->info('Watchdog logs has been successfully cleared.');
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CleanerWatchdogClearEventSubscriber::$config protected property Config object.
CleanerWatchdogClearEventSubscriber::$database protected property Database connection.
CleanerWatchdogClearEventSubscriber::$loggerChannel protected property Logger channel.
CleanerWatchdogClearEventSubscriber::clearWatchdog public function Cleaner tables clearing.
CleanerWatchdogClearEventSubscriber::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
CleanerWatchdogClearEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CleanerWatchdogClearEventSubscriber::__construct public function CleanerWatchdogClearEventSubscriber constructor.