Refactored Language and UrlShortener config customizers

This commit is contained in:
Alejandro Celaya 2018-10-03 18:55:20 +02:00
parent d5736756f7
commit 1b5081ae21
3 changed files with 78 additions and 22 deletions

View file

@ -5,23 +5,48 @@ namespace Shlinkio\Shlink\Installer\Config\Plugin;
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
use Symfony\Component\Console\Style\SymfonyStyle;
use function array_diff;
use function array_keys;
class LanguageConfigCustomizer implements ConfigCustomizerInterface
{
private const SUPPORTED_LANGUAGES = ['en', 'es'];
private const DEFAULT_LANG = 'DEFAULT';
private const CLI_LANG = 'CLI';
private const EXPECTED_KEYS = [
self::DEFAULT_LANG,
self::CLI_LANG,
];
public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
{
$io->title('LANGUAGE');
if ($appConfig->hasLanguage() && $io->confirm('Do you want to keep imported language?')) {
$lang = $appConfig->getLanguage();
$keysToAskFor = $appConfig->hasLanguage() && $io->confirm('Do you want to keep imported language?')
? array_diff(self::EXPECTED_KEYS, array_keys($lang))
: self::EXPECTED_KEYS;
if (empty($keysToAskFor)) {
return;
}
$appConfig->setLanguage([
'DEFAULT' => $this->chooseLanguage($io, 'Select default language for the application in general'),
'CLI' => $this->chooseLanguage($io, 'Select default language for CLI executions'),
]);
foreach ($keysToAskFor as $key) {
$lang[$key] = $this->ask($io, $key);
}
$appConfig->setLanguage($lang);
}
private function ask(SymfonyStyle $io, string $key)
{
switch ($key) {
case self::DEFAULT_LANG:
return $this->chooseLanguage($io, 'Select default language for the application in general');
case self::CLI_LANG:
return $this->chooseLanguage($io, 'Select default language for CLI executions');
}
return '';
}
private function chooseLanguage(SymfonyStyle $io, string $message): string

View file

@ -7,31 +7,62 @@ use Shlinkio\Shlink\Core\Service\UrlShortener;
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;
class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface
{
use AskUtilsTrait;
private const SCHEMA = 'SCHEMA';
private const HOSTNAME = 'HOSTNAME';
private const CHARS = 'CHARS';
private const VALIDATE_URL = 'VALIDATE_URL';
private const EXPECTED_KEYS = [
self::SCHEMA,
self::HOSTNAME,
self::CHARS,
self::VALIDATE_URL,
];
public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
{
$io->title('URL SHORTENER');
if ($appConfig->hasUrlShortener() && $io->confirm('Do you want to keep imported URL shortener config?')) {
$urlShortener = $appConfig->getUrlShortener();
$diffKeys = $appConfig->hasUrlShortener() && $io->confirm('Do you want to keep imported URL shortener config?');
$keysToAskFor = $diffKeys ? array_diff(self::EXPECTED_KEYS, array_keys($urlShortener)) : self::EXPECTED_KEYS;
if (empty($keysToAskFor)) {
return;
}
// Ask for URL shortener params
$appConfig->setUrlShortener([
'SCHEMA' => $io->choice(
'Select schema for generated short URLs',
['http', 'https'],
'http'
),
'HOSTNAME' => $this->askRequired($io, 'hostname', 'Hostname for generated URLs'),
'CHARS' => $io->ask('Character set for generated short codes (leave empty to autogenerate one)')
?: str_shuffle(UrlShortener::DEFAULT_CHARS),
'VALIDATE_URL' => $io->confirm('Do you want to validate long urls by 200 HTTP status code on response'),
]);
foreach ($keysToAskFor as $key) {
$urlShortener[$key] = $this->ask($io, $key);
}
$appConfig->setUrlShortener($urlShortener);
}
private function ask(SymfonyStyle $io, string $key)
{
switch ($key) {
case self::SCHEMA:
return $io->choice(
'Select schema for generated short URLs',
['http', 'https'],
'http'
);
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(UrlShortener::DEFAULT_CHARS);
case self::VALIDATE_URL:
return $io->confirm('Do you want to validate long urls by 200 HTTP status code on response');
}
return '';
}
}

View file

@ -117,7 +117,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface
'DISABLE_TRACK_PARAM' => $array['app_options']['disable_track_param'] ?? null,
]);
$this->deserializeDatabase($array['entity_manager']['connection'] ?? []);
$this->setDatabase($this->deserializeDatabase($array['entity_manager']['connection'] ?? []));
$this->setLanguage([
'DEFAULT' => $array['translator']['locale'] ?? null,
@ -132,10 +132,10 @@ final class CustomizableAppConfig implements ArraySerializableInterface
]);
}
private function deserializeDatabase(array $conn): void
private function deserializeDatabase(array $conn): array
{
if (! isset($conn['driver'])) {
return;
return [];
}
$driver = $conn['driver'];
@ -148,7 +148,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface
$params['PORT'] = $conn['port'] ?? null;
}
$this->setDatabase($params);
return $params;
}
public function getArrayCopy(): array