|
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) |
|
|
|
|
|
|
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
|
|
|
|
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.