From 1b5081ae21a15b29662ad888f1acb56450020ba9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 3 Oct 2018 18:55:20 +0200 Subject: [PATCH] Refactored Language and UrlShortener config customizers --- .../Plugin/LanguageConfigCustomizer.php | 35 ++++++++++-- .../Plugin/UrlShortenerConfigCustomizer.php | 57 ++++++++++++++----- .../src/Model/CustomizableAppConfig.php | 8 +-- 3 files changed, 78 insertions(+), 22 deletions(-) diff --git a/module/Installer/src/Config/Plugin/LanguageConfigCustomizer.php b/module/Installer/src/Config/Plugin/LanguageConfigCustomizer.php index 24d57b3c..429f3b9e 100644 --- a/module/Installer/src/Config/Plugin/LanguageConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/LanguageConfigCustomizer.php @@ -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 diff --git a/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php b/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php index a59210d7..e54bcdfb 100644 --- a/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php @@ -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 ''; } } diff --git a/module/Installer/src/Model/CustomizableAppConfig.php b/module/Installer/src/Model/CustomizableAppConfig.php index cbb50d30..27a7dcd0 100644 --- a/module/Installer/src/Model/CustomizableAppConfig.php +++ b/module/Installer/src/Model/CustomizableAppConfig.php @@ -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