Moved some config to the proper namespace, now that config is no longer part of the public contract

This commit is contained in:
Alejandro Celaya 2022-01-16 15:34:07 +01:00
parent fb43885d85
commit bfb54189b8
9 changed files with 63 additions and 52 deletions

View file

@ -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),
],

View file

@ -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),
],

View file

@ -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'],

View file

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
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 RedirectOptions extends AbstractOptions
{
private int $redirectStatusCode = DEFAULT_REDIRECT_STATUS_CODE;
private int $redirectCacheLifetime = DEFAULT_REDIRECT_CACHE_LIFETIME;
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;
}
}

View file

@ -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;

View file

@ -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

View file

@ -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)
{
}

View file

@ -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']),

View file

@ -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);
}