From ce7f334326d3d262de80e5a99e82b389b2a83416 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 17 Dec 2025 15:32:21 +0100 Subject: [PATCH] Convert GetTagVisitsCommand into invokable command --- .../src/Command/Tag/GetTagVisitsCommand.php | 69 +++++++++---------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/module/CLI/src/Command/Tag/GetTagVisitsCommand.php b/module/CLI/src/Command/Tag/GetTagVisitsCommand.php index bac12ac2..7db1b7e1 100644 --- a/module/CLI/src/Command/Tag/GetTagVisitsCommand.php +++ b/module/CLI/src/Command/Tag/GetTagVisitsCommand.php @@ -4,61 +4,60 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\Tag; -use Shlinkio\Shlink\CLI\Command\Visit\AbstractVisitsListCommand; -use Shlinkio\Shlink\CLI\Input\DomainOption; -use Shlinkio\Shlink\Common\Paginator\Paginator; -use Shlinkio\Shlink\Common\Util\DateRange; +use Shlinkio\Shlink\CLI\Command\Visit\VisitsCommandUtils; +use Shlinkio\Shlink\CLI\Input\VisitsDateRangeInput; +use Shlinkio\Shlink\CLI\Util\ShlinkTable; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\WithDomainVisitsParams; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Attribute\Argument; +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Attribute\Ask; +use Symfony\Component\Console\Attribute\MapInput; +use Symfony\Component\Console\Attribute\Option; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Style\SymfonyStyle; -use function sprintf; - -class GetTagVisitsCommand extends AbstractVisitsListCommand +#[AsCommand(GetTagVisitsCommand::NAME, 'Returns the list of visits for provided tag')] +class GetTagVisitsCommand extends Command { public const string NAME = 'tag:visits'; - private readonly DomainOption $domainOption; - public function __construct( - VisitsStatsHelperInterface $visitsHelper, + private readonly VisitsStatsHelperInterface $visitsHelper, private readonly ShortUrlStringifierInterface $shortUrlStringifier, ) { - parent::__construct($visitsHelper); - $this->domainOption = new DomainOption($this, sprintf( - 'Return visits that belong to this domain only. Use %s keyword for visits in default domain', - Domain::DEFAULT_AUTHORITY, - )); + parent::__construct(); } - protected function configure(): void - { - $this - ->setName(self::NAME) - ->setDescription('Returns the list of visits for provided tag.') - ->addArgument('tag', InputArgument::REQUIRED, 'The tag which visits we want to get.'); - } - - /** - * @return Paginator - */ - protected function getVisitsPaginator(InputInterface $input, DateRange $dateRange): Paginator - { - $tag = $input->getArgument('tag'); - return $this->visitsHelper->visitsForTag($tag, new WithDomainVisitsParams( - dateRange: $dateRange, - domain: $this->domainOption->get($input), + public function __invoke( + SymfonyStyle $io, + #[Argument('The tag which visits we want to get'), Ask('For what tag do you want to get visits')] string $tag, + #[MapInput] VisitsDateRangeInput $dateRangeInput, + #[Option( + 'Return visits that belong to this domain only. Use ' . Domain::DEFAULT_AUTHORITY . ' keyword for visits ' + . 'in default domain', + shortcut: 'd', + )] + string|null $domain = null, + ): int { + $paginator = $this->visitsHelper->visitsForTag($tag, new WithDomainVisitsParams( + dateRange: $dateRangeInput->toDateRange(), + domain: $domain, )); + [$rows, $headers] = VisitsCommandUtils::resolveRowsAndHeaders($paginator, $this->mapExtraFields(...)); + + ShlinkTable::default($io)->render($headers, $rows); + + return self::SUCCESS; } /** * @return array */ - protected function mapExtraFields(Visit $visit): array + private function mapExtraFields(Visit $visit): array { $shortUrl = $visit->shortUrl; return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];