Completed
Push — master ( 30297a...987919 )
by Alejandro
13s
created

ResolveUrlCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 22.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 15
loc 68
ccs 30
cts 36
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 10 1
A interact() 15 15 3
A execute() 0 20 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    const NAME = 'shortcode:parse';
19
20
    /**
21
     * @var UrlShortenerInterface
22
     */
23
    private $urlShortener;
24
    /**
25
     * @var TranslatorInterface
26
     */
27
    private $translator;
28
29 3
    public function __construct(UrlShortenerInterface $urlShortener, TranslatorInterface $translator)
30
    {
31 3
        $this->urlShortener = $urlShortener;
32 3
        $this->translator = $translator;
33 3
        parent::__construct(null);
34 3
    }
35
36 3
    public function configure()
37
    {
38 3
        $this->setName(self::NAME)
39 3
             ->setDescription($this->translator->translate('Returns the long URL behind a short code'))
40 3
             ->addArgument(
41 3
                 'shortCode',
42 3
                 InputArgument::REQUIRED,
43 3
                 $this->translator->translate('The short code to parse')
44
             );
45 3
    }
46
47 3 View Code Duplication
    public function interact(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49 3
        $shortCode = $input->getArgument('shortCode');
50 3
        if (! empty($shortCode)) {
51 3
            return;
52
        }
53
54
        $io = new SymfonyStyle($input, $output);
55
        $shortCode = $io->ask(
56
            $this->translator->translate('A short code was not provided. Which short code do you want to parse?')
57
        );
58
        if (! empty($shortCode)) {
59
            $input->setArgument('shortCode', $shortCode);
60
        }
61
    }
62
63 3
    public function execute(InputInterface $input, OutputInterface $output)
64
    {
65 3
        $io = new SymfonyStyle($input, $output);
66 3
        $shortCode = $input->getArgument('shortCode');
67
68
        try {
69 3
            $url = $this->urlShortener->shortCodeToUrl($shortCode);
70 1
            $output->writeln(
71 1
                \sprintf('%s <info>%s</info>', $this->translator->translate('Long URL:'), $url->getLongUrl())
72
            );
73 2
        } catch (InvalidShortCodeException $e) {
74 1
            $io->error(
75 1
                \sprintf($this->translator->translate('Provided short code "%s" has an invalid format.'), $shortCode)
76
            );
77 1
        } catch (EntityDoesNotExistException $e) {
78 1
            $io->error(
79 1
                \sprintf($this->translator->translate('Provided short code "%s" could not be found.'), $shortCode)
80
            );
81
        }
82 3
    }
83
}
84