diff --git a/config/autoload/routes.config.php b/config/autoload/routes.config.php index f7c6ce073..da3d17784 100644 --- a/config/autoload/routes.config.php +++ b/config/autoload/routes.config.php @@ -19,8 +19,6 @@ return (static function (): array { $dropDomainMiddleware = Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class; $overrideDomainMiddleware = Middleware\ShortUrl\OverrideDomainMiddleware::class; - - // TODO This should be based on config, not the env var $shortUrlRouteSuffix = EnvVars::SHORT_URL_TRAILING_SLASH->loadFromEnv() ? '[/]' : ''; return [ diff --git a/config/autoload/url-shortener.global.php b/config/autoload/url-shortener.global.php deleted file mode 100644 index 98b07ea24..000000000 --- a/config/autoload/url-shortener.global.php +++ /dev/null @@ -1,34 +0,0 @@ -loadFromEnv(), - MIN_SHORT_CODES_LENGTH, - ); - $modeFromEnv = EnvVars::SHORT_URL_MODE->loadFromEnv(); - $mode = ShortUrlMode::tryFrom($modeFromEnv) ?? ShortUrlMode::STRICT; - - return [ - - 'url_shortener' => [ - 'domain' => [ // TODO Refactor this structure to url_shortener.schema and url_shortener.default_domain - 'schema' => ((bool) EnvVars::IS_HTTPS_ENABLED->loadFromEnv()) ? 'https' : 'http', - 'hostname' => EnvVars::DEFAULT_DOMAIN->loadFromEnv(), - ], - 'default_short_codes_length' => $shortCodesLength, - 'auto_resolve_titles' => (bool) EnvVars::AUTO_RESOLVE_TITLES->loadFromEnv(), - 'append_extra_path' => (bool) EnvVars::REDIRECT_APPEND_EXTRA_PATH->loadFromEnv(), - 'multi_segment_slugs_enabled' => (bool) EnvVars::MULTI_SEGMENT_SLUGS_ENABLED->loadFromEnv(), - 'trailing_slash_enabled' => (bool) EnvVars::SHORT_URL_TRAILING_SLASH->loadFromEnv(), - 'mode' => $mode, - ], - - ]; -})(); diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index fd26a5bc3..2f098998c 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -88,7 +88,7 @@ TrackingOptions::class, ], Util\ProcessRunner::class => [SymfonyCli\Helper\ProcessHelper::class], - ApiKey\RoleResolver::class => [DomainService::class, 'config.url_shortener.domain.hostname'], + ApiKey\RoleResolver::class => [DomainService::class, UrlShortenerOptions::class], Command\ShortUrl\CreateShortUrlCommand::class => [ ShortUrl\UrlShortener::class, diff --git a/module/CLI/src/ApiKey/RoleResolver.php b/module/CLI/src/ApiKey/RoleResolver.php index 76787451a..bfcf11a3d 100644 --- a/module/CLI/src/ApiKey/RoleResolver.php +++ b/module/CLI/src/ApiKey/RoleResolver.php @@ -5,6 +5,7 @@ namespace Shlinkio\Shlink\CLI\ApiKey; use Shlinkio\Shlink\CLI\Exception\InvalidRoleConfigException; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Domain\DomainServiceInterface; use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; use Shlinkio\Shlink\Rest\ApiKey\Role; @@ -12,11 +13,11 @@ use function is_string; -class RoleResolver implements RoleResolverInterface +readonly class RoleResolver implements RoleResolverInterface { public function __construct( - private readonly DomainServiceInterface $domainService, - private readonly string $defaultDomain, + private DomainServiceInterface $domainService, + private UrlShortenerOptions $urlShortenerOptions, ) { } @@ -39,7 +40,7 @@ public function determineRoles(InputInterface $input): iterable private function resolveRoleForAuthority(string $domainAuthority): RoleDefinition { - if ($domainAuthority === $this->defaultDomain) { + if ($domainAuthority === $this->urlShortenerOptions->defaultDomain()) { throw InvalidRoleConfigException::forDomainOnlyWithDefaultDomain(); } diff --git a/module/CLI/test/ApiKey/RoleResolverTest.php b/module/CLI/test/ApiKey/RoleResolverTest.php index cbd4f0fa1..d1f696f56 100644 --- a/module/CLI/test/ApiKey/RoleResolverTest.php +++ b/module/CLI/test/ApiKey/RoleResolverTest.php @@ -10,6 +10,7 @@ use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\CLI\ApiKey\RoleResolver; use Shlinkio\Shlink\CLI\Exception\InvalidRoleConfigException; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Domain\DomainServiceInterface; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; @@ -24,7 +25,10 @@ class RoleResolverTest extends TestCase protected function setUp(): void { $this->domainService = $this->createMock(DomainServiceInterface::class); - $this->resolver = new RoleResolver($this->domainService, 'default.com'); + $this->resolver = new RoleResolver( + $this->domainService, + new UrlShortenerOptions(domain: ['hostname' => 'default.com']), + ); } #[Test, DataProvider('provideRoles')] diff --git a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php index 6016da1c1..176800abe 100644 --- a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php @@ -37,7 +37,7 @@ protected function setUp(): void { $this->shortUrlService = $this->createMock(ShortUrlListServiceInterface::class); $command = new ListShortUrlsCommand($this->shortUrlService, new ShortUrlDataTransformer( - new ShortUrlStringifier([]), + new ShortUrlStringifier(), )); $this->commandTester = CliTestUtils::testerForCommand($command); } diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index f50469c0e..420d8a395 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -167,7 +167,7 @@ ShortUrl\ShortUrlResolver::class, ], ShortUrl\Helper\ShortCodeUniquenessHelper::class => ['em', Config\Options\UrlShortenerOptions::class], - Domain\DomainService::class => ['em', 'config.url_shortener.domain.hostname'], + Domain\DomainService::class => ['em', Config\Options\UrlShortenerOptions::class], Util\DoctrineBatchHelper::class => ['em'], Util\RedirectResponseHelper::class => [Config\Options\RedirectOptions::class], @@ -197,7 +197,10 @@ Config\Options\UrlShortenerOptions::class, Lock\LockFactory::class, ], - ShortUrl\Helper\ShortUrlStringifier::class => ['config.url_shortener.domain', 'config.router.base_path'], + ShortUrl\Helper\ShortUrlStringifier::class => [ + Config\Options\UrlShortenerOptions::class, + 'config.router.base_path', + ], ShortUrl\Helper\ShortUrlTitleResolutionHelper::class => [ 'httpClient', Config\Options\UrlShortenerOptions::class, diff --git a/module/Core/src/Config/PostProcessor/MultiSegmentSlugProcessor.php b/module/Core/src/Config/PostProcessor/MultiSegmentSlugProcessor.php index 585f78b6b..c4078890b 100644 --- a/module/Core/src/Config/PostProcessor/MultiSegmentSlugProcessor.php +++ b/module/Core/src/Config/PostProcessor/MultiSegmentSlugProcessor.php @@ -4,6 +4,8 @@ namespace Shlinkio\Shlink\Core\Config\PostProcessor; +use Shlinkio\Shlink\Core\Config\EnvVars; + use function array_map; use function str_replace; @@ -14,7 +16,7 @@ class MultiSegmentSlugProcessor public function __invoke(array $config): array { - $multiSegmentEnabled = $config['url_shortener']['multi_segment_slugs_enabled'] ?? false; + $multiSegmentEnabled = (bool) EnvVars::MULTI_SEGMENT_SLUGS_ENABLED->loadFromEnv(); if (! $multiSegmentEnabled) { return $config; } diff --git a/module/Core/src/Domain/DomainService.php b/module/Core/src/Domain/DomainService.php index 93adbf5f1..382ca08ea 100644 --- a/module/Core/src/Domain/DomainService.php +++ b/module/Core/src/Domain/DomainService.php @@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManagerInterface; use Shlinkio\Shlink\Core\Config\EmptyNotFoundRedirectConfig; use Shlinkio\Shlink\Core\Config\NotFoundRedirects; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\Domain\Model\DomainItem; use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface; @@ -16,9 +17,9 @@ use function array_map; -class DomainService implements DomainServiceInterface +readonly class DomainService implements DomainServiceInterface { - public function __construct(private readonly EntityManagerInterface $em, private readonly string $defaultDomain) + public function __construct(private EntityManagerInterface $em, private UrlShortenerOptions $urlShortenerOptions) { } @@ -35,7 +36,10 @@ public function listDomains(?ApiKey $apiKey = null): array } return [ - DomainItem::forDefaultDomain($this->defaultDomain, $default ?? new EmptyNotFoundRedirectConfig()), + DomainItem::forDefaultDomain( + $this->urlShortenerOptions->defaultDomain(), + $default ?? new EmptyNotFoundRedirectConfig(), + ), ...$mappedDomains, ]; } @@ -52,7 +56,7 @@ private function defaultDomainAndRest(?ApiKey $apiKey): array $restOfDomains = []; foreach ($allDomains as $domain) { - if ($domain->authority === $this->defaultDomain) { + if ($domain->authority === $this->urlShortenerOptions->defaultDomain()) { $defaultDomain = $domain; } else { $restOfDomains[] = $domain; diff --git a/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php b/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php index 886a4d25d..540b444c0 100644 --- a/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php +++ b/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php @@ -5,22 +5,23 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Helper; use Laminas\Diactoros\Uri; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use function sprintf; -class ShortUrlStringifier implements ShortUrlStringifierInterface +readonly class ShortUrlStringifier implements ShortUrlStringifierInterface { - /** - * @param array{schema?: string, hostname?: string} $domainConfig - */ - public function __construct(private readonly array $domainConfig, private readonly string $basePath = '') - { + public function __construct( + private UrlShortenerOptions $urlShortenerOptions = new UrlShortenerOptions(), + private string $basePath = '', + ) { } public function stringify(ShortUrl $shortUrl): string { - $uriWithoutShortCode = (new Uri())->withScheme($this->domainConfig['schema'] ?? 'http') + $domainConfig = $this->urlShortenerOptions->domain; + $uriWithoutShortCode = (new Uri())->withScheme($domainConfig['schema'] ?? 'http') ->withHost($this->resolveDomain($shortUrl)) ->withPath($this->basePath) ->__toString(); @@ -31,6 +32,7 @@ public function stringify(ShortUrl $shortUrl): string private function resolveDomain(ShortUrl $shortUrl): string { - return $shortUrl->getDomain()?->authority ?? $this->domainConfig['hostname'] ?? ''; + $domainConfig = $this->urlShortenerOptions->domain; + return $shortUrl->getDomain()?->authority ?? $domainConfig['hostname'] ?? ''; } } diff --git a/module/Core/test/Action/QrCodeActionTest.php b/module/Core/test/Action/QrCodeActionTest.php index 9afae7932..d040b7d7c 100644 --- a/module/Core/test/Action/QrCodeActionTest.php +++ b/module/Core/test/Action/QrCodeActionTest.php @@ -17,6 +17,7 @@ use Shlinkio\Shlink\Common\Response\QrCodeResponse; use Shlinkio\Shlink\Core\Action\QrCodeAction; use Shlinkio\Shlink\Core\Config\Options\QrCodeOptions; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier; @@ -324,7 +325,7 @@ public function action(?QrCodeOptions $options = null): QrCodeAction { return new QrCodeAction( $this->urlResolver, - new ShortUrlStringifier(['domain' => 's.test']), + new ShortUrlStringifier(), new NullLogger(), $options ?? new QrCodeOptions(enabledForDisabledShortUrls: false), ); diff --git a/module/Core/test/Domain/DomainServiceTest.php b/module/Core/test/Domain/DomainServiceTest.php index c67597ec8..82a256547 100644 --- a/module/Core/test/Domain/DomainServiceTest.php +++ b/module/Core/test/Domain/DomainServiceTest.php @@ -11,6 +11,7 @@ use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Core\Config\EmptyNotFoundRedirectConfig; use Shlinkio\Shlink\Core\Config\NotFoundRedirects; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Domain\DomainService; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\Domain\Model\DomainItem; @@ -28,7 +29,10 @@ class DomainServiceTest extends TestCase protected function setUp(): void { $this->em = $this->createMock(EntityManagerInterface::class); - $this->domainService = new DomainService($this->em, 'default.com'); + $this->domainService = new DomainService( + $this->em, + new UrlShortenerOptions(domain: ['hostname' => 'default.com']), + ); } #[Test, DataProvider('provideExcludedDomains')] diff --git a/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php b/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php index bd3c82c83..1ea76bf67 100644 --- a/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php +++ b/module/Core/test/EventDispatcher/PublishingUpdatesGeneratorTest.php @@ -31,7 +31,7 @@ protected function setUp(): void Chronos::setTestNow($this->now); $this->generator = new PublishingUpdatesGenerator( - new ShortUrlDataTransformer(new ShortUrlStringifier([])), + new ShortUrlDataTransformer(new ShortUrlStringifier()), ); } diff --git a/module/Core/test/Matomo/MatomoVisitSenderTest.php b/module/Core/test/Matomo/MatomoVisitSenderTest.php index 816c8eeaf..1794973b0 100644 --- a/module/Core/test/Matomo/MatomoVisitSenderTest.php +++ b/module/Core/test/Matomo/MatomoVisitSenderTest.php @@ -11,6 +11,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Common\Util\DateRange; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Matomo\MatomoTrackerBuilderInterface; use Shlinkio\Shlink\Core\Matomo\MatomoVisitSender; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; @@ -36,7 +37,7 @@ protected function setUp(): void $this->visitSender = new MatomoVisitSender( $this->trackerBuilder, - new ShortUrlStringifier(['hostname' => 's2.test']), + new ShortUrlStringifier(new UrlShortenerOptions(domain: ['hostname' => 's2.test'])), $this->visitIterationRepository, ); } diff --git a/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php b/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php index e74d61829..423795318 100644 --- a/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php +++ b/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation; @@ -15,12 +16,12 @@ class ShortUrlStringifierTest extends TestCase { #[Test, DataProvider('provideConfigAndShortUrls')] public function generatesExpectedOutputBasedOnConfigAndShortUrl( - array $config, + array $domainConfig, string $basePath, ShortUrl $shortUrl, string $expected, ): void { - $stringifier = new ShortUrlStringifier($config, $basePath); + $stringifier = new ShortUrlStringifier(new UrlShortenerOptions($domainConfig), $basePath); self::assertEquals($expected, $stringifier->stringify($shortUrl)); } diff --git a/module/Core/test/ShortUrl/Transformer/ShortUrlDataTransformerTest.php b/module/Core/test/ShortUrl/Transformer/ShortUrlDataTransformerTest.php index 349392dbd..9ff014752 100644 --- a/module/Core/test/ShortUrl/Transformer/ShortUrlDataTransformerTest.php +++ b/module/Core/test/ShortUrl/Transformer/ShortUrlDataTransformerTest.php @@ -21,7 +21,7 @@ class ShortUrlDataTransformerTest extends TestCase protected function setUp(): void { - $this->transformer = new ShortUrlDataTransformer(new ShortUrlStringifier([])); + $this->transformer = new ShortUrlDataTransformer(new ShortUrlStringifier()); } #[Test, DataProvider('provideShortUrls')] diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index 3d7a66a48..b69cf36d6 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -86,7 +86,7 @@ Action\Visit\TagVisitsAction::class => [Visit\VisitsStatsHelper::class], Action\Visit\DomainVisitsAction::class => [ Visit\VisitsStatsHelper::class, - 'config.url_shortener.domain.hostname', + Config\Options\UrlShortenerOptions::class, ], Action\Visit\GlobalVisitsAction::class => [Visit\VisitsStatsHelper::class], Action\Visit\OrphanVisitsAction::class => [Visit\VisitsStatsHelper::class], @@ -113,10 +113,10 @@ ], Middleware\CrossDomainMiddleware::class => ['config.cors'], - Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class => ['config.url_shortener.domain.hostname'], - Middleware\ShortUrl\DefaultShortCodesLengthMiddleware::class => [ - 'config.url_shortener.default_short_codes_length', + Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class => [ + Config\Options\UrlShortenerOptions::class, ], + Middleware\ShortUrl\DefaultShortCodesLengthMiddleware::class => [Config\Options\UrlShortenerOptions::class], Middleware\ShortUrl\OverrideDomainMiddleware::class => [DomainService::class], Middleware\Mercure\NotConfiguredMercureErrorHandler::class => [ ProblemDetailsResponseFactory::class, diff --git a/module/Rest/src/Action/Visit/DomainVisitsAction.php b/module/Rest/src/Action/Visit/DomainVisitsAction.php index 91f071b5f..31b8add4e 100644 --- a/module/Rest/src/Action/Visit/DomainVisitsAction.php +++ b/module/Rest/src/Action/Visit/DomainVisitsAction.php @@ -8,6 +8,7 @@ use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Shlinkio\Shlink\Common\Paginator\Util\PagerfantaUtils; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Visit\Model\VisitsParams; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\Rest\Action\AbstractRestAction; @@ -20,7 +21,7 @@ class DomainVisitsAction extends AbstractRestAction public function __construct( private readonly VisitsStatsHelperInterface $visitsHelper, - private readonly string $defaultDomain, + private readonly UrlShortenerOptions $urlShortenerOptions, ) { } @@ -37,7 +38,7 @@ public function handle(Request $request): Response private function resolveDomainParam(Request $request): string { $domainParam = $request->getAttribute('domain', ''); - if ($domainParam === $this->defaultDomain) { + if ($domainParam === $this->urlShortenerOptions->defaultDomain()) { return 'DEFAULT'; } diff --git a/module/Rest/src/Middleware/ShortUrl/DefaultShortCodesLengthMiddleware.php b/module/Rest/src/Middleware/ShortUrl/DefaultShortCodesLengthMiddleware.php index 4b7779c21..0d7d947e4 100644 --- a/module/Rest/src/Middleware/ShortUrl/DefaultShortCodesLengthMiddleware.php +++ b/module/Rest/src/Middleware/ShortUrl/DefaultShortCodesLengthMiddleware.php @@ -8,11 +8,12 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter; -class DefaultShortCodesLengthMiddleware implements MiddlewareInterface +readonly class DefaultShortCodesLengthMiddleware implements MiddlewareInterface { - public function __construct(private int $defaultShortCodesLength) + public function __construct(private UrlShortenerOptions $urlShortenerOptions) { } @@ -21,7 +22,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface /** @var array $body */ $body = $request->getParsedBody(); if (! isset($body[ShortUrlInputFilter::SHORT_CODE_LENGTH])) { - $body[ShortUrlInputFilter::SHORT_CODE_LENGTH] = $this->defaultShortCodesLength; + $body[ShortUrlInputFilter::SHORT_CODE_LENGTH] = $this->urlShortenerOptions->defaultShortCodesLength; } return $handler->handle($request->withParsedBody($body)); diff --git a/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddleware.php b/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddleware.php index 73a6ac696..f50341d2e 100644 --- a/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddleware.php +++ b/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddleware.php @@ -8,10 +8,11 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; -class DropDefaultDomainFromRequestMiddleware implements MiddlewareInterface +readonly class DropDefaultDomainFromRequestMiddleware implements MiddlewareInterface { - public function __construct(private readonly string $defaultDomain) + public function __construct(private UrlShortenerOptions $urlShortenerOptions) { } @@ -27,7 +28,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface private function sanitizeDomainFromPayload(array $payload): array { - if (isset($payload['domain']) && $payload['domain'] === $this->defaultDomain) { + if (isset($payload['domain']) && $payload['domain'] === $this->urlShortenerOptions->defaultDomain()) { unset($payload['domain']); } diff --git a/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php b/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php index dd7a0e14f..669037d1b 100644 --- a/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php +++ b/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php @@ -25,7 +25,7 @@ protected function setUp(): void { $this->shortUrlService = $this->createMock(ShortUrlServiceInterface::class); $this->action = new EditShortUrlAction($this->shortUrlService, new ShortUrlDataTransformer( - new ShortUrlStringifier([]), + new ShortUrlStringifier(), )); } diff --git a/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php b/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php index d372d6929..071fb8441 100644 --- a/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php +++ b/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Common\Paginator\Paginator; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlsParams; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlListServiceInterface; @@ -30,10 +31,10 @@ protected function setUp(): void $this->service = $this->createMock(ShortUrlListServiceInterface::class); $this->action = new ListShortUrlsAction($this->service, new ShortUrlDataTransformer( - new ShortUrlStringifier([ + new ShortUrlStringifier(new UrlShortenerOptions(domain: [ 'hostname' => 's.test', 'schema' => 'https', - ]), + ])), )); } diff --git a/module/Rest/test/Action/ShortUrl/ResolveShortUrlActionTest.php b/module/Rest/test/Action/ShortUrl/ResolveShortUrlActionTest.php index 0576fd525..13b9e35d2 100644 --- a/module/Rest/test/Action/ShortUrl/ResolveShortUrlActionTest.php +++ b/module/Rest/test/Action/ShortUrl/ResolveShortUrlActionTest.php @@ -25,7 +25,7 @@ protected function setUp(): void { $this->urlResolver = $this->createMock(ShortUrlResolverInterface::class); $this->action = new ResolveShortUrlAction($this->urlResolver, new ShortUrlDataTransformer( - new ShortUrlStringifier([]), + new ShortUrlStringifier(), )); } diff --git a/module/Rest/test/Action/Visit/DomainVisitsActionTest.php b/module/Rest/test/Action/Visit/DomainVisitsActionTest.php index a6d64bd2b..3fb4c498d 100644 --- a/module/Rest/test/Action/Visit/DomainVisitsActionTest.php +++ b/module/Rest/test/Action/Visit/DomainVisitsActionTest.php @@ -11,6 +11,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Common\Paginator\Paginator; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Visit\Model\VisitsParams; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\Rest\Action\Visit\DomainVisitsAction; @@ -24,7 +25,10 @@ class DomainVisitsActionTest extends TestCase protected function setUp(): void { $this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class); - $this->action = new DomainVisitsAction($this->visitsHelper, 'the_default.com'); + $this->action = new DomainVisitsAction( + $this->visitsHelper, + new UrlShortenerOptions(domain: ['hostname' => 'the_default.com']), + ); } #[Test, DataProvider('provideDomainAuthorities')] diff --git a/module/Rest/test/Middleware/ShortUrl/DefaultShortCodesLengthMiddlewareTest.php b/module/Rest/test/Middleware/ShortUrl/DefaultShortCodesLengthMiddlewareTest.php index e6b0c6ec2..db579451c 100644 --- a/module/Rest/test/Middleware/ShortUrl/DefaultShortCodesLengthMiddlewareTest.php +++ b/module/Rest/test/Middleware/ShortUrl/DefaultShortCodesLengthMiddlewareTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter; use Shlinkio\Shlink\Rest\Middleware\ShortUrl\DefaultShortCodesLengthMiddleware; @@ -24,7 +25,7 @@ class DefaultShortCodesLengthMiddlewareTest extends TestCase protected function setUp(): void { $this->handler = $this->createMock(RequestHandlerInterface::class); - $this->middleware = new DefaultShortCodesLengthMiddleware(8); + $this->middleware = new DefaultShortCodesLengthMiddleware(new UrlShortenerOptions(defaultShortCodesLength: 8)); } #[Test, DataProvider('provideBodies')] diff --git a/module/Rest/test/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddlewareTest.php b/module/Rest/test/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddlewareTest.php index e9fceea50..530e28ca0 100644 --- a/module/Rest/test/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddlewareTest.php +++ b/module/Rest/test/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddlewareTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Rest\Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware; class DropDefaultDomainFromRequestMiddlewareTest extends TestCase @@ -23,7 +24,9 @@ class DropDefaultDomainFromRequestMiddlewareTest extends TestCase protected function setUp(): void { $this->next = $this->createMock(RequestHandlerInterface::class); - $this->middleware = new DropDefaultDomainFromRequestMiddleware('s.test'); + $this->middleware = new DropDefaultDomainFromRequestMiddleware( + new UrlShortenerOptions(domain: ['hostname' => 's.test']), + ); } #[Test, DataProvider('provideQueryParams')]