Completed
Push — master ( a81ac8...05e307 )
by Alejandro
25s queued 13s
created

ResolveUrlCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 54
rs 10
ccs 26
cts 30
cp 0.8667
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A interact() 0 11 3
A __construct() 0 4 1
A configure() 0 8 1
A execute() 0 16 3
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);
0 ignored issues
show
Bug introduced by
It seems like $shortCode can also be of type null and string[]; however, parameter $shortCode of Shlinkio\Shlink\Core\Ser...rface::shortCodeToUrl() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
            $url = $this->urlShortener->shortCodeToUrl(/** @scrutinizer ignore-type */ $shortCode, $domain);
Loading history...
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));
0 ignored issues
show
Bug introduced by
It seems like $shortCode can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            $io->error(sprintf('Provided short code "%s" has an invalid format.', /** @scrutinizer ignore-type */ $shortCode));
Loading history...
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