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