|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\CLI\Command\ShortUrl; |
|
5
|
|
|
|
|
6
|
|
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; |
|
7
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
14
|
|
|
use function sprintf; |
|
15
|
|
|
|
|
16
|
|
|
class ResolveUrlCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
public const NAME = 'short-url:parse'; |
|
19
|
|
|
private const ALIASES = ['shortcode:parse', 'short-code:parse']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var UrlShortenerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $urlShortener; |
|
25
|
|
|
|
|
26
|
3 |
|
public function __construct(UrlShortenerInterface $urlShortener) |
|
27
|
|
|
{ |
|
28
|
3 |
|
parent::__construct(); |
|
29
|
3 |
|
$this->urlShortener = $urlShortener; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
protected function configure(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this |
|
35
|
3 |
|
->setName(self::NAME) |
|
36
|
3 |
|
->setAliases(self::ALIASES) |
|
37
|
3 |
|
->setDescription('Returns the long URL behind a short code') |
|
38
|
3 |
|
->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
protected function interact(InputInterface $input, OutputInterface $output): void |
|
42
|
|
|
{ |
|
43
|
3 |
|
$shortCode = $input->getArgument('shortCode'); |
|
44
|
3 |
|
if (! empty($shortCode)) { |
|
45
|
3 |
|
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
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output): void |
|
56
|
|
|
{ |
|
57
|
3 |
|
$io = new SymfonyStyle($input, $output); |
|
58
|
3 |
|
$shortCode = $input->getArgument('shortCode'); |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
3 |
|
$url = $this->urlShortener->shortCodeToUrl($shortCode); |
|
|
|
|
|
|
62
|
1 |
|
$output->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl())); |
|
63
|
2 |
|
} catch (InvalidShortCodeException $e) { |
|
64
|
1 |
|
$io->error(sprintf('Provided short code "%s" has an invalid format.', $shortCode)); |
|
|
|
|
|
|
65
|
1 |
|
} catch (EntityDoesNotExistException $e) { |
|
66
|
1 |
|
$io->error(sprintf('Provided short code "%s" could not be found.', $shortCode)); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|