Completed
Push — master ( c32e20...5b9784 )
by Alejandro
15s
created

GetVisitsCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 15.31 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.66%

Importance

Changes 0
Metric Value
dl 15
loc 98
rs 10
c 0
b 0
f 0
ccs 52
cts 58
cp 0.8966
wmc 9
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 24 1
A interact() 15 15 3
A execute() 0 26 2
A getDateOption() 0 9 2

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\Common\Util\DateRange;
7
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
use Zend\I18n\Translator\TranslatorInterface;
15
16
class GetVisitsCommand extends Command
17
{
18
    const NAME = 'shortcode:visits';
19
20
    /**
21
     * @var VisitsTrackerInterface
22
     */
23
    private $visitsTracker;
24
    /**
25
     * @var TranslatorInterface
26
     */
27
    private $translator;
28
29 3
    public function __construct(VisitsTrackerInterface $visitsTracker, TranslatorInterface $translator)
30
    {
31 3
        $this->visitsTracker = $visitsTracker;
32 3
        $this->translator = $translator;
33 3
        parent::__construct();
34 3
    }
35
36 3
    public function configure()
37
    {
38 3
        $this->setName(self::NAME)
39 3
            ->setDescription(
40 3
                $this->translator->translate('Returns the detailed visits information for provided short code')
41
            )
42 3
            ->addArgument(
43 3
                'shortCode',
44 3
                InputArgument::REQUIRED,
45 3
                $this->translator->translate('The short code which visits we want to get')
46
            )
47 3
            ->addOption(
48 3
                'startDate',
49 3
                's',
50 3
                InputOption::VALUE_OPTIONAL,
51 3
                $this->translator->translate('Allows to filter visits, returning only those older than start date')
52
            )
53 3
            ->addOption(
54 3
                'endDate',
55 3
                'e',
56 3
                InputOption::VALUE_OPTIONAL,
57 3
                $this->translator->translate('Allows to filter visits, returning only those newer than end date')
58
            );
59 3
    }
60
61 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...
62
    {
63 3
        $shortCode = $input->getArgument('shortCode');
64 3
        if (! empty($shortCode)) {
65 3
            return;
66
        }
67
68
        $io = new SymfonyStyle($input, $output);
69
        $shortCode = $io->ask(
70
            $this->translator->translate('A short code was not provided. Which short code do you want to use?')
71
        );
72
        if (! empty($shortCode)) {
73
            $input->setArgument('shortCode', $shortCode);
74
        }
75
    }
76
77 3
    public function execute(InputInterface $input, OutputInterface $output)
78
    {
79 3
        $io = new SymfonyStyle($input, $output);
80 3
        $shortCode = $input->getArgument('shortCode');
81 3
        $startDate = $this->getDateOption($input, 'startDate');
82 3
        $endDate = $this->getDateOption($input, 'endDate');
83
84 3
        $visits = $this->visitsTracker->info($shortCode, new DateRange($startDate, $endDate));
85 3
        $rows = [];
86 3
        foreach ($visits as $row) {
87 1
            $rowData = $row->jsonSerialize();
88
89
            // Unset location info and remote addr
90 1
            unset($rowData['visitLocation'], $rowData['remoteAddr']);
91
92 1
            $rowData['country'] = $row->getVisitLocation()->getCountryName();
93
94 1
            $rows[] = \array_values($rowData);
95
        }
96 3
        $io->table([
97 3
            $this->translator->translate('Referer'),
98 3
            $this->translator->translate('Date'),
99 3
            $this->translator->translate('User agent'),
100 3
            $this->translator->translate('Country'),
101 3
        ], $rows);
102 3
    }
103
104 3
    protected function getDateOption(InputInterface $input, $key)
105
    {
106 3
        $value = $input->getOption($key);
107 3
        if (! empty($value)) {
108 1
            $value = new \DateTime($value);
109
        }
110
111 3
        return $value;
112
    }
113
}
114