Completed
Push — develop ( 9d3653...f17214 )
by Alejandro
16s queued 13s
created

ResolveUrlCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 48
rs 10
ccs 22
cts 26
cp 0.8462
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A interact() 0 11 3
A __construct() 0 4 1
A configure() 0 7 1
A execute() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
6
7
use Shlinkio\Shlink\CLI\Util\ExitCodes;
8
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
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
23
    private UrlShortenerInterface $urlShortener;
24
25 2
    public function __construct(UrlShortenerInterface $urlShortener)
26
    {
27 2
        parent::__construct();
28 2
        $this->urlShortener = $urlShortener;
29
    }
30
31 2
    protected function configure(): void
32
    {
33
        $this
34 2
            ->setName(self::NAME)
35 2
            ->setDescription('Returns the long URL behind a short code')
36 2
            ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse')
37 2
            ->addOption('domain', 'd', InputOption::VALUE_REQUIRED, 'The domain to which the short URL is attached.');
38
    }
39
40 2
    protected function interact(InputInterface $input, OutputInterface $output): void
41
    {
42 2
        $shortCode = $input->getArgument('shortCode');
43 2
        if (! empty($shortCode)) {
44 2
            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 2
    protected function execute(InputInterface $input, OutputInterface $output): ?int
55
    {
56 2
        $io = new SymfonyStyle($input, $output);
57 2
        $shortCode = $input->getArgument('shortCode');
58 2
        $domain = $input->getOption('domain');
59
60
        try {
61 2
            $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

61
            $url = $this->urlShortener->shortCodeToUrl(/** @scrutinizer ignore-type */ $shortCode, $domain);
Loading history...
62 1
            $output->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl()));
63 1
            return ExitCodes::EXIT_SUCCESS;
64 1
        } catch (ShortUrlNotFoundException $e) {
65 1
            $io->error($e->getMessage());
66 1
            return ExitCodes::EXIT_FAILURE;
67
        }
68
    }
69
}
70