Passed
Pull Request — master (#588)
by Alejandro
03:34
created

GetVisitsCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 63
rs 10
ccs 27
cts 31
cp 0.871
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getStartDateDesc() 0 3 1
A interact() 0 11 3
A getEndDateDesc() 0 3 1
A execute() 0 16 1
A doConfigure() 0 7 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
    private const ALIASES = ['shortcode:visits', 'short-code:visits'];
26
27
    /** @var VisitsTrackerInterface */
28
    private $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
            ->setAliases(self::ALIASES)
41 4
            ->setDescription('Returns the detailed visits information for provided short code')
42 4
            ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code which visits we want to get');
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
        $shortCode = $input->getArgument('shortCode');
72 4
        $startDate = $this->getDateOption($input, $output, 'startDate');
73 4
        $endDate = $this->getDateOption($input, $output, 'endDate');
74
75 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

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