Convert GetOrphanVisitsCommand into invokable command

This commit is contained in:
Alejandro Celaya
2025-12-17 15:27:39 +01:00
parent 66d35968f4
commit aecc36a463

View File

@@ -4,64 +4,56 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Visit; namespace Shlinkio\Shlink\CLI\Command\Visit;
use Shlinkio\Shlink\CLI\Input\DomainOption; use Shlinkio\Shlink\CLI\Input\VisitsDateRangeInput;
use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\CLI\Util\ShlinkTable;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitsParams; use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitsParams;
use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitType; use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitType;
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption; 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 Shlinkio\Shlink\Core\enumToString; #[AsCommand(GetOrphanVisitsCommand::NAME, 'Returns the list of orphan visits')]
use function sprintf; class GetOrphanVisitsCommand extends Command
class GetOrphanVisitsCommand extends AbstractVisitsListCommand
{ {
public const string NAME = 'visit:orphan'; public const string NAME = 'visit:orphan';
private readonly DomainOption $domainOption; public function __construct(private readonly VisitsStatsHelperInterface $visitsHelper)
public function __construct(VisitsStatsHelperInterface $visitsHelper)
{ {
parent::__construct($visitsHelper); parent::__construct();
$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,
));
} }
protected function configure(): void public function __invoke(
{ SymfonyStyle $io,
$this #[MapInput] VisitsDateRangeInput $dateRangeInput,
->setName(self::NAME) #[Option(
->setDescription('Returns the list of orphan visits.') 'Return visits that belong to this domain only. Use ' . Domain::DEFAULT_AUTHORITY . ' keyword for visits '
->addOption('type', 't', InputOption::VALUE_REQUIRED, sprintf( . 'in default domain',
'Return visits only with this type. One of %s', shortcut: 'd',
enumToString(OrphanVisitType::class), )]
)); string|null $domain = null,
} #[Option('Return visits only with this type', shortcut: 't')] OrphanVisitType|null $type = null,
): int {
/** $paginator = $this->visitsHelper->orphanVisits(new OrphanVisitsParams(
* @return Paginator<Visit> dateRange: $dateRangeInput->toDateRange(),
*/ domain: $domain,
protected function getVisitsPaginator(InputInterface $input, DateRange $dateRange): Paginator
{
$rawType = $input->getOption('type');
$type = $rawType !== null ? OrphanVisitType::from($rawType) : null;
return $this->visitsHelper->orphanVisits(new OrphanVisitsParams(
dateRange: $dateRange,
domain: $this->domainOption->get($input),
type: $type, type: $type,
)); ));
[$rows, $headers] = VisitsCommandUtils::resolveRowsAndHeaders($paginator, $this->mapExtraFields(...));
ShlinkTable::default($io)->render($headers, $rows);
return self::SUCCESS;
} }
/** /**
* @return array<string, string> * @return array<string, string>
*/ */
protected function mapExtraFields(Visit $visit): array private function mapExtraFields(Visit $visit): array
{ {
return ['type' => $visit->type->value]; return ['type' => $visit->type->value];
} }