You are here

class WebformSanitizeSubmissionsCommands in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Commands/WebformSanitizeSubmissionsCommands.php \Drupal\webform\Commands\WebformSanitizeSubmissionsCommands

Drush sql-sanitize plugin for sanitizing (truncating) webform submissions.

Hierarchy

Expanded class hierarchy of WebformSanitizeSubmissionsCommands

See also

\Drush\Drupal\Commands\sql\SanitizeSessionsCommands

1 string reference to 'WebformSanitizeSubmissionsCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses WebformSanitizeSubmissionsCommands
webform.sanitize.submissions.commands in ./drush.services.yml
\Drupal\webform\Commands\WebformSanitizeSubmissionsCommands

File

src/Commands/WebformSanitizeSubmissionsCommands.php, line 18

Namespace

Drupal\webform\Commands
View source
class WebformSanitizeSubmissionsCommands extends DrushCommands implements SanitizePluginInterface {

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

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * WebformSanitizeSubmissionsCommands constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(Connection $database, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct();
    $this->database = $database;
    $this->moduleHandler = $module_handler;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Sanitize webform submissions from the DB.
   *
   * @hook post-command sql-sanitize
   *
   * {@inheritdoc}
   */
  public function sanitize($result, CommandData $command_data) {
    $options = $command_data
      ->options();
    if ($this
      ->isEnabled($options['sanitize-webform-submissions'])) {
      $this->database
        ->truncate('webform_submission')
        ->execute();
      $this->database
        ->truncate('webform_submission_data')
        ->execute();
      if ($this->moduleHandler
        ->moduleExists('webform_submission_log')) {
        $this->database
          ->truncate('webform_submission_log')
          ->execute();
      }
      $this->entityTypeManager
        ->getStorage('webform_submission')
        ->resetCache();
      $this
        ->logger()
        ->notice(dt('Webform submission tables truncated.'));
    }
  }

  /**
   * @hook option sql-sanitize
   * @option sanitize-webform-submissions
   *   By default, submissions are truncated. Specify 'no' to disable that.
   */
  public function options($options = [
    'sanitize-webform-submissions' => NULL,
  ]) {
  }

  /**
   * @hook on-event sql-sanitize-confirms
   *
   * {@inheritdoc}
   */
  public function messages(&$messages, InputInterface $input) {
    $options = $input
      ->getOptions();
    if ($this
      ->isEnabled($options['sanitize-webform-submissions'])) {
      $messages[] = dt('Truncate webform submission tables.');
    }
  }

  /**
   * Test an option value to see if it is disabled.
   *
   * @param string $value
   *   The enabled options value.
   *
   * @return bool
   *   TRUE if santize websubmission is enabled.
   */
  protected function isEnabled($value) {
    return $value !== 'no';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformSanitizeSubmissionsCommands::$database protected property The database connection.
WebformSanitizeSubmissionsCommands::$entityTypeManager protected property The entity type manager.
WebformSanitizeSubmissionsCommands::$moduleHandler protected property The module handler.
WebformSanitizeSubmissionsCommands::isEnabled protected function Test an option value to see if it is disabled.
WebformSanitizeSubmissionsCommands::messages public function @hook on-event sql-sanitize-confirms
WebformSanitizeSubmissionsCommands::options public function @hook option sql-sanitize @option sanitize-webform-submissions By default, submissions are truncated. Specify 'no' to disable that.
WebformSanitizeSubmissionsCommands::sanitize public function Sanitize webform submissions from the DB.
WebformSanitizeSubmissionsCommands::__construct public function WebformSanitizeSubmissionsCommands constructor.