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

GenerateShortcodeCommand::interact()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 6.28

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 2
dl 19
loc 19
ccs 4
cts 14
cp 0.2857
crap 6.28
rs 9.4285
1
<?php
2
namespace Shlinkio\Shlink\CLI\Command\Shortcode;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
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\Diactoros\Uri;
15
use Zend\I18n\Translator\TranslatorInterface;
16
17
class GenerateShortcodeCommand extends Command
18
{
19
    /**
20
     * @var UrlShortenerInterface
21
     */
22
    private $urlShortener;
23
    /**
24
     * @var array
25
     */
26
    private $domainConfig;
27
    /**
28
     * @var TranslatorInterface
29
     */
30
    private $translator;
31
32
    /**
33
     * GenerateShortcodeCommand constructor.
34
     * @param UrlShortenerInterface $urlShortener
35
     * @param TranslatorInterface $translator
36
     * @param array $domainConfig
37
     *
38
     * @Inject({UrlShortener::class, "translator", "config.url_shortener.domain"})
39
     */
40 2 View Code Duplication
    public function __construct(
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...
41
        UrlShortenerInterface $urlShortener,
42
        TranslatorInterface $translator,
43
        array $domainConfig
44
    ) {
45 2
        $this->urlShortener = $urlShortener;
46 2
        $this->translator = $translator;
47 2
        $this->domainConfig = $domainConfig;
48 2
        parent::__construct(null);
49 2
    }
50
51 2
    public function configure()
52
    {
53 2
        $this->setName('shortcode:generate')
54 2
             ->setDescription(
55 2
                 $this->translator->translate('Generates a short code for provided URL and returns the short URL')
56 2
             )
57 2
             ->addArgument('longUrl', InputArgument::REQUIRED, $this->translator->translate('The long URL to parse'));
58 2
    }
59
60 2 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...
61
    {
62 2
        $longUrl = $input->getArgument('longUrl');
63 2
        if (! empty($longUrl)) {
64 2
            return;
65
        }
66
67
        /** @var QuestionHelper $helper */
68
        $helper = $this->getHelper('question');
69
        $question = new Question(sprintf(
70
            '<question>%s</question> ',
71
            $this->translator->translate('A long URL was not provided. Which URL do you want to shorten?:')
72
        ));
73
74
        $longUrl = $helper->ask($input, $output, $question);
75
        if (! empty($longUrl)) {
76
            $input->setArgument('longUrl', $longUrl);
77
        }
78
    }
79
80 2
    public function execute(InputInterface $input, OutputInterface $output)
81
    {
82 2
        $longUrl = $input->getArgument('longUrl');
83
84
        try {
85 2
            if (! isset($longUrl)) {
86
                $output->writeln(sprintf('<error>%s</error>', $this->translator->translate('A URL was not provided!')));
87
                return;
88
            }
89
90 2
            $shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl));
91 1
            $shortUrl = (new Uri())->withPath($shortCode)
92 1
                ->withScheme($this->domainConfig['schema'])
93 1
                ->withHost($this->domainConfig['hostname']);
94
95 1
            $output->writeln([
96 1
                sprintf('%s <info>%s</info>', $this->translator->translate('Processed URL:'), $longUrl),
97 1
                sprintf('%s <info>%s</info>', $this->translator->translate('Generated URL:'), $shortUrl),
98 1
            ]);
99 2
        } catch (InvalidUrlException $e) {
100 1
            $output->writeln(sprintf(
101 1
                '<error>' . $this->translator->translate(
102
                    'Provided URL "%s" is invalid. Try with a different one.'
103 1
                ) . '</error>',
104
                $longUrl
105 1
            ));
106
        }
107 2
    }
108
}
109