From 33a404f051835998a50cfcfbba7393ccd70efe1e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 18 Feb 2020 20:34:48 +0100 Subject: [PATCH] Updated CLI command to create short URLs so that it respects configs for short code length --- composer.json | 2 +- config/autoload/installer.global.php | 1 + module/CLI/config/dependencies.config.php | 6 +++++- .../src/Command/ShortUrl/GenerateShortUrlCommand.php | 12 +++++++++++- .../Command/ShortUrl/GenerateShortUrlCommandTest.php | 2 +- 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index cdda9028..52681a0e 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/autoload/installer.global.php b/config/autoload/installer.global.php index 296c0635..c40d75d1 100644 --- a/config/autoload/installer.global.php +++ b/config/autoload/installer.global.php @@ -30,6 +30,7 @@ return [ Option\TaskWorkerNumConfigOption::class, Option\WebWorkerNumConfigOption::class, Option\RedisServersConfigOption::class, + Option\ShortCodeLengthOption::class, ], 'installation_commands' => [ diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 1f94f5a6..1cc67fd9 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -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], diff --git a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php index 28d192b1..7369f1f6 100644 --- a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php @@ -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, ]), ); diff --git a/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php index df1019b1..bcf00acb 100644 --- a/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php @@ -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);