Passed
Pull Request — master (#357)
by Alejandro
05:24
created

ResolveUrlCommand::execute()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 5
nop 2
crap 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\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
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
    /** @var UrlShortenerInterface */
23
    private $urlShortener;
24
25 3
    public function __construct(UrlShortenerInterface $urlShortener)
26
    {
27 3
        parent::__construct();
28 3
        $this->urlShortener = $urlShortener;
29
    }
30
31 3
    protected function configure(): void
32
    {
33
        $this
34 3
            ->setName(self::NAME)
35 3
            ->setAliases(self::ALIASES)
36 3
            ->setDescription('Returns the long URL behind a short code')
37 3
            ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse');
38
    }
39
40 3
    protected function interact(InputInterface $input, OutputInterface $output): void
41
    {
42 3
        $shortCode = $input->getArgument('shortCode');
43 3
        if (! empty($shortCode)) {
44 3
            return;
45
        }
46
47
        $io = new SymfonyStyle($input, $output);
48
        $shortCode = $io->ask('A short code was not provided. Which short code do you want to parse?');
49
        if (! empty($shortCode)) {
50
            $input->setArgument('shortCode', $shortCode);
51
        }
52
    }
53
54 3
    protected function execute(InputInterface $input, OutputInterface $output): ?int
55
    {
56 3
        $io = new SymfonyStyle($input, $output);
57 3
        $shortCode = $input->getArgument('shortCode');
58
59
        try {
60 3
            $url = $this->urlShortener->shortCodeToUrl($shortCode);
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

60
            $url = $this->urlShortener->shortCodeToUrl(/** @scrutinizer ignore-type */ $shortCode);
Loading history...
61 1
            $output->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl()));
62 1
            return ExitCodes::EXIT_SUCCESS;
63 2
        } catch (InvalidShortCodeException $e) {
64 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

64
            $io->error(sprintf('Provided short code "%s" has an invalid format.', /** @scrutinizer ignore-type */ $shortCode));
Loading history...
65 1
            return ExitCodes::EXIT_FAILURE;
66 1
        } catch (EntityDoesNotExistException $e) {
67 1
            $io->error(sprintf('Provided short code "%s" could not be found.', $shortCode));
68 1
            return ExitCodes::EXIT_FAILURE;
69
        }
70
    }
71
}
72