Completed
Push — develop ( 9d3653...f17214 )
by Alejandro
16s queued 13s
created

GetVisitsCommand::doConfigure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
ccs 4
cts 4
cp 1
crap 1
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)));
0 ignored issues
show
Bug introduced by
It seems like $shortCode can also be of type null and string[]; however, parameter $shortCode of Shlinkio\Shlink\Core\Ser...rackerInterface::info() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        $paginator = $this->visitsTracker->info(/** @scrutinizer ignore-type */ $shortCode, new VisitsParams(new DateRange($startDate, $endDate)));
Loading history...
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