use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
-use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Utils\DrupalApi;
/**
*
* @package Drupal\Console\Command\User
*/
-class DeleteCommand extends Command
+class DeleteCommand extends UserBase
{
- use CommandTrait;
-
- /**
- * @var EntityTypeManagerInterface
- */
- protected $entityTypeManager;
-
/**
* @var QueryFactory
*/
QueryFactory $entityQuery,
DrupalApi $drupalApi
) {
- $this->entityTypeManager = $entityTypeManager;
$this->entityQuery = $entityQuery;
$this->drupalApi = $drupalApi;
- parent::__construct();
+ parent::__construct($entityTypeManager);
}
/**
->setName('user:delete')
->setDescription($this->trans('commands.user.delete.description'))
->addOption(
- 'user-id',
+ 'user',
null,
InputOption::VALUE_OPTIONAL,
- $this->trans('commands.user.delete.options.user-id')
+ $this->trans('commands.user.delete.options.user')
)
->addOption(
'roles',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
$this->trans('commands.user.delete.options.roles')
- );
+ )->setAliases(['ud']);
}
/**
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
-
- $userId = $input->getOption('user-id');
- if (!$userId) {
- $userId = $io->askEmpty(
- $this->trans('commands.user.delete.questions.user-id'),
- null
- );
- $input->setOption('user-id', $userId);
- }
+ $user = $this->getUserOption();
$roles = $input->getOption('roles');
- if (!$userId && !$roles) {
+ if (!$user && !$roles) {
$systemRoles = $this->drupalApi->getRoles(false, false, false);
- $roles = $io->choice(
+ $roles = $this->getIo()->choice(
$this->trans('commands.user.delete.questions.roles'),
array_values($systemRoles),
null,
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
-
- $userId = $input->getOption('user-id');
-
- if ($userId && $userId <= 1) {
- $io->error(
- sprintf(
- $this->trans('commands.user.delete.errors.invalid-user-id'),
- $userId
- )
- );
+ $user = $input->getOption('user');
- return 1;
- }
+ if ($user) {
+ $userEntity = $this->getUserEntity($user);
+ if (!$userEntity) {
+ $this->getIo()->error(
+ sprintf(
+ $this->trans('commands.user.delete.errors.invalid-user'),
+ $user
+ )
+ );
- if ($userId) {
- $user = $this->entityTypeManager
- ->getStorage('user')
- ->load($userId);
+ return 1;
+ }
- if (!$user) {
- $io->error(
+ if ($userEntity->id() <= 1) {
+ $this->getIo()->error(
sprintf(
$this->trans('commands.user.delete.errors.invalid-user'),
- $userId
+ $user
)
);
}
try {
- $user->delete();
- $io->info(
+ $userEntity->delete();
+ $this->getIo()->info(
sprintf(
$this->trans('commands.user.delete.messages.user-deleted'),
- $user->getUsername()
+ $userEntity->getUsername()
)
);
+
+ return 0;
} catch (\Exception $e) {
- $io->error($e->getMessage());
+ $this->getIo()->error($e->getMessage());
return 1;
}
$roles = $input->getOption('roles');
if ($roles) {
- $userStorage = $this->entityTypeManager->getStorage('user');
+ $roles = is_array($roles)?$roles:[$roles];
- $query = $this->entityQuery->get('user');
- $query->condition('roles', is_array($roles)?$roles:[$roles], 'IN');
- $query->condition('uid', 1, '>');
+ $query = $this->entityQuery
+ ->get('user')
+ ->condition('roles', array_values($roles), 'IN')
+ ->condition('uid', 1, '>');
$results = $query->execute();
- $users = $userStorage->loadMultiple($results);
+ $users = $this->entityTypeManager
+ ->getStorage('user')
+ ->loadMultiple($results);
$tableHeader = [
$this->trans('commands.user.debug.messages.user-id'),
];
$tableRows = [];
- foreach ($users as $userId => $user) {
+ foreach ($users as $user => $userEntity) {
try {
- $user->delete();
- $tableRows['success'][] = [$userId, $user->getUsername()];
+ $userEntity->delete();
+ $tableRows['success'][] = [$user, $userEntity->getUsername()];
} catch (\Exception $e) {
- $tableRows['error'][] = [$userId, $user->getUsername()];
- $io->error($e->getMessage());
+ $tableRows['error'][] = [$user, $userEntity->getUsername()];
+ $this->getIo()->error($e->getMessage());
return 1;
}
}
if ($tableRows['success']) {
- $io->table($tableHeader, $tableRows['success']);
- $io->success(
+ $this->getIo()->table($tableHeader, $tableRows['success']);
+ $this->getIo()->success(
sprintf(
$this->trans('commands.user.delete.messages.users-deleted'),
count($tableRows['success'])
)
);
+
+ return 0;
}
}
}