1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\CLI\Command\ShortUrl; |
5
|
|
|
|
6
|
|
|
use Shlinkio\Shlink\CLI\Util\ExitCodes; |
7
|
|
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; |
8
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
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
|
|
|
private const ALIASES = ['shortcode:parse', 'short-code:parse']; |
23
|
|
|
|
24
|
|
|
/** @var UrlShortenerInterface */ |
25
|
|
|
private $urlShortener; |
26
|
|
|
|
27
|
3 |
|
public function __construct(UrlShortenerInterface $urlShortener) |
28
|
|
|
{ |
29
|
3 |
|
parent::__construct(); |
30
|
3 |
|
$this->urlShortener = $urlShortener; |
31
|
|
|
} |
32
|
|
|
|
33
|
3 |
|
protected function configure(): void |
34
|
|
|
{ |
35
|
|
|
$this |
36
|
3 |
|
->setName(self::NAME) |
37
|
3 |
|
->setAliases(self::ALIASES) |
38
|
3 |
|
->setDescription('Returns the long URL behind a short code') |
39
|
3 |
|
->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse') |
40
|
3 |
|
->addOption('domain', 'd', InputOption::VALUE_REQUIRED, 'The domain to which the short URL is attached.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
protected function interact(InputInterface $input, OutputInterface $output): void |
44
|
|
|
{ |
45
|
3 |
|
$shortCode = $input->getArgument('shortCode'); |
46
|
3 |
|
if (! empty($shortCode)) { |
47
|
3 |
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$io = new SymfonyStyle($input, $output); |
51
|
|
|
$shortCode = $io->ask('A short code was not provided. Which short code do you want to parse?'); |
52
|
|
|
if (! empty($shortCode)) { |
53
|
|
|
$input->setArgument('shortCode', $shortCode); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
58
|
|
|
{ |
59
|
3 |
|
$io = new SymfonyStyle($input, $output); |
60
|
3 |
|
$shortCode = $input->getArgument('shortCode'); |
61
|
3 |
|
$domain = $input->getOption('domain'); |
62
|
|
|
|
63
|
|
|
try { |
64
|
3 |
|
$url = $this->urlShortener->shortCodeToUrl($shortCode, $domain); |
|
|
|
|
65
|
1 |
|
$output->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl())); |
66
|
1 |
|
return ExitCodes::EXIT_SUCCESS; |
67
|
2 |
|
} catch (InvalidShortCodeException $e) { |
68
|
1 |
|
$io->error(sprintf('Provided short code "%s" has an invalid format.', $shortCode)); |
|
|
|
|
69
|
1 |
|
return ExitCodes::EXIT_FAILURE; |
70
|
1 |
|
} catch (EntityDoesNotExistException $e) { |
71
|
1 |
|
$io->error(sprintf('Provided short code "%s" could not be found.', $shortCode)); |
72
|
1 |
|
return ExitCodes::EXIT_FAILURE; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|