Completed
Push — master ( aca90e...8ef0e7 )
by Alejandro
10s
created

GetVisitsCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 2
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
ccs 18
cts 18
cp 1
crap 2
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
            // Unset location info
89 1
            unset($rowData['visitLocation']);
90
91 1
            $rows[] = \array_values($rowData);
92
        }
93 3
        $io->table([
94 3
            $this->translator->translate('Referer'),
95 3
            $this->translator->translate('Date'),
96 3
            $this->translator->translate('Remote Address'),
97 3
            $this->translator->translate('User agent'),
98 3
        ], $rows);
99 3
    }
100
101 3
    protected function getDateOption(InputInterface $input, $key)
102
    {
103 3
        $value = $input->getOption($key);
104 3
        if (! empty($value)) {
105 1
            $value = new \DateTime($value);
106
        }
107
108 3
        return $value;
109
    }
110
}
111