diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 54a861e6..1ce6414f 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -22,7 +22,7 @@ return [ ErrorHandler\NotFoundTemplateHandler::class => InvokableFactory::class, Options\AppOptions::class => [ValinorConfigFactory::class, 'config.app_options'], - Options\DeleteShortUrlsOptions::class => ConfigAbstractFactory::class, + Options\DeleteShortUrlsOptions::class => [ValinorConfigFactory::class, 'config.delete_short_urls'], Options\NotFoundRedirectOptions::class => ConfigAbstractFactory::class, Options\RedirectOptions::class => ConfigAbstractFactory::class, Options\UrlShortenerOptions::class => ConfigAbstractFactory::class, @@ -86,7 +86,6 @@ return [ Domain\DomainService::class, ], - Options\DeleteShortUrlsOptions::class => ['config.delete_short_urls'], Options\NotFoundRedirectOptions::class => ['config.not_found_redirects'], Options\RedirectOptions::class => ['config.redirects'], Options\UrlShortenerOptions::class => ['config.url_shortener'], diff --git a/module/Core/src/Options/DeleteShortUrlsOptions.php b/module/Core/src/Options/DeleteShortUrlsOptions.php index ff1c356a..a645181b 100644 --- a/module/Core/src/Options/DeleteShortUrlsOptions.php +++ b/module/Core/src/Options/DeleteShortUrlsOptions.php @@ -4,34 +4,13 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Options; -use Laminas\Stdlib\AbstractOptions; - use const Shlinkio\Shlink\DEFAULT_DELETE_SHORT_URL_THRESHOLD; -class DeleteShortUrlsOptions extends AbstractOptions +final class DeleteShortUrlsOptions { - private int $visitsThreshold = DEFAULT_DELETE_SHORT_URL_THRESHOLD; - private bool $checkVisitsThreshold = true; - - public function getVisitsThreshold(): int - { - return $this->visitsThreshold; - } - - protected function setVisitsThreshold(int $visitsThreshold): self - { - $this->visitsThreshold = $visitsThreshold; - return $this; - } - - public function doCheckVisitsThreshold(): bool - { - return $this->checkVisitsThreshold; - } - - protected function setCheckVisitsThreshold(bool $checkVisitsThreshold): self - { - $this->checkVisitsThreshold = $checkVisitsThreshold; - return $this; + public function __construct( + public readonly int $visitsThreshold = DEFAULT_DELETE_SHORT_URL_THRESHOLD, + public readonly bool $checkVisitsThreshold = true, + ) { } } diff --git a/module/Core/src/Service/ShortUrl/DeleteShortUrlService.php b/module/Core/src/Service/ShortUrl/DeleteShortUrlService.php index e6f2e82d..d4d6803f 100644 --- a/module/Core/src/Service/ShortUrl/DeleteShortUrlService.php +++ b/module/Core/src/Service/ShortUrl/DeleteShortUrlService.php @@ -32,7 +32,7 @@ class DeleteShortUrlService implements DeleteShortUrlServiceInterface $shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey); if (! $ignoreThreshold && $this->isThresholdReached($shortUrl)) { throw Exception\DeleteShortUrlException::fromVisitsThreshold( - $this->deleteShortUrlsOptions->getVisitsThreshold(), + $this->deleteShortUrlsOptions->visitsThreshold, $identifier, ); } @@ -43,10 +43,10 @@ class DeleteShortUrlService implements DeleteShortUrlServiceInterface private function isThresholdReached(ShortUrl $shortUrl): bool { - if (! $this->deleteShortUrlsOptions->doCheckVisitsThreshold()) { + if (! $this->deleteShortUrlsOptions->checkVisitsThreshold) { return false; } - return $shortUrl->getVisitsCount() >= $this->deleteShortUrlsOptions->getVisitsThreshold(); + return $shortUrl->getVisitsCount() >= $this->deleteShortUrlsOptions->visitsThreshold; } } diff --git a/module/Core/test/Service/ShortUrl/DeleteShortUrlServiceTest.php b/module/Core/test/Service/ShortUrl/DeleteShortUrlServiceTest.php index 391d52fd..87a6582f 100644 --- a/module/Core/test/Service/ShortUrl/DeleteShortUrlServiceTest.php +++ b/module/Core/test/Service/ShortUrl/DeleteShortUrlServiceTest.php @@ -102,9 +102,9 @@ class DeleteShortUrlServiceTest extends TestCase private function createService(bool $checkVisitsThreshold = true, int $visitsThreshold = 5): DeleteShortUrlService { - return new DeleteShortUrlService($this->em->reveal(), new DeleteShortUrlsOptions([ - 'visitsThreshold' => $visitsThreshold, - 'checkVisitsThreshold' => $checkVisitsThreshold, - ]), $this->urlResolver->reveal()); + return new DeleteShortUrlService($this->em->reveal(), new DeleteShortUrlsOptions( + $visitsThreshold, + $checkVisitsThreshold, + ), $this->urlResolver->reveal()); } }