diff --git a/module/CLI/src/Command/Domain/DomainRedirectsCommand.php b/module/CLI/src/Command/Domain/DomainRedirectsCommand.php index fc94ee49..59deb35e 100644 --- a/module/CLI/src/Command/Domain/DomainRedirectsCommand.php +++ b/module/CLI/src/Command/Domain/DomainRedirectsCommand.php @@ -9,9 +9,9 @@ use Shlinkio\Shlink\Core\Domain\DomainServiceInterface; use Shlinkio\Shlink\Core\Domain\Model\DomainItem; use Symfony\Component\Console\Attribute\Argument; use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Attribute\Interact; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use function array_filter; @@ -32,7 +32,8 @@ class DomainRedirectsCommand extends Command parent::__construct(); } - protected function interact(InputInterface $input, OutputInterface $output): void + #[Interact] + public function askDomain(InputInterface $input, SymfonyStyle $io): void { /** @var string|null $domain */ $domain = $input->getArgument('domain'); @@ -40,7 +41,6 @@ class DomainRedirectsCommand extends Command return; } - $io = new SymfonyStyle($input, $output); $askNewDomain = static fn () => $io->ask('Domain authority for which you want to set specific redirects'); /** @var string[] $availableDomains */ diff --git a/module/CLI/src/Command/Integration/MatomoSendVisitsCommand.php b/module/CLI/src/Command/Integration/MatomoSendVisitsCommand.php index f5d8e84c..b45c0135 100644 --- a/module/CLI/src/Command/Integration/MatomoSendVisitsCommand.php +++ b/module/CLI/src/Command/Integration/MatomoSendVisitsCommand.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\Integration; -use Cake\Chronos\Chronos; use Shlinkio\Shlink\Core\Matomo\MatomoOptions; use Shlinkio\Shlink\Core\Matomo\MatomoVisitSenderInterface; use Shlinkio\Shlink\Core\Matomo\VisitSendingProgressTrackerInterface; @@ -17,10 +16,12 @@ use Throwable; use function Shlinkio\Shlink\Common\buildDateRange; use function Shlinkio\Shlink\Core\dateRangeToHumanFriendly; +use function Shlinkio\Shlink\Core\normalizeOptionalDate; use function sprintf; #[AsCommand( name: MatomoSendVisitsCommand::NAME, + description: 'Send existing visits to the configured matomo instance', help: <<setDescription(sprintf( - '%sSend existing visits to the configured matomo instance', - $this->matomoEnabled ? '' : '[MATOMO INTEGRATION DISABLED] ', - )); - } - public function __invoke( SymfonyStyle $io, InputInterface $input, @@ -81,8 +74,8 @@ class MatomoSendVisitsCommand extends Command implements VisitSendingProgressTra // TODO Validate provided date formats $dateRange = buildDateRange( - startDate: $since !== null ? Chronos::parse($since) : null, - endDate: $until !== null ? Chronos::parse($until) : null, + startDate: normalizeOptionalDate($since), + endDate: normalizeOptionalDate($until), ); if ($input->isInteractive()) { diff --git a/module/CLI/src/Command/RedirectRule/ManageRedirectRulesCommand.php b/module/CLI/src/Command/RedirectRule/ManageRedirectRulesCommand.php index 9a129e9d..81e41497 100644 --- a/module/CLI/src/Command/RedirectRule/ManageRedirectRulesCommand.php +++ b/module/CLI/src/Command/RedirectRule/ManageRedirectRulesCommand.php @@ -4,48 +4,41 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\RedirectRule; -use Shlinkio\Shlink\CLI\Input\ShortUrlIdentifierInput; use Shlinkio\Shlink\CLI\RedirectRule\RedirectRuleHandlerInterface; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\RedirectRule\ShortUrlRedirectRuleServiceInterface; +use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface; +use Symfony\Component\Console\Attribute\Argument; +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Attribute\Option; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use function sprintf; +#[AsCommand( + name: ManageRedirectRulesCommand::NAME, + description: 'Set redirect rules for a short URL', +)] class ManageRedirectRulesCommand extends Command { public const string NAME = 'short-url:manage-rules'; - private readonly ShortUrlIdentifierInput $shortUrlIdentifierInput; - public function __construct( protected readonly ShortUrlResolverInterface $shortUrlResolver, protected readonly ShortUrlRedirectRuleServiceInterface $ruleService, protected readonly RedirectRuleHandlerInterface $ruleHandler, ) { parent::__construct(); - $this->shortUrlIdentifierInput = new ShortUrlIdentifierInput( - $this, - shortCodeDesc: 'The short code which rules we want to set.', - domainDesc: 'The domain for the short code.', - ); } - protected function configure(): void - { - $this - ->setName(self::NAME) - ->setDescription('Set redirect rules for a short URL'); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - $io = new SymfonyStyle($input, $output); - $identifier = $this->shortUrlIdentifierInput->toShortUrlIdentifier($input); + public function __invoke( + SymfonyStyle $io, + #[Argument('The short code which rules we want to set')] string $shortCode, + #[Option('The domain of the short code', shortcut: 'd')] string|null $domain = null, + ): int { + $identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, $domain); try { $shortUrl = $this->shortUrlResolver->resolveShortUrl($identifier); diff --git a/module/CLI/test/Command/RedirectRule/ManageRedirectRulesCommandTest.php b/module/CLI/test/Command/RedirectRule/ManageRedirectRulesCommandTest.php index 5cb45a4b..ac5073f4 100644 --- a/module/CLI/test/Command/RedirectRule/ManageRedirectRulesCommandTest.php +++ b/module/CLI/test/Command/RedirectRule/ManageRedirectRulesCommandTest.php @@ -48,7 +48,7 @@ class ManageRedirectRulesCommandTest extends TestCase $this->ruleService->expects($this->never())->method('saveRulesForShortUrl'); $this->ruleHandler->expects($this->never())->method('manageRules'); - $exitCode = $this->commandTester->execute(['shortCode' => 'foo']); + $exitCode = $this->commandTester->execute(['short-code' => 'foo']); $output = $this->commandTester->getDisplay(); self::assertEquals(Command::FAILURE, $exitCode); @@ -67,7 +67,7 @@ class ManageRedirectRulesCommandTest extends TestCase $this->ruleHandler->expects($this->once())->method('manageRules')->willReturn(null); $this->ruleService->expects($this->never())->method('saveRulesForShortUrl'); - $exitCode = $this->commandTester->execute(['shortCode' => 'foo']); + $exitCode = $this->commandTester->execute(['short-code' => 'foo']); $output = $this->commandTester->getDisplay(); self::assertEquals(Command::SUCCESS, $exitCode); @@ -86,7 +86,7 @@ class ManageRedirectRulesCommandTest extends TestCase $this->ruleHandler->expects($this->once())->method('manageRules')->willReturn([]); $this->ruleService->expects($this->once())->method('saveRulesForShortUrl')->with($shortUrl, []); - $exitCode = $this->commandTester->execute(['shortCode' => 'foo']); + $exitCode = $this->commandTester->execute(['short-code' => 'foo']); $output = $this->commandTester->getDisplay(); self::assertEquals(Command::SUCCESS, $exitCode);