Updated installer so that it no longer asks for a charset and instead just generates one

This commit is contained in:
Alejandro Celaya 2019-02-03 13:02:12 +01:00
parent 79c132219b
commit 2d18ef5cee
6 changed files with 35 additions and 19 deletions

View file

@ -1,7 +1,8 @@
<?php
declare(strict_types=1);
use Shlinkio\Shlink\Installer\Config\Plugin\DatabaseConfigCustomizer;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Installer\Config\Plugin;
use Shlinkio\Shlink\Installer\Factory\InstallApplicationFactory;
use Symfony\Component\Console\Application;
use Symfony\Component\Filesystem\Filesystem;
@ -19,9 +20,13 @@ $container = new ServiceManager([
Filesystem::class => 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'],
],
],
],

View file

@ -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;

View file

@ -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);

View file

@ -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;
}
// 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:

View file

@ -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,
]]),

View file

@ -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',