diff --git a/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php b/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php index 6eb5fabf..0e5d4a00 100644 --- a/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php @@ -19,11 +19,15 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface public const HOSTNAME = 'HOSTNAME'; public const CHARS = 'CHARS'; public const VALIDATE_URL = 'VALIDATE_URL'; + public const ENABLE_NOT_FOUND_REDIRECTION = 'ENABLE_NOT_FOUND_REDIRECTION'; + public const NOT_FOUND_REDIRECT_TO = 'NOT_FOUND_REDIRECT_TO'; private const EXPECTED_KEYS = [ self::SCHEMA, self::HOSTNAME, self::CHARS, self::VALIDATE_URL, + self::ENABLE_NOT_FOUND_REDIRECTION, + self::NOT_FOUND_REDIRECT_TO, ]; public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void @@ -38,6 +42,11 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface $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]) { + continue; + } + $urlShortener[$key] = $this->ask($io, $key); } $appConfig->setUrlShortener($urlShortener); @@ -60,6 +69,18 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface ) ?: 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'); + case self::ENABLE_NOT_FOUND_REDIRECTION: + return $io->confirm( + 'Do you want to enable a redirection to a custom URL when a user hits an invalid short URL? ' . + '(If not enabled, the user will see a default "404 not found" page)', + false + ); + case self::NOT_FOUND_REDIRECT_TO: + return $this->askRequired( + $io, + 'redirect URL', + 'Custom URL to redirect to when a user hits an invalid short URL' + ); } return ''; diff --git a/module/Installer/src/Model/CustomizableAppConfig.php b/module/Installer/src/Model/CustomizableAppConfig.php index 2e307f3f..f347519f 100644 --- a/module/Installer/src/Model/CustomizableAppConfig.php +++ b/module/Installer/src/Model/CustomizableAppConfig.php @@ -146,6 +146,16 @@ final class CustomizableAppConfig implements ArraySerializableInterface UrlShortenerConfigCustomizer::HOSTNAME => ['url_shortener', 'domain', 'hostname'], UrlShortenerConfigCustomizer::CHARS => ['url_shortener', 'shortcode_chars'], UrlShortenerConfigCustomizer::VALIDATE_URL => ['url_shortener', 'validate_url'], + UrlShortenerConfigCustomizer::ENABLE_NOT_FOUND_REDIRECTION => [ + 'url_shortener', + 'not_found_short_url', + 'enable_redirection', + ], + UrlShortenerConfigCustomizer::NOT_FOUND_REDIRECT_TO => [ + 'url_shortener', + 'not_found_short_url', + 'redirect_to', + ], ], $pathCollection)); } @@ -191,6 +201,11 @@ final class CustomizableAppConfig implements ArraySerializableInterface ], 'shortcode_chars' => $this->urlShortener[UrlShortenerConfigCustomizer::CHARS] ?? '', 'validate_url' => $this->urlShortener[UrlShortenerConfigCustomizer::VALIDATE_URL] ?? true, + 'not_found_short_url' => [ + 'enable_redirection' => + $this->urlShortener[UrlShortenerConfigCustomizer::ENABLE_NOT_FOUND_REDIRECTION] ?? false, + 'redirect_to' => $this->urlShortener[UrlShortenerConfigCustomizer::NOT_FOUND_REDIRECT_TO] ?? null, + ], ], ];