Updated CLI command to create short URLs so that it respects configs for short code length

This commit is contained in:
Alejandro Celaya 2020-02-18 20:34:48 +01:00
parent 51e130c7a0
commit 33a404f051
5 changed files with 19 additions and 4 deletions

View File

@ -49,7 +49,7 @@
"pugx/shortid-php": "^0.5",
"shlinkio/shlink-common": "^2.7.0",
"shlinkio/shlink-event-dispatcher": "^1.3",
"shlinkio/shlink-installer": "^4.1.0",
"shlinkio/shlink-installer": "^4.2.0",
"shlinkio/shlink-ip-geolocation": "^1.3.1",
"symfony/console": "^5.0",
"symfony/filesystem": "^5.0",

View File

@ -30,6 +30,7 @@ return [
Option\TaskWorkerNumConfigOption::class,
Option\WebWorkerNumConfigOption::class,
Option\RedisServersConfigOption::class,
Option\ShortCodeLengthOption::class,
],
'installation_commands' => [

View File

@ -54,7 +54,11 @@ return [
ConfigAbstractFactory::class => [
GeolocationDbUpdater::class => [DbUpdater::class, Reader::class, 'Shlinkio\Shlink\LocalLockFactory'],
Command\ShortUrl\GenerateShortUrlCommand::class => [Service\UrlShortener::class, 'config.url_shortener.domain'],
Command\ShortUrl\GenerateShortUrlCommand::class => [
Service\UrlShortener::class,
'config.url_shortener.domain',
'config.url_shortener.default_short_codes_length',
],
Command\ShortUrl\ResolveUrlCommand::class => [Service\ShortUrl\ShortUrlResolver::class],
Command\ShortUrl\ListShortUrlsCommand::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'],
Command\ShortUrl\GetVisitsCommand::class => [Service\VisitsTracker::class],

View File

@ -30,12 +30,14 @@ class GenerateShortUrlCommand extends Command
private UrlShortenerInterface $urlShortener;
private array $domainConfig;
private int $defaultShortCodeLength;
public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig)
public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig, int $defaultShortCodeLength)
{
parent::__construct();
$this->urlShortener = $urlShortener;
$this->domainConfig = $domainConfig;
$this->defaultShortCodeLength = $defaultShortCodeLength;
}
protected function configure(): void
@ -87,6 +89,12 @@ class GenerateShortUrlCommand extends Command
'd',
InputOption::VALUE_REQUIRED,
'The domain to which this short URL will be attached.',
)
->addOption(
'shortCodeLength',
'l',
InputOption::VALUE_REQUIRED,
'The length for generated short code (it will be ignored if --customSlug was provided).',
);
}
@ -117,6 +125,7 @@ class GenerateShortUrlCommand extends Command
$tags = unique(flatten(array_map($explodeWithComma, $input->getOption('tags'))));
$customSlug = $input->getOption('customSlug');
$maxVisits = $input->getOption('maxVisits');
$shortCodeLength = $input->getOption('shortCodeLength') ?? $this->defaultShortCodeLength;
try {
$shortUrl = $this->urlShortener->urlToShortCode(
@ -129,6 +138,7 @@ class GenerateShortUrlCommand extends Command
ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits !== null ? (int) $maxVisits : null,
ShortUrlMetaInputFilter::FIND_IF_EXISTS => $input->getOption('findIfExists'),
ShortUrlMetaInputFilter::DOMAIN => $input->getOption('domain'),
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $shortCodeLength,
]),
);

View File

@ -31,7 +31,7 @@ class GenerateShortUrlCommandTest extends TestCase
public function setUp(): void
{
$this->urlShortener = $this->prophesize(UrlShortener::class);
$command = new GenerateShortUrlCommand($this->urlShortener->reveal(), self::DOMAIN_CONFIG);
$command = new GenerateShortUrlCommand($this->urlShortener->reveal(), self::DOMAIN_CONFIG, 5);
$app = new Application();
$app->add($command);
$this->commandTester = new CommandTester($command);