WebformSanitizeSubmissionsCommands.php in Webform 6.x
File
src/Commands/WebformSanitizeSubmissionsCommands.php
View source
<?php
namespace Drupal\webform\Commands;
use Consolidation\AnnotatedCommand\CommandData;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drush\Commands\DrushCommands;
use Drush\Drupal\Commands\sql\SanitizePluginInterface;
use Symfony\Component\Console\Input\InputInterface;
class WebformSanitizeSubmissionsCommands extends DrushCommands implements SanitizePluginInterface {
protected $database;
protected $moduleHandler;
protected $entityTypeManager;
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;
}
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.'));
}
}
public function options($options = [
'sanitize-webform-submissions' => NULL,
]) {
}
public function messages(&$messages, InputInterface $input) {
$options = $input
->getOptions();
if ($this
->isEnabled($options['sanitize-webform-submissions'])) {
$messages[] = dt('Truncate webform submission tables.');
}
}
protected function isEnabled($value) {
return $value !== 'no';
}
}