Completed
Pull Request — master (#199)
by Alejandro
03:59
created

ResolveUrlCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
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