Completed
Push — master ( ccb9d5...d7e68d )
by Alejandro
09:04
created

ResolveUrlCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 25.33 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 72.09%

Importance

Changes 0
Metric Value
dl 19
loc 75
rs 10
c 0
b 0
f 0
ccs 31
cts 43
cp 0.7209
wmc 9
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
B execute() 0 25 4

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