diff --git a/module/CLI/src/Command/ShortUrl/ResolveUrlCommand.php b/module/CLI/src/Command/ShortUrl/ResolveUrlCommand.php index b6bf71f7..b70ba70e 100644 --- a/module/CLI/src/Command/ShortUrl/ResolveUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/ResolveUrlCommand.php @@ -4,60 +4,42 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\ShortUrl; -use Shlinkio\Shlink\CLI\Input\ShortUrlIdentifierInput; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; +use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface; +use Symfony\Component\Console\Attribute\Argument; +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Attribute\Ask; +use Symfony\Component\Console\Attribute\Option; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use function sprintf; +#[AsCommand(ResolveUrlCommand::NAME, 'Returns the long URL behind a short code')] class ResolveUrlCommand extends Command { - public const string NAME = 'short-url:parse'; - - private readonly ShortUrlIdentifierInput $shortUrlIdentifierInput; + public const string NAME = 'short-url:resolve'; public function __construct(private readonly ShortUrlResolverInterface $urlResolver) { parent::__construct(); - $this->shortUrlIdentifierInput = new ShortUrlIdentifierInput( - $this, - shortCodeDesc: 'The short code to parse', - domainDesc: 'The domain to which the short URL is attached.', - ); } - protected function configure(): void - { - $this - ->setName(self::NAME) - ->setDescription('Returns the long URL behind a short code'); - } - - protected function interact(InputInterface $input, OutputInterface $output): void - { - $shortCode = $this->shortUrlIdentifierInput->shortCode($input); - if (! empty($shortCode)) { - return; - } - - $io = new SymfonyStyle($input, $output); - $shortCode = $io->ask('A short code was not provided. Which short code do you want to parse?'); - if (! empty($shortCode)) { - $input->setArgument('shortCode', $shortCode); - } - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - $io = new SymfonyStyle($input, $output); + public function __invoke( + SymfonyStyle $io, + #[ + Argument('The short code to resolve'), + Ask('A short code was not provided. Which short code do you want to resolve?'), + ] + string $shortCode, + #[Option('The domain to which the short URL is attached', shortcut: 'd')] string|null $domain = null, + ): int { + $identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, $domain); try { - $url = $this->urlResolver->resolveShortUrl($this->shortUrlIdentifierInput->toShortUrlIdentifier($input)); - $output->writeln(sprintf('Long URL: %s', $url->getLongUrl())); + $url = $this->urlResolver->resolveShortUrl($identifier); + $io->writeln(sprintf('Long URL: %s', $url->getLongUrl())); return self::SUCCESS; } catch (ShortUrlNotFoundException $e) { $io->error($e->getMessage()); diff --git a/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php index 742ae05c..30060dd6 100644 --- a/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php @@ -40,7 +40,7 @@ class ResolveUrlCommandTest extends TestCase ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), )->willReturn($shortUrl); - $this->commandTester->execute(['shortCode' => $shortCode]); + $this->commandTester->execute(['short-code' => $shortCode]); $output = $this->commandTester->getDisplay(); self::assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output); } @@ -68,7 +68,7 @@ class ResolveUrlCommandTest extends TestCase 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); }