Reduce hardcoded options in ShortUrlDataInput

This commit is contained in:
Alejandro Celaya 2024-07-27 09:12:54 +02:00
parent 7f9dc10f6a
commit b9ba1246d4
2 changed files with 64 additions and 22 deletions

View file

@ -30,45 +30,46 @@ readonly final class ShortUrlDataInput
$command $command
->addOption( ->addOption(
'tags', ShortUrlDataOption::TAGS->value,
't', ShortUrlDataOption::TAGS->shortcut(),
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Tags to apply to the short URL', 'Tags to apply to the short URL',
) )
->addOption( ->addOption(
'valid-since', ShortUrlDataOption::VALID_SINCE->value,
's', ShortUrlDataOption::VALID_SINCE->shortcut(),
InputOption::VALUE_REQUIRED, InputOption::VALUE_REQUIRED,
'The date from which this short URL will be valid. ' 'The date from which this short URL will be valid. '
. 'If someone tries to access it before this date, it will not be found.', . 'If someone tries to access it before this date, it will not be found.',
) )
->addOption( ->addOption(
'valid-until', ShortUrlDataOption::VALID_UNTIL->value,
'u', ShortUrlDataOption::VALID_UNTIL->shortcut(),
InputOption::VALUE_REQUIRED, InputOption::VALUE_REQUIRED,
'The date until which this short URL will be valid. ' 'The date until which this short URL will be valid. '
. 'If someone tries to access it after this date, it will not be found.', . 'If someone tries to access it after this date, it will not be found.',
) )
->addOption( ->addOption(
'max-visits', ShortUrlDataOption::MAX_VISITS->value,
'm', ShortUrlDataOption::MAX_VISITS->shortcut(),
InputOption::VALUE_REQUIRED, InputOption::VALUE_REQUIRED,
'This will limit the number of visits for this short URL.', 'This will limit the number of visits for this short URL.',
) )
->addOption( ->addOption(
'title', ShortUrlDataOption::TITLE->value,
mode: InputOption::VALUE_REQUIRED, ShortUrlDataOption::TITLE->shortcut(),
description: 'A descriptive title for the short URL.', InputOption::VALUE_REQUIRED,
'A descriptive title for the short URL.',
) )
->addOption( ->addOption(
'crawlable', ShortUrlDataOption::CRAWLABLE->value,
'r', ShortUrlDataOption::CRAWLABLE->shortcut(),
InputOption::VALUE_NONE, InputOption::VALUE_NONE,
'Tells if this short URL will be included as "Allow" in Shlink\'s robots.txt.', 'Tells if this short URL will be included as "Allow" in Shlink\'s robots.txt.',
) )
->addOption( ->addOption(
'no-forward-query', ShortUrlDataOption::NO_FORWARD_QUERY->value,
'w', ShortUrlDataOption::NO_FORWARD_QUERY->shortcut(),
InputOption::VALUE_NONE, InputOption::VALUE_NONE,
'Disables the forwarding of the query string to the long URL, when the short URL is visited.', 'Disables the forwarding of the query string to the long URL, when the short URL is visited.',
); );
@ -106,27 +107,27 @@ readonly final class ShortUrlDataInput
// Avoid setting arguments that were not explicitly provided. // Avoid setting arguments that were not explicitly provided.
// This is important when editing short URLs and should not make a difference when creating. // This is important when editing short URLs and should not make a difference when creating.
if ($input->hasParameterOption(['--valid-since', '-s'])) { if (ShortUrlDataOption::VALID_SINCE->wasProvided($input)) {
$data[ShortUrlInputFilter::VALID_SINCE] = $input->getOption('valid-since'); $data[ShortUrlInputFilter::VALID_SINCE] = $input->getOption('valid-since');
} }
if ($input->hasParameterOption(['--valid-until', '-v'])) { if (ShortUrlDataOption::VALID_UNTIL->wasProvided($input)) {
$data[ShortUrlInputFilter::VALID_UNTIL] = $input->getOption('valid-until'); $data[ShortUrlInputFilter::VALID_UNTIL] = $input->getOption('valid-until');
} }
if ($input->hasParameterOption(['--max-visits', '-m'])) { if (ShortUrlDataOption::MAX_VISITS->wasProvided($input)) {
$maxVisits = $input->getOption('max-visits'); $maxVisits = $input->getOption('max-visits');
$data[ShortUrlInputFilter::MAX_VISITS] = $maxVisits !== null ? (int) $maxVisits : null; $data[ShortUrlInputFilter::MAX_VISITS] = $maxVisits !== null ? (int) $maxVisits : null;
} }
if ($input->hasParameterOption(['--tags', '-t'])) { if (ShortUrlDataOption::TAGS->wasProvided($input)) {
$tags = array_unique(flatten(array_map(splitByComma(...), $input->getOption('tags')))); $tags = array_unique(flatten(array_map(splitByComma(...), $input->getOption('tags'))));
$data[ShortUrlInputFilter::TAGS] = $tags; $data[ShortUrlInputFilter::TAGS] = $tags;
} }
if ($input->hasParameterOption('--title')) { if (ShortUrlDataOption::TITLE->wasProvided($input)) {
$data[ShortUrlInputFilter::TITLE] = $input->getOption('title'); $data[ShortUrlInputFilter::TITLE] = $input->getOption('title');
} }
if ($input->hasParameterOption(['--crawlable', '-r'])) { if (ShortUrlDataOption::CRAWLABLE->wasProvided($input)) {
$data[ShortUrlInputFilter::CRAWLABLE] = $input->getOption('crawlable'); $data[ShortUrlInputFilter::CRAWLABLE] = $input->getOption('crawlable');
} }
if ($input->hasParameterOption(['--no-forward-query', '-w'])) { if (ShortUrlDataOption::NO_FORWARD_QUERY->wasProvided($input)) {
$data[ShortUrlInputFilter::FORWARD_QUERY] = !$input->getOption('no-forward-query'); $data[ShortUrlInputFilter::FORWARD_QUERY] = !$input->getOption('no-forward-query');
} }

View file

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Input;
use Symfony\Component\Console\Input\InputInterface;
use function sprintf;
enum ShortUrlDataOption: string
{
case TAGS = 'tags';
case VALID_SINCE = 'valid-since';
case VALID_UNTIL = 'valid-until';
case MAX_VISITS = 'max-visits';
case TITLE = 'title';
case CRAWLABLE = 'crawlable';
case NO_FORWARD_QUERY = 'no-forward-query';
public function shortcut(): ?string
{
return match ($this) {
self::TAGS => 't',
self::VALID_SINCE => 's',
self::VALID_UNTIL => 'u',
self::MAX_VISITS => 'm',
self::TITLE => null,
self::CRAWLABLE => 'r',
self::NO_FORWARD_QUERY => 'w',
};
}
public function wasProvided(InputInterface $input): bool
{
$option = sprintf('--%s', $this->value);
$shortcut = $this->shortcut();
return $input->hasParameterOption($shortcut === null ? $option : [$option, sprintf('-%s', $shortcut)]);
}
}