From 13ee71f3519ce68f1fcaadc67a0e4a1b3cd6b3af Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 12 Apr 2024 09:24:05 +0200 Subject: [PATCH] Move allowed HTTP methods definition to RedirectStatus enum --- .../PostProcessor/ShortUrlMethodsProcessor.php | 6 +----- module/Core/src/Util/RedirectStatus.php | 12 ++++++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/module/Core/src/Config/PostProcessor/ShortUrlMethodsProcessor.php b/module/Core/src/Config/PostProcessor/ShortUrlMethodsProcessor.php index 23ffe326..a73c584d 100644 --- a/module/Core/src/Config/PostProcessor/ShortUrlMethodsProcessor.php +++ b/module/Core/src/Config/PostProcessor/ShortUrlMethodsProcessor.php @@ -4,8 +4,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Config\PostProcessor; -use Fig\Http\Message\RequestMethodInterface; -use Mezzio\Router\Route; use Shlinkio\Shlink\Core\Action\RedirectAction; use Shlinkio\Shlink\Core\Util\RedirectStatus; @@ -40,9 +38,7 @@ class ShortUrlMethodsProcessor $redirectStatus = RedirectStatus::tryFrom( $config['redirects']['redirect_status_code'] ?? 0, ) ?? DEFAULT_REDIRECT_STATUS_CODE; - $redirectRoute['allowed_methods'] = $redirectStatus->isGetOnlyStatus() - ? [RequestMethodInterface::METHOD_GET] - : Route::HTTP_METHOD_ANY; + $redirectRoute['allowed_methods'] = $redirectStatus->allowedHttpMethods(); $config['routes'] = [...$rest, $redirectRoute]; return $config; diff --git a/module/Core/src/Util/RedirectStatus.php b/module/Core/src/Util/RedirectStatus.php index 347376d2..defea11b 100644 --- a/module/Core/src/Util/RedirectStatus.php +++ b/module/Core/src/Util/RedirectStatus.php @@ -2,6 +2,9 @@ namespace Shlinkio\Shlink\Core\Util; +use Fig\Http\Message\RequestMethodInterface; +use Mezzio\Router\Route; + use function Shlinkio\Shlink\Core\ArrayUtils\contains; enum RedirectStatus: int @@ -16,8 +19,13 @@ enum RedirectStatus: int return contains($this, [self::STATUS_301, self::STATUS_308]); } - public function isGetOnlyStatus(): bool + /** + * @return array|Route::HTTP_METHOD_ANY + */ + public function allowedHttpMethods(): array|null { - return contains($this, [self::STATUS_301, self::STATUS_302]); + return contains($this, [self::STATUS_301, self::STATUS_302]) + ? [RequestMethodInterface::METHOD_GET] + : Route::HTTP_METHOD_ANY; } }