You are here

class CleanerTablesClearEventSubscriber in Cleaner 8.2

Class CleanerTablesClearEventSubscriber.

@package Drupal\cleaner\EventSubscriber

Hierarchy

Expanded class hierarchy of CleanerTablesClearEventSubscriber

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

File

src/EventSubscriber/CleanerTablesClearEventSubscriber.php, line 19

Namespace

Drupal\cleaner\EventSubscriber
View source
class CleanerTablesClearEventSubscriber 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][] = [
      'clearTables',
      100,
    ];
    return $events;
  }

  /**
   * CleanerTablesClearEventSubscriber 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 clearTables() {
    if ($this->config
      ->get('cleaner_additional_tables') != '') {
      $cleared = 0;
      $tables = $this
        ->getAdditionalTables();
      foreach ($tables as $table) {
        if (!$this->database
          ->schema()
          ->tableExists($table)) {
          continue;
        }
        if ($this->database
          ->query("TRUNCATE {$table}")
          ->execute()) {
          $cleared++;
        }
      }
      $this->loggerChannel
        ->info('Cleaner cleared @count additional tables', [
        '@count' => $cleared,
      ]);
    }
  }

  /**
   * Get an additional tables for clearing.
   *
   * @return array
   *   Additional tables array.
   */
  protected function getAdditionalTables() {
    $tables = [];
    $additional = $this->config
      ->get('cleaner_additional_tables');
    $additional = self::explode($additional);
    foreach ($additional as $table) {
      if ($this->database
        ->schema()
        ->tableExists($table)) {
        $tables[] = $table;
      }
    }
    return $tables;
  }

  /**
   * Explode the string into the array.
   *
   * @param string $string
   *   String to be exploded.
   *
   * @return array
   *   Exploded string in array format.
   */
  private static function explode($string = '') {
    return is_string($string) && !empty($string) ? explode(',', self::sanitize($string)) : [];
  }

  /**
   * Sanitize the string.
   *
   * @param string $input
   *   Input to be sanitized.
   *
   * @return string|null
   *   Sanitized string.
   */
  private static function sanitize($input = '') {
    return !empty($input) && is_string($input) ? Xss::filter(trim($input)) : NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CleanerTablesClearEventSubscriber::$config protected property Config object.
CleanerTablesClearEventSubscriber::$database protected property Database connection.
CleanerTablesClearEventSubscriber::$loggerChannel protected property Logger channel.
CleanerTablesClearEventSubscriber::clearTables public function Cleaner tables clearing.
CleanerTablesClearEventSubscriber::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
CleanerTablesClearEventSubscriber::explode private static function Explode the string into the array.
CleanerTablesClearEventSubscriber::getAdditionalTables protected function Get an additional tables for clearing.
CleanerTablesClearEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CleanerTablesClearEventSubscriber::sanitize private static function Sanitize the string.
CleanerTablesClearEventSubscriber::__construct public function CleanerTablesClearEventSubscriber constructor.