1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\CLI\Command\ShortUrl; |
6
|
|
|
|
7
|
|
|
use Shlinkio\Shlink\CLI\Command\Util\AbstractWithDateRangeCommand; |
8
|
|
|
use Shlinkio\Shlink\CLI\Util\ExitCodes; |
9
|
|
|
use Shlinkio\Shlink\CLI\Util\ShlinkTable; |
10
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange; |
11
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit; |
12
|
|
|
use Shlinkio\Shlink\Core\Model\VisitsParams; |
13
|
|
|
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
18
|
|
|
|
19
|
|
|
use function Functional\map; |
20
|
|
|
use function Functional\select_keys; |
21
|
|
|
|
22
|
|
|
class GetVisitsCommand extends AbstractWithDateRangeCommand |
23
|
|
|
{ |
24
|
|
|
public const NAME = 'short-url:visits'; |
25
|
|
|
|
26
|
|
|
private VisitsTrackerInterface $visitsTracker; |
27
|
|
|
|
28
|
4 |
|
public function __construct(VisitsTrackerInterface $visitsTracker) |
29
|
|
|
{ |
30
|
4 |
|
$this->visitsTracker = $visitsTracker; |
31
|
4 |
|
parent::__construct(); |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
protected function doConfigure(): void |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
4 |
|
->setName(self::NAME) |
38
|
4 |
|
->setDescription('Returns the detailed visits information for provided short code') |
39
|
4 |
|
->addArgument('shortCode', InputArgument::REQUIRED, 'The short code which visits we want to get'); |
40
|
|
|
} |
41
|
|
|
|
42
|
4 |
|
protected function getStartDateDesc(): string |
43
|
|
|
{ |
44
|
4 |
|
return 'Allows to filter visits, returning only those older than start date'; |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
protected function getEndDateDesc(): string |
48
|
|
|
{ |
49
|
4 |
|
return 'Allows to filter visits, returning only those newer than end date'; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
protected function interact(InputInterface $input, OutputInterface $output): void |
53
|
|
|
{ |
54
|
4 |
|
$shortCode = $input->getArgument('shortCode'); |
55
|
4 |
|
if (! empty($shortCode)) { |
56
|
4 |
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$io = new SymfonyStyle($input, $output); |
60
|
|
|
$shortCode = $io->ask('A short code was not provided. Which short code do you want to use?'); |
61
|
|
|
if (! empty($shortCode)) { |
62
|
|
|
$input->setArgument('shortCode', $shortCode); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
67
|
|
|
{ |
68
|
4 |
|
$shortCode = $input->getArgument('shortCode'); |
69
|
4 |
|
$startDate = $this->getDateOption($input, $output, 'startDate'); |
70
|
4 |
|
$endDate = $this->getDateOption($input, $output, 'endDate'); |
71
|
|
|
|
72
|
4 |
|
$paginator = $this->visitsTracker->info($shortCode, new VisitsParams(new DateRange($startDate, $endDate))); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$rows = map($paginator->getCurrentItems(), function (Visit $visit) { |
75
|
1 |
|
$rowData = $visit->jsonSerialize(); |
76
|
1 |
|
$rowData['country'] = $visit->getVisitLocation()->getCountryName(); |
77
|
1 |
|
return select_keys($rowData, ['referer', 'date', 'userAgent', 'country']); |
78
|
4 |
|
}); |
79
|
4 |
|
ShlinkTable::fromOutput($output)->render(['Referer', 'Date', 'User agent', 'Country'], $rows); |
80
|
|
|
|
81
|
4 |
|
return ExitCodes::EXIT_SUCCESS; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|