1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\CLI\Command\ShortUrl; |
6
|
|
|
|
7
|
|
|
use Shlinkio\Shlink\CLI\Util\ExitCodes; |
8
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; |
9
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; |
10
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
17
|
|
|
|
18
|
|
|
use function sprintf; |
19
|
|
|
|
20
|
|
|
class ResolveUrlCommand extends Command |
21
|
|
|
{ |
22
|
|
|
public const NAME = 'short-url:parse'; |
23
|
|
|
|
24
|
|
|
private ShortUrlResolverInterface $urlResolver; |
25
|
|
|
|
26
|
2 |
|
public function __construct(ShortUrlResolverInterface $urlResolver) |
27
|
|
|
{ |
28
|
2 |
|
parent::__construct(); |
29
|
2 |
|
$this->urlResolver = $urlResolver; |
30
|
2 |
|
} |
31
|
|
|
|
32
|
2 |
|
protected function configure(): void |
33
|
|
|
{ |
34
|
|
|
$this |
35
|
2 |
|
->setName(self::NAME) |
36
|
2 |
|
->setDescription('Returns the long URL behind a short code') |
37
|
2 |
|
->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse') |
38
|
2 |
|
->addOption('domain', 'd', InputOption::VALUE_REQUIRED, 'The domain to which the short URL is attached.'); |
39
|
2 |
|
} |
40
|
|
|
|
41
|
2 |
|
protected function interact(InputInterface $input, OutputInterface $output): void |
42
|
|
|
{ |
43
|
2 |
|
$shortCode = $input->getArgument('shortCode'); |
44
|
2 |
|
if (! empty($shortCode)) { |
45
|
2 |
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$io = new SymfonyStyle($input, $output); |
49
|
|
|
$shortCode = $io->ask('A short code was not provided. Which short code do you want to parse?'); |
50
|
|
|
if (! empty($shortCode)) { |
51
|
|
|
$input->setArgument('shortCode', $shortCode); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
56
|
|
|
{ |
57
|
2 |
|
$io = new SymfonyStyle($input, $output); |
58
|
|
|
|
59
|
|
|
try { |
60
|
2 |
|
$url = $this->urlResolver->resolveShortUrl(ShortUrlIdentifier::fromCli($input)); |
61
|
1 |
|
$output->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl())); |
62
|
1 |
|
return ExitCodes::EXIT_SUCCESS; |
63
|
1 |
|
} catch (ShortUrlNotFoundException $e) { |
64
|
1 |
|
$io->error($e->getMessage()); |
65
|
1 |
|
return ExitCodes::EXIT_FAILURE; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|