Allowed to resolve title during short URL edition if it has to

This commit is contained in:
Alejandro Celaya 2021-02-04 23:02:26 +01:00
parent ed18f10b94
commit 71e91a541f
4 changed files with 47 additions and 16 deletions

View File

@ -171,24 +171,29 @@ class ShortUrl extends AbstractEntity
ShortUrlEdit $shortUrlEdit, ShortUrlEdit $shortUrlEdit,
?ShortUrlRelationResolverInterface $relationResolver = null ?ShortUrlRelationResolverInterface $relationResolver = null
): void { ): void {
if ($shortUrlEdit->hasValidSince()) { if ($shortUrlEdit->validSinceWasProvided()) {
$this->validSince = $shortUrlEdit->validSince(); $this->validSince = $shortUrlEdit->validSince();
} }
if ($shortUrlEdit->hasValidUntil()) { if ($shortUrlEdit->validUntilWasProvided()) {
$this->validUntil = $shortUrlEdit->validUntil(); $this->validUntil = $shortUrlEdit->validUntil();
} }
if ($shortUrlEdit->hasMaxVisits()) { if ($shortUrlEdit->maxVisitsWasProvided()) {
$this->maxVisits = $shortUrlEdit->maxVisits(); $this->maxVisits = $shortUrlEdit->maxVisits();
} }
if ($shortUrlEdit->hasLongUrl()) { if ($shortUrlEdit->longUrlWasProvided()) {
$this->longUrl = $shortUrlEdit->longUrl(); $this->longUrl = $shortUrlEdit->longUrl() ?? $this->longUrl;
} }
if ($shortUrlEdit->hasTags()) { if ($shortUrlEdit->tagsWereProvided()) {
$relationResolver = $relationResolver ?? new SimpleShortUrlRelationResolver(); $relationResolver = $relationResolver ?? new SimpleShortUrlRelationResolver();
$this->tags = $relationResolver->resolveTags($shortUrlEdit->tags()); $this->tags = $relationResolver->resolveTags($shortUrlEdit->tags());
} }
if ($shortUrlEdit->hasTitle()) { if (
$this->title === null
|| $shortUrlEdit->titleWasProvided()
|| ($this->titleWasAutoResolved && $shortUrlEdit->titleWasAutoResolved())
) {
$this->title = $shortUrlEdit->title(); $this->title = $shortUrlEdit->title();
$this->titleWasAutoResolved = $shortUrlEdit->titleWasAutoResolved();
} }
} }

View File

@ -27,6 +27,7 @@ final class ShortUrlEdit
private array $tags = []; private array $tags = [];
private bool $titlePropWasProvided = false; private bool $titlePropWasProvided = false;
private ?string $title = null; private ?string $title = null;
private bool $titleWasAutoResolved = false;
private ?bool $validateUrl = null; private ?bool $validateUrl = null;
private function __construct() private function __construct()
@ -74,7 +75,7 @@ final class ShortUrlEdit
return $this->longUrl; return $this->longUrl;
} }
public function hasLongUrl(): bool public function longUrlWasProvided(): bool
{ {
return $this->longUrlPropWasProvided && $this->longUrl !== null; return $this->longUrlPropWasProvided && $this->longUrl !== null;
} }
@ -84,7 +85,7 @@ final class ShortUrlEdit
return $this->validSince; return $this->validSince;
} }
public function hasValidSince(): bool public function validSinceWasProvided(): bool
{ {
return $this->validSincePropWasProvided; return $this->validSincePropWasProvided;
} }
@ -94,7 +95,7 @@ final class ShortUrlEdit
return $this->validUntil; return $this->validUntil;
} }
public function hasValidUntil(): bool public function validUntilWasProvided(): bool
{ {
return $this->validUntilPropWasProvided; return $this->validUntilPropWasProvided;
} }
@ -104,7 +105,7 @@ final class ShortUrlEdit
return $this->maxVisits; return $this->maxVisits;
} }
public function hasMaxVisits(): bool public function maxVisitsWasProvided(): bool
{ {
return $this->maxVisitsPropWasProvided; return $this->maxVisitsPropWasProvided;
} }
@ -117,7 +118,7 @@ final class ShortUrlEdit
return $this->tags; return $this->tags;
} }
public function hasTags(): bool public function tagsWereProvided(): bool
{ {
return $this->tagsPropWasProvided; return $this->tagsPropWasProvided;
} }
@ -127,11 +128,25 @@ final class ShortUrlEdit
return $this->title; return $this->title;
} }
public function hasTitle(): bool public function titleWasProvided(): bool
{ {
return $this->titlePropWasProvided; return $this->titlePropWasProvided;
} }
public function titleWasAutoResolved(): bool
{
return $this->titleWasAutoResolved;
}
public function withResolvedTitle(string $title): self
{
$copy = clone $this;
$copy->title = $title;
$copy->titleWasAutoResolved = true;
return $copy;
}
public function doValidateUrl(): ?bool public function doValidateUrl(): ?bool
{ {
return $this->validateUrl; return $this->validateUrl;

View File

@ -61,8 +61,8 @@ class ShortUrlService implements ShortUrlServiceInterface
ShortUrlEdit $shortUrlEdit, ShortUrlEdit $shortUrlEdit,
?ApiKey $apiKey = null ?ApiKey $apiKey = null
): ShortUrl { ): ShortUrl {
if ($shortUrlEdit->hasLongUrl()) { if ($shortUrlEdit->longUrlWasProvided()) {
$this->urlValidator->validateUrl($shortUrlEdit->longUrl(), $shortUrlEdit->doValidateUrl()); $shortUrlEdit = $this->processTitleAndValidateUrl($shortUrlEdit);
} }
$shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey); $shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
@ -72,4 +72,15 @@ class ShortUrlService implements ShortUrlServiceInterface
return $shortUrl; return $shortUrl;
} }
private function processTitleAndValidateUrl(ShortUrlEdit $shortUrlEdit): ShortUrlEdit
{
if ($shortUrlEdit->titleWasProvided()) {
$this->urlValidator->validateUrl($shortUrlEdit->longUrl(), $shortUrlEdit->doValidateUrl());
return $shortUrlEdit;
}
$title = $this->urlValidator->validateUrlWithTitle($shortUrlEdit->longUrl(), $shortUrlEdit->doValidateUrl());
return $title === null ? $shortUrlEdit : $shortUrlEdit->withResolvedTitle($title);
}
} }

View File

@ -102,7 +102,7 @@ class ShortUrlServiceTest extends TestCase
self::assertEquals($shortUrlEdit->longUrl() ?? $originalLongUrl, $shortUrl->getLongUrl()); self::assertEquals($shortUrlEdit->longUrl() ?? $originalLongUrl, $shortUrl->getLongUrl());
$findShortUrl->shouldHaveBeenCalled(); $findShortUrl->shouldHaveBeenCalled();
$flush->shouldHaveBeenCalled(); $flush->shouldHaveBeenCalled();
$this->urlValidator->validateUrl( $this->urlValidator->validateUrlWithTitle(
$shortUrlEdit->longUrl(), $shortUrlEdit->longUrl(),
$shortUrlEdit->doValidateUrl(), $shortUrlEdit->doValidateUrl(),
)->shouldHaveBeenCalledTimes($expectedValidateCalls); )->shouldHaveBeenCalledTimes($expectedValidateCalls);