diff --git a/config/install-container.php b/config/install-container.php index 0db90897..ca3d273a 100644 --- a/config/install-container.php +++ b/config/install-container.php @@ -1,7 +1,8 @@ InvokableFactory::class, ], 'services' => [ + 'random-chars-generator' => function () { + return str_shuffle(UrlShortenerOptions::DEFAULT_CHARS); + }, 'config' => [ ConfigAbstractFactory::class => [ - DatabaseConfigCustomizer::class => [Filesystem::class], + Plugin\DatabaseConfigCustomizer::class => [Filesystem::class], + Plugin\UrlShortenerConfigCustomizer::class => ['random-chars-generator'], ], ], ], diff --git a/module/Core/src/Options/UrlShortenerOptions.php b/module/Core/src/Options/UrlShortenerOptions.php index 9ac2d0c6..312f1b65 100644 --- a/module/Core/src/Options/UrlShortenerOptions.php +++ b/module/Core/src/Options/UrlShortenerOptions.php @@ -7,7 +7,7 @@ use Zend\Stdlib\AbstractOptions; class UrlShortenerOptions extends AbstractOptions { - public const DEFAULT_CHARS = '123456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'; + public const DEFAULT_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // phpcs:disable protected $__strictMode__ = false; diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index 48dae28e..61dab2c9 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -71,13 +71,13 @@ class UrlShortenerTest extends TestCase */ public function urlIsProperlyShortened(): void { - // 10 -> 12C1c + // 10 -> 0Q1Y $shortUrl = $this->urlShortener->urlToShortCode( new Uri('http://foobar.com/12345/hello?foo=bar'), [], ShortUrlMeta::createEmpty() ); - $this->assertEquals('12C1c', $shortUrl->getShortCode()); + $this->assertEquals('0Q1Y', $shortUrl->getShortCode()); } /** @@ -215,7 +215,6 @@ class UrlShortenerTest extends TestCase */ public function shortCodeIsProperlyParsed(): void { - // 12C1c -> 10 $shortCode = '12C1c'; $shortUrl = new ShortUrl('expected_url'); $shortUrl->setShortCode($shortCode); diff --git a/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php b/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php index 0ef0c8d6..fa718509 100644 --- a/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php @@ -3,13 +3,13 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Installer\Config\Plugin; -use Shlinkio\Shlink\Core\Options\UrlShortenerOptions; use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig; use Shlinkio\Shlink\Installer\Util\AskUtilsTrait; use Symfony\Component\Console\Style\SymfonyStyle; use function array_diff; use function array_keys; -use function str_shuffle; +use function count; +use function Functional\contains; class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface { @@ -30,6 +30,14 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface self::NOT_FOUND_REDIRECT_TO, ]; + /** @var callable */ + private $randomCharsGenerator; + + public function __construct(callable $randomCharsGenerator) + { + $this->randomCharsGenerator = $randomCharsGenerator; + } + public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void { $urlShortener = $appConfig->getUrlShortener(); @@ -40,7 +48,11 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface return; } - $io->title('URL SHORTENER'); + // Print title if there are keys other than "chars" + $onlyKeyIsChars = count($keysToAskFor) === 1 && contains($keysToAskFor, self::CHARS); + if (! $onlyKeyIsChars) { + $io->title('URL SHORTENER'); + } foreach ($keysToAskFor as $key) { // Skip not found redirect URL when the user decided not to redirect if ($key === self::NOT_FOUND_REDIRECT_TO && ! $urlShortener[self::ENABLE_NOT_FOUND_REDIRECTION]) { @@ -64,9 +76,8 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface case self::HOSTNAME: return $this->askRequired($io, 'hostname', 'Hostname for generated URLs'); case self::CHARS: - return $io->ask( - 'Character set for generated short codes (leave empty to autogenerate one)' - ) ?: str_shuffle(UrlShortenerOptions::DEFAULT_CHARS); + // This won't actually ask anything, just generate the chars. Asking for this was confusing for users + return ($this->randomCharsGenerator)(); case self::VALIDATE_URL: return $io->confirm('Do you want to validate long urls by 200 HTTP status code on response'); case self::ENABLE_NOT_FOUND_REDIRECTION: diff --git a/module/Installer/src/Factory/InstallApplicationFactory.php b/module/Installer/src/Factory/InstallApplicationFactory.php index b790bb87..728cf716 100644 --- a/module/Installer/src/Factory/InstallApplicationFactory.php +++ b/module/Installer/src/Factory/InstallApplicationFactory.php @@ -43,7 +43,7 @@ class InstallApplicationFactory implements FactoryInterface $container->get(Filesystem::class), new ConfigCustomizerManager($container, ['factories' => [ Plugin\DatabaseConfigCustomizer::class => ConfigAbstractFactory::class, - Plugin\UrlShortenerConfigCustomizer::class => InvokableFactory::class, + Plugin\UrlShortenerConfigCustomizer::class => ConfigAbstractFactory::class, Plugin\LanguageConfigCustomizer::class => InvokableFactory::class, Plugin\ApplicationConfigCustomizer::class => InvokableFactory::class, ]]), diff --git a/module/Installer/test/Config/Plugin/UrlShortenerConfigCustomizerTest.php b/module/Installer/test/Config/Plugin/UrlShortenerConfigCustomizerTest.php index 18a9940e..2a5079a6 100644 --- a/module/Installer/test/Config/Plugin/UrlShortenerConfigCustomizerTest.php +++ b/module/Installer/test/Config/Plugin/UrlShortenerConfigCustomizerTest.php @@ -21,7 +21,9 @@ class UrlShortenerConfigCustomizerTest extends TestCase { $this->io = $this->prophesize(SymfonyStyle::class); $this->io->title(Argument::any())->willReturn(null); - $this->plugin = new UrlShortenerConfigCustomizer(); + $this->plugin = new UrlShortenerConfigCustomizer(function () { + return 'the_chars'; + }); } /** @@ -40,12 +42,12 @@ class UrlShortenerConfigCustomizerTest extends TestCase $this->assertEquals([ 'SCHEMA' => 'chosen', 'HOSTNAME' => 'asked', - 'CHARS' => 'asked', + 'CHARS' => 'the_chars', 'VALIDATE_URL' => true, 'ENABLE_NOT_FOUND_REDIRECTION' => true, 'NOT_FOUND_REDIRECT_TO' => 'asked', ], $config->getUrlShortener()); - $ask->shouldHaveBeenCalledTimes(3); + $ask->shouldHaveBeenCalledTimes(2); $choice->shouldHaveBeenCalledOnce(); $confirm->shouldHaveBeenCalledTimes(2); } @@ -61,7 +63,6 @@ class UrlShortenerConfigCustomizerTest extends TestCase $config = new CustomizableAppConfig(); $config->setUrlShortener([ 'SCHEMA' => 'foo', - 'HOSTNAME' => 'foo', 'ENABLE_NOT_FOUND_REDIRECTION' => true, 'NOT_FOUND_REDIRECT_TO' => 'foo', ]); @@ -70,8 +71,8 @@ class UrlShortenerConfigCustomizerTest extends TestCase $this->assertEquals([ 'SCHEMA' => 'foo', - 'HOSTNAME' => 'foo', - 'CHARS' => 'asked', + 'HOSTNAME' => 'asked', + 'CHARS' => 'the_chars', 'VALIDATE_URL' => false, 'ENABLE_NOT_FOUND_REDIRECTION' => true, 'NOT_FOUND_REDIRECT_TO' => 'foo',