Completed
Push — master ( a7d308...df23f2 )
by Alejandro
25s queued 11s
created

ResolveUrlCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 85.19%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 51
ccs 23
cts 27
cp 0.8519
rs 10
c 0
b 0
f 0
wmc 7

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 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
    private const ALIASES = ['shortcode:parse', 'short-code:parse'];
23
24
    /** @var UrlShortenerInterface */
25
    private $urlShortener;
26
27 2
    public function __construct(UrlShortenerInterface $urlShortener)
28
    {
29 2
        parent::__construct();
30 2
        $this->urlShortener = $urlShortener;
31
    }
32
33 2
    protected function configure(): void
34
    {
35
        $this
36 2
            ->setName(self::NAME)
37 2
            ->setAliases(self::ALIASES)
38 2
            ->setDescription('Returns the long URL behind a short code')
39 2
            ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse')
40 2
            ->addOption('domain', 'd', InputOption::VALUE_REQUIRED, 'The domain to which the short URL is attached.');
41
    }
42
43 2
    protected function interact(InputInterface $input, OutputInterface $output): void
44
    {
45 2
        $shortCode = $input->getArgument('shortCode');
46 2
        if (! empty($shortCode)) {
47 2
            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 2
    protected function execute(InputInterface $input, OutputInterface $output): ?int
58
    {
59 2
        $io = new SymfonyStyle($input, $output);
60 2
        $shortCode = $input->getArgument('shortCode');
61 2
        $domain = $input->getOption('domain');
62
63
        try {
64 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

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 1
        } catch (ShortUrlNotFoundException $e) {
68 1
            $io->error($e->getMessage());
69 1
            return ExitCodes::EXIT_FAILURE;
70
        }
71
    }
72
}
73