Completed
Pull Request — master (#199)
by Alejandro
02:49
created

ResolveUrlCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.78%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 31
cts 37
cp 0.8378
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

4 Methods

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