Skip to content

Commit

Permalink
Remove dependencies on url_shortener raw config
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Oct 20, 2024
1 parent 0b51bd1 commit c2e1cf1
Show file tree
Hide file tree
Showing 26 changed files with 83 additions and 84 deletions.
2 changes: 0 additions & 2 deletions config/autoload/routes.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
34 changes: 0 additions & 34 deletions config/autoload/url-shortener.global.php

This file was deleted.

2 changes: 1 addition & 1 deletion module/CLI/config/dependencies.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions module/CLI/src/ApiKey/RoleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
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;
use Symfony\Component\Console\Input\InputInterface;

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

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

Expand Down
6 changes: 5 additions & 1 deletion module/CLI/test/ApiKey/RoleResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 5 additions & 2 deletions module/Core/config/dependencies.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Shlinkio\Shlink\Core\Config\PostProcessor;

use Shlinkio\Shlink\Core\Config\EnvVars;

use function array_map;
use function str_replace;

Expand All @@ -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;
}
Expand Down
12 changes: 8 additions & 4 deletions module/Core/src/Domain/DomainService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
}

Expand All @@ -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,
];
}
Expand All @@ -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;
Expand Down
18 changes: 10 additions & 8 deletions module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'] ?? '';
}
}
3 changes: 2 additions & 1 deletion module/Core/test/Action/QrCodeActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
);
Expand Down
6 changes: 5 additions & 1 deletion module/Core/test/Domain/DomainServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp(): void
Chronos::setTestNow($this->now);

$this->generator = new PublishingUpdatesGenerator(
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
new ShortUrlDataTransformer(new ShortUrlStringifier()),
);
}

Expand Down
3 changes: 2 additions & 1 deletion module/Core/test/Matomo/MatomoVisitSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
);
}
Expand Down
5 changes: 3 additions & 2 deletions module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down
8 changes: 4 additions & 4 deletions module/Rest/config/dependencies.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions module/Rest/src/Action/Visit/DomainVisitsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +21,7 @@ class DomainVisitsAction extends AbstractRestAction

public function __construct(
private readonly VisitsStatsHelperInterface $visitsHelper,
private readonly string $defaultDomain,
private readonly UrlShortenerOptions $urlShortenerOptions,
) {
}

Expand All @@ -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';
}

Expand Down
Loading

0 comments on commit c2e1cf1

Please sign in to comment.