mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 04:03:12 +08:00
56 lines
2.0 KiB
PHP
56 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Shlinkio\Shlink\CLI\Command\Domain;
|
|
|
|
use Shlinkio\Shlink\CLI\Command\Visit\VisitsCommandUtils;
|
|
use Shlinkio\Shlink\CLI\Input\VisitsDateRangeInput;
|
|
use Shlinkio\Shlink\CLI\Util\ShlinkTable;
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
|
|
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
|
|
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\Command\Command;
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
#[AsCommand(GetDomainVisitsCommand::NAME, 'Returns the list of visits for provided domain')]
|
|
class GetDomainVisitsCommand extends Command
|
|
{
|
|
public const string NAME = 'domain:visits';
|
|
|
|
public function __construct(
|
|
private readonly VisitsStatsHelperInterface $visitsHelper,
|
|
private readonly ShortUrlStringifierInterface $shortUrlStringifier,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function __invoke(
|
|
SymfonyStyle $io,
|
|
#[Argument('The domain which visits we want to get'), Ask('For what domain do you want to get visits?')]
|
|
string $domain,
|
|
#[MapInput] VisitsDateRangeInput $dateRangeInput,
|
|
): int {
|
|
$paginator = $this->visitsHelper->visitsForDomain($domain, new VisitsParams($dateRangeInput->toDateRange()));
|
|
[$rows, $headers] = VisitsCommandUtils::resolveRowsAndHeaders($paginator, $this->mapExtraFields(...));
|
|
|
|
ShlinkTable::default($io)->render($headers, $rows);
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function mapExtraFields(Visit $visit): array
|
|
{
|
|
$shortUrl = $visit->shortUrl;
|
|
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
|
|
}
|
|
}
|