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