dbmanager/src/Console/DeleteUserCommand.php

49 lines
1.4 KiB
PHP

<?php
namespace App\Console;
use App\Service\DatabaseService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
#[AsCommand('user:delete')]
class DeleteUserCommand extends Command
{
public function __construct(
private readonly DatabaseService $db,
)
{
parent::__construct();
}
public function configure(): void
{
$this->addArgument('name', InputArgument::OPTIONAL, 'User name');
$this->addArgument('host', InputArgument::OPTIONAL, 'User host');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$question = $this->getHelper('question');
$host = $input->getArgument('host');
if (!$name = $input->getArgument('name')) {
$selectQuestion = new ChoiceQuestion('User name: ', array_map(
fn($user) => $user['name'],
$this->db->listUsers())
);
$name = $question->ask($input, $output, $selectQuestion);
}
$this->db->deleteUser($name, $host);
$output->writeln(sprintf('User "%s" successfully deleted', $name));
return Command::SUCCESS;
}
}