Completed
Push — master ( 96faaf...c1c325 )
by Alejandro
03:51
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\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\Question;
15
use Zend\Diactoros\Uri;
16
use Zend\I18n\Translator\TranslatorInterface;
17
18
class GenerateShortcodeCommand extends Command
19
{
20
    /**
21
     * @var UrlShortenerInterface
22
     */
23
    private $urlShortener;
24
    /**
25
     * @var array
26
     */
27
    private $domainConfig;
28
    /**
29
     * @var TranslatorInterface
30
     */
31
    private $translator;
32
33
    /**
34
     * GenerateShortcodeCommand constructor.
35
     * @param UrlShortenerInterface $urlShortener
36
     * @param TranslatorInterface $translator
37
     * @param array $domainConfig
38
     *
39
     * @Inject({UrlShortener::class, "translator", "config.url_shortener.domain"})
40
     */
41 2
    public function __construct(
42
        UrlShortenerInterface $urlShortener,
43
        TranslatorInterface $translator,
44
        array $domainConfig
45
    ) {
46 2
        $this->urlShortener = $urlShortener;
47 2
        $this->translator = $translator;
48 2
        $this->domainConfig = $domainConfig;
49 2
        parent::__construct(null);
50 2
    }
51
52 2
    public function configure()
53
    {
54 2
        $this->setName('shortcode:generate')
55 2
             ->setDescription(
56 2
                 $this->translator->translate('Generates a short code for provided URL and returns the short URL')
57 2
             )
58 2
             ->addArgument('longUrl', InputArgument::REQUIRED, $this->translator->translate('The long URL to parse'))
59 2
             ->addOption(
60 2
                 'tags',
61 2
                 't',
62 2
                 InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
63 2
                 $this->translator->translate('Tags to apply to the new short URL')
64 2
             );
65 2
    }
66
67 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...
68
    {
69 2
        $longUrl = $input->getArgument('longUrl');
70 2
        if (! empty($longUrl)) {
71 2
            return;
72
        }
73
74
        /** @var QuestionHelper $helper */
75
        $helper = $this->getHelper('question');
76
        $question = new Question(sprintf(
77
            '<question>%s</question> ',
78
            $this->translator->translate('A long URL was not provided. Which URL do you want to shorten?:')
79
        ));
80
81
        $longUrl = $helper->ask($input, $output, $question);
82
        if (! empty($longUrl)) {
83
            $input->setArgument('longUrl', $longUrl);
84
        }
85
    }
86
87 2
    public function execute(InputInterface $input, OutputInterface $output)
88
    {
89 2
        $longUrl = $input->getArgument('longUrl');
90 2
        $tags = $input->getOption('tags');
91 2
        $processedTags = [];
92 2
        foreach ($tags as $key => $tag) {
93
            $explodedTags = explode(',', $tag);
94
            $processedTags = array_merge($processedTags, $explodedTags);
95 2
        }
96 2
        $tags = $processedTags;
97
98
        try {
99 2
            if (! isset($longUrl)) {
100
                $output->writeln(sprintf('<error>%s</error>', $this->translator->translate('A URL was not provided!')));
101
                return;
102
            }
103
104 2
            $shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags);
105 1
            $shortUrl = (new Uri())->withPath($shortCode)
106 1
                                   ->withScheme($this->domainConfig['schema'])
107 1
                                   ->withHost($this->domainConfig['hostname']);
108
109 1
            $output->writeln([
110 1
                sprintf('%s <info>%s</info>', $this->translator->translate('Processed URL:'), $longUrl),
111 1
                sprintf('%s <info>%s</info>', $this->translator->translate('Generated URL:'), $shortUrl),
112 1
            ]);
113 2
        } catch (InvalidUrlException $e) {
114 1
            $output->writeln(sprintf(
115 1
                '<error>' . $this->translator->translate(
116
                    'Provided URL "%s" is invalid. Try with a different one.'
117 1
                ) . '</error>',
118
                $longUrl
119 1
            ));
120
        }
121 2
    }
122
}
123