Completed
Pull Request — master (#252)
by Alejandro
03:36 queued 01:06
created

ResolveUrlCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 84.85%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 67
ccs 28
cts 33
cp 0.8485
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A interact() 0 13 3
A __construct() 0 5 1
A configure() 0 10 1
A execute() 0 17 3
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 Zend\I18n\Translator\TranslatorInterface;
15
use function sprintf;
16
17
class ResolveUrlCommand extends Command
18
{
19
    public const NAME = 'short-url:parse';
20
    private const ALIASES = ['shortcode:parse', 'short-code:parse'];
21
22
    /**
23
     * @var UrlShortenerInterface
24
     */
25
    private $urlShortener;
26
    /**
27
     * @var TranslatorInterface
28
     */
29
    private $translator;
30
31 3
    public function __construct(UrlShortenerInterface $urlShortener, TranslatorInterface $translator)
32
    {
33 3
        $this->urlShortener = $urlShortener;
34 3
        $this->translator = $translator;
35 3
        parent::__construct(null);
36
    }
37
38 3
    protected function configure(): void
39
    {
40
        $this
41 3
            ->setName(self::NAME)
42 3
            ->setAliases(self::ALIASES)
43 3
            ->setDescription($this->translator->translate('Returns the long URL behind a short code'))
44 3
            ->addArgument(
45 3
                'shortCode',
46 3
                InputArgument::REQUIRED,
47 3
                $this->translator->translate('The short code to parse')
48
            );
49
    }
50
51 3
    protected function interact(InputInterface $input, OutputInterface $output): void
52
    {
53 3
        $shortCode = $input->getArgument('shortCode');
54 3
        if (! empty($shortCode)) {
55 3
            return;
56
        }
57
58
        $io = new SymfonyStyle($input, $output);
59
        $shortCode = $io->ask(
60
            $this->translator->translate('A short code was not provided. Which short code do you want to parse?')
61
        );
62
        if (! empty($shortCode)) {
63
            $input->setArgument('shortCode', $shortCode);
64
        }
65
    }
66
67 3
    protected function execute(InputInterface $input, OutputInterface $output): void
68
    {
69 3
        $io = new SymfonyStyle($input, $output);
70 3
        $shortCode = $input->getArgument('shortCode');
71
72
        try {
73 3
            $url = $this->urlShortener->shortCodeToUrl($shortCode);
74 1
            $output->writeln(
75 1
                sprintf('%s <info>%s</info>', $this->translator->translate('Long URL:'), $url->getLongUrl())
76
            );
77 2
        } catch (InvalidShortCodeException $e) {
78 1
            $io->error(
79 1
                sprintf($this->translator->translate('Provided short code "%s" has an invalid format.'), $shortCode)
80
            );
81 1
        } catch (EntityDoesNotExistException $e) {
82 1
            $io->error(
83 1
                sprintf($this->translator->translate('Provided short code "%s" could not be found.'), $shortCode)
84
            );
85
        }
86
    }
87
}
88