mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Migrated UrlShortenerOptions to immutable object
This commit is contained in:
@@ -4,69 +4,17 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Options;
|
||||
|
||||
use Laminas\Stdlib\AbstractOptions;
|
||||
|
||||
use const Shlinkio\Shlink\DEFAULT_SHORT_CODES_LENGTH;
|
||||
|
||||
class UrlShortenerOptions extends AbstractOptions
|
||||
final class UrlShortenerOptions
|
||||
{
|
||||
protected $__strictMode__ = false; // phpcs:ignore
|
||||
|
||||
private array $domain = [];
|
||||
private int $defaultShortCodesLength = DEFAULT_SHORT_CODES_LENGTH;
|
||||
private bool $autoResolveTitles = false;
|
||||
private bool $appendExtraPath = false;
|
||||
private bool $multiSegmentSlugsEnabled = false;
|
||||
|
||||
public function domain(): array
|
||||
{
|
||||
return $this->domain;
|
||||
}
|
||||
|
||||
protected function setDomain(array $domain): self
|
||||
{
|
||||
$this->domain = $domain;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function defaultShortCodesLength(): int
|
||||
{
|
||||
return $this->defaultShortCodesLength;
|
||||
}
|
||||
|
||||
protected function setDefaultShortCodesLength(int $defaultShortCodesLength): self
|
||||
{
|
||||
$this->defaultShortCodesLength = $defaultShortCodesLength;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function autoResolveTitles(): bool
|
||||
{
|
||||
return $this->autoResolveTitles;
|
||||
}
|
||||
|
||||
protected function setAutoResolveTitles(bool $autoResolveTitles): void
|
||||
{
|
||||
$this->autoResolveTitles = $autoResolveTitles;
|
||||
}
|
||||
|
||||
public function appendExtraPath(): bool
|
||||
{
|
||||
return $this->appendExtraPath;
|
||||
}
|
||||
|
||||
protected function setAppendExtraPath(bool $appendExtraPath): void
|
||||
{
|
||||
$this->appendExtraPath = $appendExtraPath;
|
||||
}
|
||||
|
||||
public function multiSegmentSlugsEnabled(): bool
|
||||
{
|
||||
return $this->multiSegmentSlugsEnabled;
|
||||
}
|
||||
|
||||
protected function setMultiSegmentSlugsEnabled(bool $multiSegmentSlugsEnabled): void
|
||||
{
|
||||
$this->multiSegmentSlugsEnabled = $multiSegmentSlugsEnabled;
|
||||
public function __construct(
|
||||
/** @var array{schema: ?string, hostname: ?string} */
|
||||
public readonly array $domain = ['schema' => null, 'hostname' => null],
|
||||
public readonly int $defaultShortCodesLength = DEFAULT_SHORT_CODES_LENGTH,
|
||||
public readonly bool $autoResolveTitles = false,
|
||||
public readonly bool $appendExtraPath = false,
|
||||
public readonly bool $multiSegmentSlugsEnabled = false,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,16 +49,16 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface
|
||||
|
||||
private function shouldApplyLogic(?NotFoundType $notFoundType): bool
|
||||
{
|
||||
if ($notFoundType === null || ! $this->urlShortenerOptions->appendExtraPath()) {
|
||||
if ($notFoundType === null || ! $this->urlShortenerOptions->appendExtraPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
// If multi-segment slugs are enabled, the appropriate not-found type is "invalid_short_url"
|
||||
$this->urlShortenerOptions->multiSegmentSlugsEnabled() && $notFoundType->isInvalidShortUrl()
|
||||
$this->urlShortenerOptions->multiSegmentSlugsEnabled && $notFoundType->isInvalidShortUrl()
|
||||
) || (
|
||||
// If multi-segment slugs are disabled, the appropriate not-found type is "regular_404"
|
||||
! $this->urlShortenerOptions->multiSegmentSlugsEnabled() && $notFoundType->isRegularNotFound()
|
||||
! $this->urlShortenerOptions->multiSegmentSlugsEnabled && $notFoundType->isRegularNotFound()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface
|
||||
$longUrl = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $query, $extraPath);
|
||||
return $this->redirectResponseHelper->buildRedirectResponse($longUrl);
|
||||
} catch (ShortUrlNotFoundException) {
|
||||
if ($extraPath === null || ! $this->urlShortenerOptions->multiSegmentSlugsEnabled()) {
|
||||
if ($extraPath === null || ! $this->urlShortenerOptions->multiSegmentSlugsEnabled) {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,11 +46,11 @@ class UrlValidator implements UrlValidatorInterface, RequestMethodInterface
|
||||
|
||||
public function validateUrlWithTitle(string $url, bool $doValidate): ?string
|
||||
{
|
||||
if (! $doValidate && ! $this->options->autoResolveTitles()) {
|
||||
if (! $doValidate && ! $this->options->autoResolveTitles) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->options->autoResolveTitles()) {
|
||||
if (! $this->options->autoResolveTitles) {
|
||||
$this->validateUrlAndGetResponse($url, self::METHOD_HEAD);
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user