diff --git a/module/CLI/src/Command/ShortUrl/GetVisitsCommand.php b/module/CLI/src/Command/ShortUrl/GetVisitsCommand.php index b58ea3ac..0b7de663 100644 --- a/module/CLI/src/Command/ShortUrl/GetVisitsCommand.php +++ b/module/CLI/src/Command/ShortUrl/GetVisitsCommand.php @@ -21,6 +21,7 @@ use Symfony\Component\Console\Style\SymfonyStyle; use function Functional\map; use function Functional\select_keys; +use function sprintf; class GetVisitsCommand extends AbstractWithDateRangeCommand { @@ -39,18 +40,18 @@ class GetVisitsCommand extends AbstractWithDateRangeCommand $this ->setName(self::NAME) ->setDescription('Returns the detailed visits information for provided short code') - ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code which visits we want to get') - ->addOption('domain', 'd', InputOption::VALUE_REQUIRED, 'The domain for the short code'); + ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code which visits we want to get.') + ->addOption('domain', 'd', InputOption::VALUE_REQUIRED, 'The domain for the short code.'); } - protected function getStartDateDesc(): string + protected function getStartDateDesc(string $optionName): string { - return 'Allows to filter visits, returning only those older than start date'; + return sprintf('Allows to filter visits, returning only those older than "%s".', $optionName); } - protected function getEndDateDesc(): string + protected function getEndDateDesc(string $optionName): string { - return 'Allows to filter visits, returning only those newer than end date'; + return sprintf('Allows to filter visits, returning only those newer than "%s".', $optionName); } protected function interact(InputInterface $input, OutputInterface $output): void @@ -70,8 +71,8 @@ class GetVisitsCommand extends AbstractWithDateRangeCommand protected function execute(InputInterface $input, OutputInterface $output): ?int { $identifier = ShortUrlIdentifier::fromCli($input); - $startDate = $this->getDateOption($input, $output, 'startDate'); - $endDate = $this->getDateOption($input, $output, 'endDate'); + $startDate = $this->getStartDateOption($input, $output); + $endDate = $this->getEndDateOption($input, $output); $paginator = $this->visitsTracker->info($identifier, new VisitsParams(new DateRange($startDate, $endDate))); diff --git a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php index 3f539e27..cf20e328 100644 --- a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php +++ b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php @@ -60,28 +60,33 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand 'page', 'p', InputOption::VALUE_REQUIRED, - 'The first page to list (10 items per page unless "--all" is provided)', + 'The first page to list (10 items per page unless "--all" is provided).', '1', ) - ->addOption( - 'searchTerm', + ->addOptionWithDeprecatedFallback( + 'search-term', 'st', InputOption::VALUE_REQUIRED, - 'A query used to filter results by searching for it on the longUrl and shortCode fields', + 'A query used to filter results by searching for it on the longUrl and shortCode fields.', ) ->addOption( 'tags', 't', InputOption::VALUE_REQUIRED, - 'A comma-separated list of tags to filter results', + 'A comma-separated list of tags to filter results.', ) - ->addOption( - 'orderBy', + ->addOptionWithDeprecatedFallback( + 'order-by', 'o', InputOption::VALUE_REQUIRED, - 'The field from which we want to order by. Pass ASC or DESC separated by a comma', + 'The field from which we want to order by. Pass ASC or DESC separated by a comma.', + ) + ->addOptionWithDeprecatedFallback( + 'show-tags', + null, + InputOption::VALUE_NONE, + 'Whether to display the tags or not.', ) - ->addOption('showTags', null, InputOption::VALUE_NONE, 'Whether to display the tags or not') ->addOption( 'all', 'a', @@ -91,14 +96,14 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand ); } - protected function getStartDateDesc(): string + protected function getStartDateDesc(string $optionName): string { - return 'Allows to filter short URLs, returning only those created after "startDate"'; + return sprintf('Allows to filter short URLs, returning only those created after "%s".', $optionName); } - protected function getEndDateDesc(): string + protected function getEndDateDesc(string $optionName): string { - return 'Allows to filter short URLs, returning only those created before "endDate"'; + return sprintf('Allows to filter short URLs, returning only those created before "%s".', $optionName); } protected function execute(InputInterface $input, OutputInterface $output): ?int @@ -106,13 +111,13 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand $io = new SymfonyStyle($input, $output); $page = (int) $input->getOption('page'); - $searchTerm = $input->getOption('searchTerm'); + $searchTerm = $this->getOptionWithDeprecatedFallback($input, 'search-term'); $tags = $input->getOption('tags'); $tags = ! empty($tags) ? explode(',', $tags) : []; - $showTags = (bool) $input->getOption('showTags'); - $all = (bool) $input->getOption('all'); - $startDate = $this->getDateOption($input, $output, 'startDate'); - $endDate = $this->getDateOption($input, $output, 'endDate'); + $showTags = $this->getOptionWithDeprecatedFallback($input, 'show-tags'); + $all = $input->getOption('all'); + $startDate = $this->getStartDateOption($input, $output); + $endDate = $this->getEndDateOption($input, $output); $orderBy = $this->processOrderBy($input); $data = [ @@ -178,7 +183,7 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand */ private function processOrderBy(InputInterface $input) { - $orderBy = $input->getOption('orderBy'); + $orderBy = $this->getOptionWithDeprecatedFallback($input, 'order-by'); if (empty($orderBy)) { return null; } diff --git a/module/CLI/src/Command/Util/AbstractWithDateRangeCommand.php b/module/CLI/src/Command/Util/AbstractWithDateRangeCommand.php index bd64701a..39e60c9a 100644 --- a/module/CLI/src/Command/Util/AbstractWithDateRangeCommand.php +++ b/module/CLI/src/Command/Util/AbstractWithDateRangeCommand.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\Util; use Cake\Chronos\Chronos; -use Symfony\Component\Console\Command\Command; +use Shlinkio\Shlink\CLI\Command\BaseCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -13,19 +13,42 @@ use Throwable; use function sprintf; -abstract class AbstractWithDateRangeCommand extends Command +abstract class AbstractWithDateRangeCommand extends BaseCommand { + private const START_DATE = 'start-date'; + private const END_DATE = 'end-date'; + final protected function configure(): void { $this->doConfigure(); $this - ->addOption('startDate', 's', InputOption::VALUE_REQUIRED, $this->getStartDateDesc()) - ->addOption('endDate', 'e', InputOption::VALUE_REQUIRED, $this->getEndDateDesc()); + ->addOptionWithDeprecatedFallback( + self::START_DATE, + 's', + InputOption::VALUE_REQUIRED, + $this->getStartDateDesc(self::START_DATE), + ) + ->addOptionWithDeprecatedFallback( + self::END_DATE, + 'e', + InputOption::VALUE_REQUIRED, + $this->getEndDateDesc(self::END_DATE), + ); } - protected function getDateOption(InputInterface $input, OutputInterface $output, string $key): ?Chronos + protected function getStartDateOption(InputInterface $input, OutputInterface $output): ?Chronos { - $value = $input->getOption($key); + return $this->getDateOption($input, $output, self::START_DATE); + } + + protected function getEndDateOption(InputInterface $input, OutputInterface $output): ?Chronos + { + return $this->getDateOption($input, $output, self::END_DATE); + } + + private function getDateOption(InputInterface $input, OutputInterface $output, string $key): ?Chronos + { + $value = $this->getOptionWithDeprecatedFallback($input, $key); if (empty($value)) { return null; } @@ -49,6 +72,7 @@ abstract class AbstractWithDateRangeCommand extends Command abstract protected function doConfigure(): void; - abstract protected function getStartDateDesc(): string; - abstract protected function getEndDateDesc(): string; + abstract protected function getStartDateDesc(string $optionName): string; + + abstract protected function getEndDateDesc(string $optionName): string; } diff --git a/module/CLI/test/Command/ShortUrl/GetVisitsCommandTest.php b/module/CLI/test/Command/ShortUrl/GetVisitsCommandTest.php index 50c1751f..a4239bd2 100644 --- a/module/CLI/test/Command/ShortUrl/GetVisitsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GetVisitsCommandTest.php @@ -92,7 +92,7 @@ class GetVisitsCommandTest extends TestCase $info->shouldHaveBeenCalledOnce(); self::assertStringContainsString( - sprintf('Ignored provided "startDate" since its value "%s" is not a valid date', $startDate), + sprintf('Ignored provided "start-date" since its value "%s" is not a valid date', $startDate), $output, ); }