mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 15:23:12 +08:00
Create command to edit existing short URLs
This commit is contained in:
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Input;
|
||||
|
||||
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlEdition;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
@@ -53,6 +55,11 @@ readonly final class ShortUrlDataInput
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'This will limit the number of visits for this short URL.',
|
||||
)
|
||||
->addOption(
|
||||
'title',
|
||||
mode: InputOption::VALUE_REQUIRED,
|
||||
description: 'A descriptive title for the short URL.',
|
||||
)
|
||||
->addOption(
|
||||
'crawlable',
|
||||
'r',
|
||||
@@ -67,59 +74,46 @@ readonly final class ShortUrlDataInput
|
||||
);
|
||||
}
|
||||
|
||||
public function longUrl(InputInterface $input): ?string
|
||||
{
|
||||
return $this->longUrlAsOption ? $input->getOption('long-url') : $input->getArgument('longUrl');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function tags(InputInterface $input): array
|
||||
{
|
||||
return array_unique(flatten(array_map(splitByComma(...), $input->getOption('tags'))));
|
||||
}
|
||||
|
||||
public function validSince(InputInterface $input): ?string
|
||||
{
|
||||
return $input->getOption('valid-since');
|
||||
}
|
||||
|
||||
public function validUntil(InputInterface $input): ?string
|
||||
{
|
||||
return $input->getOption('valid-until');
|
||||
}
|
||||
|
||||
public function maxVisits(InputInterface $input): ?int
|
||||
{
|
||||
$maxVisits = $input->getOption('max-visits');
|
||||
return $maxVisits !== null ? (int) $maxVisits : null;
|
||||
}
|
||||
|
||||
public function crawlable(InputInterface $input): bool
|
||||
{
|
||||
return $input->getOption('crawlable');
|
||||
}
|
||||
|
||||
public function noForwardQuery(InputInterface $input): bool
|
||||
{
|
||||
return $input->getOption('no-forward-query');
|
||||
}
|
||||
|
||||
public function toShortUrlEdition(InputInterface $input): ShortUrlEdition
|
||||
{
|
||||
return ShortUrlEdition::fromRawData([
|
||||
ShortUrlInputFilter::LONG_URL => $this->longUrl($input),
|
||||
ShortUrlInputFilter::VALID_SINCE => $this->validSince($input),
|
||||
ShortUrlInputFilter::VALID_UNTIL => $this->validUntil($input),
|
||||
ShortUrlInputFilter::MAX_VISITS => $this->maxVisits($input),
|
||||
ShortUrlInputFilter::TAGS => $this->tags($input),
|
||||
ShortUrlInputFilter::CRAWLABLE => $this->crawlable($input),
|
||||
ShortUrlInputFilter::FORWARD_QUERY => !$this->noForwardQuery($input),
|
||||
// ShortUrlInputFilter::TITLE => TODO,
|
||||
]);
|
||||
return ShortUrlEdition::fromRawData($this->getCommonData($input));
|
||||
}
|
||||
|
||||
// TODO
|
||||
// public function toShortUrlCreation(InputInterface $input)
|
||||
public function toShortUrlCreation(
|
||||
InputInterface $input,
|
||||
UrlShortenerOptions $options,
|
||||
string $customSlugField,
|
||||
string $shortCodeLengthField,
|
||||
string $pathPrefixField,
|
||||
string $findIfExistsField,
|
||||
string $domainField,
|
||||
): ShortUrlCreation {
|
||||
$shortCodeLength = $input->getOption($shortCodeLengthField) ?? $options->defaultShortCodesLength;
|
||||
return ShortUrlCreation::fromRawData([
|
||||
...$this->getCommonData($input),
|
||||
ShortUrlInputFilter::CUSTOM_SLUG => $input->getOption($customSlugField),
|
||||
ShortUrlInputFilter::SHORT_CODE_LENGTH => $shortCodeLength,
|
||||
ShortUrlInputFilter::PATH_PREFIX => $input->getOption($pathPrefixField),
|
||||
ShortUrlInputFilter::FIND_IF_EXISTS => $input->getOption($findIfExistsField),
|
||||
ShortUrlInputFilter::DOMAIN => $input->getOption($domainField),
|
||||
], $options);
|
||||
}
|
||||
|
||||
private function getCommonData(InputInterface $input): array
|
||||
{
|
||||
$longUrl = $this->longUrlAsOption ? $input->getOption('long-url') : $input->getArgument('longUrl');
|
||||
$tags = array_unique(flatten(array_map(splitByComma(...), $input->getOption('tags'))));
|
||||
$maxVisits = $input->getOption('max-visits');
|
||||
|
||||
return [
|
||||
ShortUrlInputFilter::LONG_URL => $longUrl,
|
||||
ShortUrlInputFilter::VALID_SINCE => $input->getOption('valid-since'),
|
||||
ShortUrlInputFilter::VALID_UNTIL => $input->getOption('valid-until'),
|
||||
ShortUrlInputFilter::MAX_VISITS => $maxVisits !== null ? (int) $maxVisits : null,
|
||||
ShortUrlInputFilter::TAGS => $tags,
|
||||
ShortUrlInputFilter::TITLE => $input->getOption('title'),
|
||||
ShortUrlInputFilter::CRAWLABLE => $input->getOption('crawlable'),
|
||||
ShortUrlInputFilter::FORWARD_QUERY => !$input->getOption('no-forward-query'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user