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