Migrated DeleteShortUrlsOptions to immutable object

This commit is contained in:
Alejandro Celaya 2022-09-17 13:04:49 +02:00
parent 9685929824
commit 784908420e
4 changed files with 13 additions and 35 deletions

View File

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

View File

@ -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,
) {
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}