mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 04:03:12 +08:00
Convert ResolveUrlCommand into invokable command
This commit is contained in:
@@ -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: <info>%s</info>', $url->getLongUrl()));
|
||||
$url = $this->urlResolver->resolveShortUrl($identifier);
|
||||
$io->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl()));
|
||||
return self::SUCCESS;
|
||||
} catch (ShortUrlNotFoundException $e) {
|
||||
$io->error($e->getMessage());
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user