From 36cb760a88434216cb2140d6a5d7defe9fe78a50 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 15 Dec 2025 09:47:16 +0100 Subject: [PATCH] Convert DeleteShortUrlCommand into invokable command --- .../ShortUrl/DeleteShortUrlCommand.php | 47 +++++++------------ .../Command/ShortUrl/EditShortUrlCommand.php | 2 +- .../ShortUrl/DeleteShortUrlCommandTest.php | 8 ++-- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/module/CLI/src/Command/ShortUrl/DeleteShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/DeleteShortUrlCommand.php index e6a11ea1..9d578729 100644 --- a/module/CLI/src/Command/ShortUrl/DeleteShortUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/DeleteShortUrlCommand.php @@ -4,53 +4,40 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\ShortUrl; -use Shlinkio\Shlink\CLI\Input\ShortUrlIdentifierInput; use Shlinkio\Shlink\Core\Exception; use Shlinkio\Shlink\Core\ShortUrl\DeleteShortUrlServiceInterface; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; +use Symfony\Component\Console\Attribute\Argument; +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Attribute\Option; use Symfony\Component\Console\Command\Command; -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; +#[AsCommand(name: DeleteShortUrlCommand::NAME, description: 'Deletes a short URL')] class DeleteShortUrlCommand extends Command { public const string NAME = 'short-url:delete'; - private readonly ShortUrlIdentifierInput $shortUrlIdentifierInput; - public function __construct(private readonly DeleteShortUrlServiceInterface $deleteShortUrlService) { parent::__construct(); - $this->shortUrlIdentifierInput = new ShortUrlIdentifierInput( - $this, - shortCodeDesc: 'The short code for the short URL to be deleted', - domainDesc: 'The domain if the short code does not belong to the default one', - ); } - protected function configure(): void - { - $this - ->setName(self::NAME) - ->setDescription('Deletes a short URL') - ->addOption( - 'ignore-threshold', - 'i', - InputOption::VALUE_NONE, - 'Ignores the safety visits threshold check, which could make short URLs with many visits to be ' - . 'accidentally deleted', - ); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - $io = new SymfonyStyle($input, $output); - $identifier = $this->shortUrlIdentifierInput->toShortUrlIdentifier($input); - $ignoreThreshold = $input->getOption('ignore-threshold'); + public function __invoke( + SymfonyStyle $io, + #[Argument('The short code for the short URL to be deleted')] string $shortCode, + #[Option('TThe domain if the short code does not belong to the default one', shortcut: 'd')] + string|null $domain = null, + #[Option( + 'Ignores the safety visits threshold check, which could make short URLs with many visits to be ' + . 'accidentally deleted', + shortcut: 'i', + )] + bool $ignoreThreshold = false, + ): int { + $identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, $domain); try { $this->runDelete($io, $identifier, $ignoreThreshold); diff --git a/module/CLI/src/Command/ShortUrl/EditShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/EditShortUrlCommand.php index 7bdd82e2..f4e3b06e 100644 --- a/module/CLI/src/Command/ShortUrl/EditShortUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/EditShortUrlCommand.php @@ -36,8 +36,8 @@ class EditShortUrlCommand extends Command public function __invoke( SymfonyStyle $io, - #[Argument('The short code to edit')] string $shortCode, #[MapInput] ShortUrlDataInput $data, + #[Argument('The short code to edit')] string $shortCode, #[Option('The domain to which the short URL is attached', shortcut: 'd')] string|null $domain = null, #[Option('The long URL to set', shortcut: 'l')] string|null $longUrl = null, ): int { diff --git a/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php index 62872123..c8d94388 100644 --- a/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php @@ -39,7 +39,7 @@ class DeleteShortUrlCommandTest extends TestCase $this->isFalse(), ); - $this->commandTester->execute(['shortCode' => $shortCode]); + $this->commandTester->execute(['short-code' => $shortCode]); $output = $this->commandTester->getDisplay(); self::assertStringContainsString( @@ -58,7 +58,7 @@ class DeleteShortUrlCommandTest extends TestCase $this->isFalse(), )->willThrowException(Exception\ShortUrlNotFoundException::fromNotFound($identifier)); - $this->commandTester->execute(['shortCode' => $shortCode]); + $this->commandTester->execute(['short-code' => $shortCode]); $output = $this->commandTester->getDisplay(); self::assertStringContainsString(sprintf('No URL found with short code "%s"', $shortCode), $output); @@ -88,7 +88,7 @@ class DeleteShortUrlCommandTest extends TestCase }); $this->commandTester->setInputs($retryAnswer); - $this->commandTester->execute(['shortCode' => $shortCode]); + $this->commandTester->execute(['short-code' => $shortCode]); $output = $this->commandTester->getDisplay(); self::assertStringContainsString(sprintf( @@ -118,7 +118,7 @@ class DeleteShortUrlCommandTest extends TestCase )); $this->commandTester->setInputs(['no']); - $this->commandTester->execute(['shortCode' => $shortCode]); + $this->commandTester->execute(['short-code' => $shortCode]); $output = $this->commandTester->getDisplay(); self::assertStringContainsString(sprintf(