Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

ResolveUrlCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 24.36 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 75.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 78
ccs 31
cts 41
cp 0.7561
rs 10
wmc 8
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 10 1
A interact() 19 19 3
A execute() 0 21 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
namespace Shlinkio\Shlink\CLI\Command\Shortcode;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
6
use Shlinkio\Shlink\Core\Service\UrlShortener;
7
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Helper\QuestionHelper;
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\Question\Question;
14
use Zend\I18n\Translator\TranslatorInterface;
15
16
class ResolveUrlCommand extends Command
17
{
18
    /**
19
     * @var UrlShortenerInterface
20
     */
21
    private $urlShortener;
22
    /**
23
     * @var TranslatorInterface
24
     */
25
    private $translator;
26
27
    /**
28
     * ResolveUrlCommand constructor.
29
     * @param UrlShortenerInterface $urlShortener
30
     * @param TranslatorInterface $translator
31
     *
32
     * @Inject({UrlShortener::class, "translator"})
33
     */
34 3
    public function __construct(UrlShortenerInterface $urlShortener, TranslatorInterface $translator)
35
    {
36 3
        $this->urlShortener = $urlShortener;
37 3
        $this->translator = $translator;
38 3
        parent::__construct(null);
39 3
    }
40
41 3
    public function configure()
42
    {
43 3
        $this->setName('shortcode:parse')
44 3
             ->setDescription($this->translator->translate('Returns the long URL behind a short code'))
45 3
             ->addArgument(
46 3
                 'shortCode',
47 3
                 InputArgument::REQUIRED,
48 3
                 $this->translator->translate('The short code to parse')
49 3
             );
50 3
    }
51
52 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...
53
    {
54 3
        $shortCode = $input->getArgument('shortCode');
55 3
        if (! empty($shortCode)) {
56 3
            return;
57
        }
58
59
        /** @var QuestionHelper $helper */
60
        $helper = $this->getHelper('question');
61
        $question = new Question(sprintf(
62
            '<question>%s</question> ',
63
            $this->translator->translate('A short code was not provided. Which short code do you want to parse?:')
64
        ));
65
66
        $shortCode = $helper->ask($input, $output, $question);
67
        if (! empty($shortCode)) {
68
            $input->setArgument('shortCode', $shortCode);
69
        }
70
    }
71
72 3
    public function execute(InputInterface $input, OutputInterface $output)
73
    {
74 3
        $shortCode = $input->getArgument('shortCode');
75
76
        try {
77 3
            $longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
78 2
            if (! isset($longUrl)) {
79 1
                $output->writeln(sprintf(
80 1
                    '<error>' . $this->translator->translate('No URL found for short code "%s"') . '</error>',
81
                    $shortCode
82 1
                ));
83 1
                return;
84
            }
85
86 1
            $output->writeln(sprintf('%s <info>%s</info>', $this->translator->translate('Long URL:'), $longUrl));
87 2
        } catch (InvalidShortCodeException $e) {
88 1
            $output->writeln(sprintf('<error>' . $this->translator->translate(
89
                'Provided short code "%s" has an invalid format.'
90 1
            ) . '</error>', $shortCode));
91
        }
92 2
    }
93
}
94