Completed
Pull Request — master (#199)
by Alejandro
03:59
created

GetVisitsCommand::getDateOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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