mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-11 09:43:13 +08:00
Simplify NotFoundRedirectConfigInterface with property hooks and asymetric visibility
This commit is contained in:
@@ -6,33 +6,7 @@ namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
final class EmptyNotFoundRedirectConfig implements NotFoundRedirectConfigInterface
|
||||
{
|
||||
public function invalidShortUrlRedirect(): string|null
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function hasInvalidShortUrlRedirect(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function regular404Redirect(): string|null
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function hasRegular404Redirect(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function baseUrlRedirect(): string|null
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function hasBaseUrlRedirect(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
private(set) string|null $invalidShortUrlRedirect = null;
|
||||
private(set) string|null $regular404Redirect = null;
|
||||
private(set) string|null $baseUrlRedirect = null;
|
||||
}
|
||||
|
||||
@@ -6,15 +6,7 @@ namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
interface NotFoundRedirectConfigInterface
|
||||
{
|
||||
public function invalidShortUrlRedirect(): string|null;
|
||||
|
||||
public function hasInvalidShortUrlRedirect(): bool;
|
||||
|
||||
public function regular404Redirect(): string|null;
|
||||
|
||||
public function hasRegular404Redirect(): bool;
|
||||
|
||||
public function baseUrlRedirect(): string|null;
|
||||
|
||||
public function hasBaseUrlRedirect(): bool;
|
||||
public string|null $invalidShortUrlRedirect { get; }
|
||||
public string|null $regular404Redirect { get; }
|
||||
public string|null $baseUrlRedirect { get; }
|
||||
}
|
||||
|
||||
@@ -32,10 +32,9 @@ class NotFoundRedirectResolver implements NotFoundRedirectResolverInterface
|
||||
UriInterface $currentUri,
|
||||
): ResponseInterface|null {
|
||||
$urlToRedirectTo = match (true) {
|
||||
$notFoundType->isBaseUrl() && $config->hasBaseUrlRedirect() => $config->baseUrlRedirect(),
|
||||
$notFoundType->isRegularNotFound() && $config->hasRegular404Redirect() => $config->regular404Redirect(),
|
||||
$notFoundType->isInvalidShortUrl() && $config->hasInvalidShortUrlRedirect() =>
|
||||
$config->invalidShortUrlRedirect(),
|
||||
$notFoundType->isBaseUrl() => $config->baseUrlRedirect,
|
||||
$notFoundType->isRegularNotFound() => $config->regular404Redirect,
|
||||
$notFoundType->isInvalidShortUrl() => $config->invalidShortUrlRedirect,
|
||||
default => null,
|
||||
};
|
||||
|
||||
|
||||
@@ -6,12 +6,12 @@ namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
final class NotFoundRedirects implements JsonSerializable
|
||||
final readonly class NotFoundRedirects implements JsonSerializable
|
||||
{
|
||||
private function __construct(
|
||||
public readonly string|null $baseUrlRedirect,
|
||||
public readonly string|null $regular404Redirect,
|
||||
public readonly string|null $invalidShortUrlRedirect,
|
||||
public string|null $baseUrlRedirect,
|
||||
public string|null $regular404Redirect,
|
||||
public string|null $invalidShortUrlRedirect,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ final class NotFoundRedirects implements JsonSerializable
|
||||
|
||||
public static function fromConfig(NotFoundRedirectConfigInterface $config): self
|
||||
{
|
||||
return new self($config->baseUrlRedirect(), $config->regular404Redirect(), $config->invalidShortUrlRedirect());
|
||||
return new self($config->baseUrlRedirect, $config->regular404Redirect, $config->invalidShortUrlRedirect);
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
|
||||
@@ -10,48 +10,18 @@ use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
|
||||
final readonly class NotFoundRedirectOptions implements NotFoundRedirectConfigInterface
|
||||
{
|
||||
public function __construct(
|
||||
public string|null $invalidShortUrl = null,
|
||||
public string|null $regular404 = null,
|
||||
public string|null $baseUrl = null,
|
||||
public string|null $invalidShortUrlRedirect = null,
|
||||
public string|null $regular404Redirect = null,
|
||||
public string|null $baseUrlRedirect = null,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromEnv(): self
|
||||
{
|
||||
return new self(
|
||||
invalidShortUrl: EnvVars::DEFAULT_INVALID_SHORT_URL_REDIRECT->loadFromEnv(),
|
||||
regular404: EnvVars::DEFAULT_REGULAR_404_REDIRECT->loadFromEnv(),
|
||||
baseUrl: EnvVars::DEFAULT_BASE_URL_REDIRECT->loadFromEnv(),
|
||||
invalidShortUrlRedirect: EnvVars::DEFAULT_INVALID_SHORT_URL_REDIRECT->loadFromEnv(),
|
||||
regular404Redirect: EnvVars::DEFAULT_REGULAR_404_REDIRECT->loadFromEnv(),
|
||||
baseUrlRedirect: EnvVars::DEFAULT_BASE_URL_REDIRECT->loadFromEnv(),
|
||||
);
|
||||
}
|
||||
|
||||
public function invalidShortUrlRedirect(): string|null
|
||||
{
|
||||
return $this->invalidShortUrl;
|
||||
}
|
||||
|
||||
public function hasInvalidShortUrlRedirect(): bool
|
||||
{
|
||||
return $this->invalidShortUrl !== null;
|
||||
}
|
||||
|
||||
public function regular404Redirect(): string|null
|
||||
{
|
||||
return $this->regular404;
|
||||
}
|
||||
|
||||
public function hasRegular404Redirect(): bool
|
||||
{
|
||||
return $this->regular404 !== null;
|
||||
}
|
||||
|
||||
public function baseUrlRedirect(): string|null
|
||||
{
|
||||
return $this->baseUrl;
|
||||
}
|
||||
|
||||
public function hasBaseUrlRedirect(): bool
|
||||
{
|
||||
return $this->baseUrl !== null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ class Domain extends AbstractEntity implements JsonSerializable, NotFoundRedirec
|
||||
|
||||
private function __construct(
|
||||
public readonly string $authority,
|
||||
private string|null $baseUrlRedirect = null,
|
||||
private string|null $regular404Redirect = null,
|
||||
private string|null $invalidShortUrlRedirect = null,
|
||||
private(set) string|null $baseUrlRedirect = null,
|
||||
private(set) string|null $regular404Redirect = null,
|
||||
private(set) string|null $invalidShortUrlRedirect = null,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -31,36 +31,6 @@ class Domain extends AbstractEntity implements JsonSerializable, NotFoundRedirec
|
||||
return $this->authority;
|
||||
}
|
||||
|
||||
public function invalidShortUrlRedirect(): string|null
|
||||
{
|
||||
return $this->invalidShortUrlRedirect;
|
||||
}
|
||||
|
||||
public function hasInvalidShortUrlRedirect(): bool
|
||||
{
|
||||
return $this->invalidShortUrlRedirect !== null;
|
||||
}
|
||||
|
||||
public function regular404Redirect(): string|null
|
||||
{
|
||||
return $this->regular404Redirect;
|
||||
}
|
||||
|
||||
public function hasRegular404Redirect(): bool
|
||||
{
|
||||
return $this->regular404Redirect !== null;
|
||||
}
|
||||
|
||||
public function baseUrlRedirect(): string|null
|
||||
{
|
||||
return $this->baseUrlRedirect;
|
||||
}
|
||||
|
||||
public function hasBaseUrlRedirect(): bool
|
||||
{
|
||||
return $this->baseUrlRedirect !== null;
|
||||
}
|
||||
|
||||
public function configureNotFoundRedirects(NotFoundRedirects $redirects): void
|
||||
{
|
||||
$this->baseUrlRedirect = $redirects->baseUrlRedirect;
|
||||
|
||||
Reference in New Issue
Block a user