diff --git a/config/autoload/redirects.global.php b/config/autoload/redirects.global.php index e38b9c25..20cde8eb 100644 --- a/config/autoload/redirects.global.php +++ b/config/autoload/redirects.global.php @@ -15,8 +15,7 @@ return [ 'base_url' => env('DEFAULT_BASE_URL_REDIRECT'), ], - 'url_shortener' => [ - // TODO Move these options to their own config namespace. Maybe "redirects". + 'redirects' => [ 'redirect_status_code' => (int) env('REDIRECT_STATUS_CODE', DEFAULT_REDIRECT_STATUS_CODE), 'redirect_cache_lifetime' => (int) env('REDIRECT_CACHE_LIFETIME', DEFAULT_REDIRECT_CACHE_LIFETIME), ], diff --git a/config/autoload/webhooks.global.php b/config/autoload/webhooks.global.php index fb946028..6bbbfbda 100644 --- a/config/autoload/webhooks.global.php +++ b/config/autoload/webhooks.global.php @@ -9,9 +9,8 @@ return (static function (): array { return [ - 'url_shortener' => [ - // TODO Move these options to their own config namespace - 'visits_webhooks' => $webhooks === null ? [] : explode(',', $webhooks), + 'visits_webhooks' => [ + 'webhooks' => $webhooks === null ? [] : explode(',', $webhooks), 'notify_orphan_visits_to_webhooks' => (bool) env('NOTIFY_ORPHAN_VISITS_TO_WEBHOOKS', false), ], diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index fdfecef9..90931c11 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -23,6 +23,7 @@ return [ Options\AppOptions::class => ConfigAbstractFactory::class, Options\DeleteShortUrlsOptions::class => ConfigAbstractFactory::class, Options\NotFoundRedirectOptions::class => ConfigAbstractFactory::class, + Options\RedirectOptions::class => ConfigAbstractFactory::class, Options\UrlShortenerOptions::class => ConfigAbstractFactory::class, Options\TrackingOptions::class => ConfigAbstractFactory::class, Options\QrCodeOptions::class => ConfigAbstractFactory::class, @@ -86,10 +87,11 @@ return [ Options\AppOptions::class => ['config.app_options'], Options\DeleteShortUrlsOptions::class => ['config.delete_short_urls'], Options\NotFoundRedirectOptions::class => ['config.not_found_redirects'], + Options\RedirectOptions::class => ['config.redirects'], Options\UrlShortenerOptions::class => ['config.url_shortener'], Options\TrackingOptions::class => ['config.tracking'], Options\QrCodeOptions::class => ['config.qr_codes'], - Options\WebhookOptions::class => ['config.url_shortener'], // TODO This config is currently under url_shortener + Options\WebhookOptions::class => ['config.visits_webhooks'], Service\UrlShortener::class => [ ShortUrl\Helper\ShortUrlTitleResolutionHelper::class, @@ -123,7 +125,7 @@ return [ Util\UrlValidator::class => ['httpClient', Options\UrlShortenerOptions::class], Util\DoctrineBatchHelper::class => ['em'], - Util\RedirectResponseHelper::class => [Options\UrlShortenerOptions::class], + Util\RedirectResponseHelper::class => [Options\RedirectOptions::class], Config\NotFoundRedirectResolver::class => [Util\RedirectResponseHelper::class, 'Logger_Shlink'], diff --git a/module/Core/src/Options/RedirectOptions.php b/module/Core/src/Options/RedirectOptions.php new file mode 100644 index 00000000..5479c59b --- /dev/null +++ b/module/Core/src/Options/RedirectOptions.php @@ -0,0 +1,45 @@ +redirectStatusCode; + } + + protected function setRedirectStatusCode(int $redirectStatusCode): void + { + $this->redirectStatusCode = $this->normalizeRedirectStatusCode($redirectStatusCode); + } + + private function normalizeRedirectStatusCode(int $statusCode): int + { + return contains([301, 302], $statusCode) ? $statusCode : DEFAULT_REDIRECT_STATUS_CODE; + } + + public function redirectCacheLifetime(): int + { + return $this->redirectCacheLifetime; + } + + protected function setRedirectCacheLifetime(int $redirectCacheLifetime): void + { + $this->redirectCacheLifetime = $redirectCacheLifetime > 0 + ? $redirectCacheLifetime + : DEFAULT_REDIRECT_CACHE_LIFETIME; + } +} diff --git a/module/Core/src/Options/UrlShortenerOptions.php b/module/Core/src/Options/UrlShortenerOptions.php index ecbbb590..775254ce 100644 --- a/module/Core/src/Options/UrlShortenerOptions.php +++ b/module/Core/src/Options/UrlShortenerOptions.php @@ -6,18 +6,11 @@ namespace Shlinkio\Shlink\Core\Options; use Laminas\Stdlib\AbstractOptions; -use function Functional\contains; - -use const Shlinkio\Shlink\DEFAULT_REDIRECT_CACHE_LIFETIME; -use const Shlinkio\Shlink\DEFAULT_REDIRECT_STATUS_CODE; - class UrlShortenerOptions extends AbstractOptions { protected $__strictMode__ = false; // phpcs:ignore private bool $validateUrl = true; - private int $redirectStatusCode = DEFAULT_REDIRECT_STATUS_CODE; - private int $redirectCacheLifetime = DEFAULT_REDIRECT_CACHE_LIFETIME; private bool $autoResolveTitles = false; private bool $appendExtraPath = false; @@ -31,33 +24,6 @@ class UrlShortenerOptions extends AbstractOptions $this->validateUrl = $validateUrl; } - public function redirectStatusCode(): int - { - return $this->redirectStatusCode; - } - - protected function setRedirectStatusCode(int $redirectStatusCode): void - { - $this->redirectStatusCode = $this->normalizeRedirectStatusCode($redirectStatusCode); - } - - private function normalizeRedirectStatusCode(int $statusCode): int - { - return contains([301, 302], $statusCode) ? $statusCode : DEFAULT_REDIRECT_STATUS_CODE; - } - - public function redirectCacheLifetime(): int - { - return $this->redirectCacheLifetime; - } - - protected function setRedirectCacheLifetime(int $redirectCacheLifetime): void - { - $this->redirectCacheLifetime = $redirectCacheLifetime > 0 - ? $redirectCacheLifetime - : DEFAULT_REDIRECT_CACHE_LIFETIME; - } - public function autoResolveTitles(): bool { return $this->autoResolveTitles; diff --git a/module/Core/src/Options/WebhookOptions.php b/module/Core/src/Options/WebhookOptions.php index c86789b2..6eb07692 100644 --- a/module/Core/src/Options/WebhookOptions.php +++ b/module/Core/src/Options/WebhookOptions.php @@ -10,22 +10,22 @@ class WebhookOptions extends AbstractOptions { protected $__strictMode__ = false; // phpcs:ignore - private array $visitsWebhooks = []; + private array $webhooks = []; private bool $notifyOrphanVisitsToWebhooks = false; public function webhooks(): array { - return $this->visitsWebhooks; + return $this->webhooks; } public function hasWebhooks(): bool { - return ! empty($this->visitsWebhooks); + return ! empty($this->webhooks); } - protected function setVisitsWebhooks(array $visitsWebhooks): void + protected function setWebhooks(array $webhooks): void { - $this->visitsWebhooks = $visitsWebhooks; + $this->webhooks = $webhooks; } public function notifyOrphanVisits(): bool diff --git a/module/Core/src/Util/RedirectResponseHelper.php b/module/Core/src/Util/RedirectResponseHelper.php index 5f9edf99..312c2a95 100644 --- a/module/Core/src/Util/RedirectResponseHelper.php +++ b/module/Core/src/Util/RedirectResponseHelper.php @@ -7,13 +7,13 @@ namespace Shlinkio\Shlink\Core\Util; use Fig\Http\Message\StatusCodeInterface; use Laminas\Diactoros\Response\RedirectResponse; use Psr\Http\Message\ResponseInterface; -use Shlinkio\Shlink\Core\Options\UrlShortenerOptions; +use Shlinkio\Shlink\Core\Options\RedirectOptions; use function sprintf; class RedirectResponseHelper implements RedirectResponseHelperInterface { - public function __construct(private UrlShortenerOptions $options) + public function __construct(private RedirectOptions $options) { } diff --git a/module/Core/test/EventDispatcher/NotifyVisitToWebHooksTest.php b/module/Core/test/EventDispatcher/NotifyVisitToWebHooksTest.php index 99609bb4..56324e40 100644 --- a/module/Core/test/EventDispatcher/NotifyVisitToWebHooksTest.php +++ b/module/Core/test/EventDispatcher/NotifyVisitToWebHooksTest.php @@ -162,7 +162,7 @@ class NotifyVisitToWebHooksTest extends TestCase $this->em->reveal(), $this->logger->reveal(), new WebhookOptions( - ['visits_webhooks' => $webhooks, 'notify_orphan_visits_to_webhooks' => $notifyOrphanVisits], + ['webhooks' => $webhooks, 'notify_orphan_visits_to_webhooks' => $notifyOrphanVisits], ), new ShortUrlDataTransformer(new ShortUrlStringifier([])), new AppOptions(['name' => 'Shlink', 'version' => '1.2.3']), diff --git a/module/Core/test/Util/RedirectResponseHelperTest.php b/module/Core/test/Util/RedirectResponseHelperTest.php index eb26768f..651d4bc7 100644 --- a/module/Core/test/Util/RedirectResponseHelperTest.php +++ b/module/Core/test/Util/RedirectResponseHelperTest.php @@ -6,17 +6,17 @@ namespace ShlinkioTest\Shlink\Core\Util; use Laminas\Diactoros\Response\RedirectResponse; use PHPUnit\Framework\TestCase; -use Shlinkio\Shlink\Core\Options\UrlShortenerOptions; +use Shlinkio\Shlink\Core\Options\RedirectOptions; use Shlinkio\Shlink\Core\Util\RedirectResponseHelper; class RedirectResponseHelperTest extends TestCase { private RedirectResponseHelper $helper; - private UrlShortenerOptions $shortenerOpts; + private RedirectOptions $shortenerOpts; protected function setUp(): void { - $this->shortenerOpts = new UrlShortenerOptions(); + $this->shortenerOpts = new RedirectOptions(); $this->helper = new RedirectResponseHelper($this->shortenerOpts); }