Remove dependencies on url_shortener raw config

This commit is contained in:
Alejandro Celaya
2024-10-20 12:52:00 +02:00
parent b991b1699e
commit c8e5196aab
26 changed files with 83 additions and 84 deletions

View File

@@ -167,7 +167,7 @@ return [
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 @@ return [
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,

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
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;
}

View File

@@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\Core\Domain;
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 Shlinkio\Shlink\Rest\Entity\ApiKey;
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 @@ class DomainService implements DomainServiceInterface
}
return [
DomainItem::forDefaultDomain($this->defaultDomain, $default ?? new EmptyNotFoundRedirectConfig()),
DomainItem::forDefaultDomain(
$this->urlShortenerOptions->defaultDomain(),
$default ?? new EmptyNotFoundRedirectConfig(),
),
...$mappedDomains,
];
}
@@ -52,7 +56,7 @@ class DomainService implements DomainServiceInterface
$restOfDomains = [];
foreach ($allDomains as $domain) {
if ($domain->authority === $this->defaultDomain) {
if ($domain->authority === $this->urlShortenerOptions->defaultDomain()) {
$defaultDomain = $domain;
} else {
$restOfDomains[] = $domain;

View File

@@ -5,22 +5,23 @@ declare(strict_types=1);
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 @@ class ShortUrlStringifier implements ShortUrlStringifierInterface
private function resolveDomain(ShortUrl $shortUrl): string
{
return $shortUrl->getDomain()?->authority ?? $this->domainConfig['hostname'] ?? '';
$domainConfig = $this->urlShortenerOptions->domain;
return $shortUrl->getDomain()?->authority ?? $domainConfig['hostname'] ?? '';
}
}

View File

@@ -17,6 +17,7 @@ use Psr\Log\NullLogger;
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 @@ class QrCodeActionTest extends TestCase
{
return new QrCodeAction(
$this->urlResolver,
new ShortUrlStringifier(['domain' => 's.test']),
new ShortUrlStringifier(),
new NullLogger(),
$options ?? new QrCodeOptions(enabledForDisabledShortUrls: false),
);

View File

@@ -11,6 +11,7 @@ use PHPUnit\Framework\MockObject\MockObject;
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')]

View File

@@ -31,7 +31,7 @@ class PublishingUpdatesGeneratorTest extends TestCase
Chronos::setTestNow($this->now);
$this->generator = new PublishingUpdatesGenerator(
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
new ShortUrlDataTransformer(new ShortUrlStringifier()),
);
}

View File

@@ -11,6 +11,7 @@ use PHPUnit\Framework\Attributes\Test;
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 @@ class MatomoVisitSenderTest extends TestCase
$this->visitSender = new MatomoVisitSender(
$this->trackerBuilder,
new ShortUrlStringifier(['hostname' => 's2.test']),
new ShortUrlStringifier(new UrlShortenerOptions(domain: ['hostname' => 's2.test'])),
$this->visitIterationRepository,
);
}

View File

@@ -7,6 +7,7 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl\Helper;
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));
}

View File

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