Migrated NotFoundRedirectOptions to immutable object

This commit is contained in:
Alejandro Celaya 2022-09-17 13:32:40 +02:00
parent 39693ca1fe
commit 20f457a3e9
5 changed files with 25 additions and 42 deletions

View file

@ -43,10 +43,10 @@ class ListDomainsCommandTest extends TestCase
));
$listDomains = $this->domainService->listDomains()->willReturn([
DomainItem::forDefaultDomain('foo.com', new NotFoundRedirectOptions([
'base_url' => 'https://foo.com/default/base',
'invalid_short_url' => 'https://foo.com/default/invalid',
])),
DomainItem::forDefaultDomain('foo.com', new NotFoundRedirectOptions(
invalidShortUrl: 'https://foo.com/default/invalid',
baseUrl: 'https://foo.com/default/base',
)),
DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com')),
DomainItem::forNonDefaultDomain($bazDomain),
]);

View file

@ -23,7 +23,7 @@ return [
Options\AppOptions::class => [ValinorConfigFactory::class, 'config.app_options'],
Options\DeleteShortUrlsOptions::class => [ValinorConfigFactory::class, 'config.delete_short_urls'],
Options\NotFoundRedirectOptions::class => ConfigAbstractFactory::class,
Options\NotFoundRedirectOptions::class => [ValinorConfigFactory::class, 'config.not_found_redirects'],
Options\RedirectOptions::class => ConfigAbstractFactory::class,
Options\UrlShortenerOptions::class => ConfigAbstractFactory::class,
Options\TrackingOptions::class => [ValinorConfigFactory::class, 'config.tracking'],
@ -86,7 +86,6 @@ return [
Domain\DomainService::class,
],
Options\NotFoundRedirectOptions::class => ['config.not_found_redirects'],
Options\RedirectOptions::class => ['config.redirects'],
Options\UrlShortenerOptions::class => ['config.url_shortener'],
Options\QrCodeOptions::class => ['config.qr_codes'],

View file

@ -4,14 +4,16 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Options;
use Laminas\Stdlib\AbstractOptions;
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
class NotFoundRedirectOptions extends AbstractOptions implements NotFoundRedirectConfigInterface
final class NotFoundRedirectOptions implements NotFoundRedirectConfigInterface
{
private ?string $invalidShortUrl = null;
private ?string $regular404 = null;
private ?string $baseUrl = null;
public function __construct(
public readonly ?string $invalidShortUrl = null,
public readonly ?string $regular404 = null,
public readonly ?string $baseUrl = null,
) {
}
public function invalidShortUrlRedirect(): ?string
{
@ -23,12 +25,6 @@ class NotFoundRedirectOptions extends AbstractOptions implements NotFoundRedirec
return $this->invalidShortUrl !== null;
}
protected function setInvalidShortUrl(?string $invalidShortUrl): self
{
$this->invalidShortUrl = $invalidShortUrl;
return $this;
}
public function regular404Redirect(): ?string
{
return $this->regular404;
@ -39,12 +35,6 @@ class NotFoundRedirectOptions extends AbstractOptions implements NotFoundRedirec
return $this->regular404 !== null;
}
protected function setRegular404(?string $regular404): self
{
$this->regular404 = $regular404;
return $this;
}
public function baseUrlRedirect(): ?string
{
return $this->baseUrl;
@ -54,10 +44,4 @@ class NotFoundRedirectOptions extends AbstractOptions implements NotFoundRedirec
{
return $this->baseUrl !== null;
}
protected function setBaseUrl(?string $baseUrl): self
{
$this->baseUrl = $baseUrl;
return $this;
}
}

View file

@ -60,57 +60,57 @@ class NotFoundRedirectResolverTest extends TestCase
yield 'base URL with trailing slash' => [
$uri = new Uri('/'),
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(['baseUrl' => 'baseUrl']),
new NotFoundRedirectOptions(baseUrl: 'baseUrl'),
'baseUrl',
];
yield 'base URL with domain placeholder' => [
$uri = new Uri('https://doma.in'),
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(['baseUrl' => 'https://redirect-here.com/{DOMAIN}']),
new NotFoundRedirectOptions(baseUrl: 'https://redirect-here.com/{DOMAIN}'),
'https://redirect-here.com/doma.in',
];
yield 'base URL with domain placeholder in query' => [
$uri = new Uri('https://doma.in'),
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(['baseUrl' => 'https://redirect-here.com/?domain={DOMAIN}']),
new NotFoundRedirectOptions(baseUrl: 'https://redirect-here.com/?domain={DOMAIN}'),
'https://redirect-here.com/?domain=doma.in',
];
yield 'base URL without trailing slash' => [
$uri = new Uri(''),
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(['baseUrl' => 'baseUrl']),
new NotFoundRedirectOptions(baseUrl: 'baseUrl'),
'baseUrl',
];
yield 'regular 404' => [
$uri = new Uri('/foo/bar'),
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(['regular404' => 'regular404']),
new NotFoundRedirectOptions(regular404: 'regular404'),
'regular404',
];
yield 'regular 404 with path placeholder in query' => [
$uri = new Uri('/foo/bar'),
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions(['regular404' => 'https://redirect-here.com/?path={ORIGINAL_PATH}']),
new NotFoundRedirectOptions(regular404: 'https://redirect-here.com/?path={ORIGINAL_PATH}'),
'https://redirect-here.com/?path=%2Ffoo%2Fbar',
];
yield 'regular 404 with multiple placeholders' => [
$uri = new Uri('https://doma.in/foo/bar'),
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
new NotFoundRedirectOptions([
'regular404' => 'https://redirect-here.com/{ORIGINAL_PATH}/{DOMAIN}/?d={DOMAIN}&p={ORIGINAL_PATH}',
]),
new NotFoundRedirectOptions(
regular404: 'https://redirect-here.com/{ORIGINAL_PATH}/{DOMAIN}/?d={DOMAIN}&p={ORIGINAL_PATH}',
),
'https://redirect-here.com/foo/bar/doma.in/?d=doma.in&p=%2Ffoo%2Fbar',
];
yield 'invalid short URL' => [
new Uri('/foo'),
$this->notFoundType($this->requestForRoute(RedirectAction::class)),
new NotFoundRedirectOptions(['invalidShortUrl' => 'invalidShortUrl']),
new NotFoundRedirectOptions(invalidShortUrl: 'invalidShortUrl'),
'invalidShortUrl',
];
yield 'invalid short URL with path placeholder' => [
new Uri('/foo'),
$this->notFoundType($this->requestForRoute(RedirectAction::class)),
new NotFoundRedirectOptions(['invalidShortUrl' => 'https://redirect-here.com/{ORIGINAL_PATH}']),
new NotFoundRedirectOptions(invalidShortUrl: 'https://redirect-here.com/{ORIGINAL_PATH}'),
'https://redirect-here.com/foo',
];
}

View file

@ -55,7 +55,7 @@ class DomainRedirectsRequestTest extends TestCase
yield 'some values' => [['domain' => 'foo', 'regular404Redirect' => 'bar'], null, 'foo', null, 'bar', null];
yield 'fallbacks' => [
['domain' => 'domain', 'baseUrlRedirect' => 'bar'],
new NotFoundRedirectOptions(['regular404' => 'fallback', 'invalidShortUrl' => 'fallback2']),
new NotFoundRedirectOptions(invalidShortUrl: 'fallback2', regular404: 'fallback'),
'domain',
'bar',
'fallback',
@ -63,7 +63,7 @@ class DomainRedirectsRequestTest extends TestCase
];
yield 'fallback ignored' => [
['domain' => 'domain', 'regular404Redirect' => 'bar', 'invalidShortUrlRedirect' => null],
new NotFoundRedirectOptions(['regular404' => 'fallback', 'invalidShortUrl' => 'fallback2']),
new NotFoundRedirectOptions(invalidShortUrl: 'fallback2', regular404: 'fallback'),
'domain',
null,
'bar',