From 9d64d4ed1df462ab01b3a99fa6451560d64c42a3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 18 May 2023 09:32:54 +0200 Subject: [PATCH] Create abstract base class for commands deleting visits --- .../ShortUrl/DeleteShortUrlVisitsCommand.php | 18 +++------- .../Visit/AbstractDeleteVisitsCommand.php | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 module/CLI/src/Command/Visit/AbstractDeleteVisitsCommand.php diff --git a/module/CLI/src/Command/ShortUrl/DeleteShortUrlVisitsCommand.php b/module/CLI/src/Command/ShortUrl/DeleteShortUrlVisitsCommand.php index 5bb4d2a5..6cd04bfe 100644 --- a/module/CLI/src/Command/ShortUrl/DeleteShortUrlVisitsCommand.php +++ b/module/CLI/src/Command/ShortUrl/DeleteShortUrlVisitsCommand.php @@ -4,20 +4,19 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\ShortUrl; +use Shlinkio\Shlink\CLI\Command\Visit\AbstractDeleteVisitsCommand; use Shlinkio\Shlink\CLI\Util\ExitCode; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlVisitsDeleterInterface; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use function sprintf; -class DeleteShortUrlVisitsCommand extends Command +class DeleteShortUrlVisitsCommand extends AbstractDeleteVisitsCommand { public const NAME = 'short-url:visits-delete'; @@ -44,15 +43,9 @@ class DeleteShortUrlVisitsCommand extends Command ); } - protected function execute(InputInterface $input, OutputInterface $output): ?int + protected function doExecute(InputInterface $input, SymfonyStyle $io): ?int { $identifier = ShortUrlIdentifier::fromCli($input); - $io = new SymfonyStyle($input, $output); - if (! $this->confirm($io)) { - $io->info('Operation aborted'); - return ExitCode::EXIT_SUCCESS; - } - try { $result = $this->deleter->deleteShortUrlVisits($identifier); $io->success(sprintf('Successfully deleted %s visits', $result->affectedItems)); @@ -64,9 +57,8 @@ class DeleteShortUrlVisitsCommand extends Command } } - private function confirm(SymfonyStyle $io): bool + protected function getWarningMessage(): string { - $io->warning('You are about to delete all visits for a short URL. This operation cannot be undone.'); - return $io->confirm('Continue deleting visits?', false); + return 'You are about to delete all visits for a short URL. This operation cannot be undone.'; } } diff --git a/module/CLI/src/Command/Visit/AbstractDeleteVisitsCommand.php b/module/CLI/src/Command/Visit/AbstractDeleteVisitsCommand.php new file mode 100644 index 00000000..f171d59a --- /dev/null +++ b/module/CLI/src/Command/Visit/AbstractDeleteVisitsCommand.php @@ -0,0 +1,35 @@ +confirm($io)) { + $io->info('Operation aborted'); + return ExitCode::EXIT_SUCCESS; + } + + return $this->doExecute($input, $io); + } + + private function confirm(SymfonyStyle $io): bool + { + $io->warning($this->getWarningMessage()); + return $io->confirm('Continue deleting visits?', false); + } + + abstract protected function doExecute(InputInterface $input, SymfonyStyle $io): ?int; + + abstract protected function getWarningMessage(): string; +}