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,
?ShortUrlRelationResolverInterface $relationResolver = null
): void {
if ($shortUrlEdit->hasValidSince()) {
if ($shortUrlEdit->validSinceWasProvided()) {
$this->validSince = $shortUrlEdit->validSince();
}
if ($shortUrlEdit->hasValidUntil()) {
if ($shortUrlEdit->validUntilWasProvided()) {
$this->validUntil = $shortUrlEdit->validUntil();
}
if ($shortUrlEdit->hasMaxVisits()) {
if ($shortUrlEdit->maxVisitsWasProvided()) {
$this->maxVisits = $shortUrlEdit->maxVisits();
}
if ($shortUrlEdit->hasLongUrl()) {
$this->longUrl = $shortUrlEdit->longUrl();
if ($shortUrlEdit->longUrlWasProvided()) {
$this->longUrl = $shortUrlEdit->longUrl() ?? $this->longUrl;
}
if ($shortUrlEdit->hasTags()) {
if ($shortUrlEdit->tagsWereProvided()) {
$relationResolver = $relationResolver ?? new SimpleShortUrlRelationResolver();
$this->tags = $relationResolver->resolveTags($shortUrlEdit->tags());
}
if ($shortUrlEdit->hasTitle()) {
if (
$this->title === null
|| $shortUrlEdit->titleWasProvided()
|| ($this->titleWasAutoResolved && $shortUrlEdit->titleWasAutoResolved())
) {
$this->title = $shortUrlEdit->title();
$this->titleWasAutoResolved = $shortUrlEdit->titleWasAutoResolved();
}
}

View File

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

View File

@ -61,8 +61,8 @@ class ShortUrlService implements ShortUrlServiceInterface
ShortUrlEdit $shortUrlEdit,
?ApiKey $apiKey = null
): ShortUrl {
if ($shortUrlEdit->hasLongUrl()) {
$this->urlValidator->validateUrl($shortUrlEdit->longUrl(), $shortUrlEdit->doValidateUrl());
if ($shortUrlEdit->longUrlWasProvided()) {
$shortUrlEdit = $this->processTitleAndValidateUrl($shortUrlEdit);
}
$shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
@ -72,4 +72,15 @@ class ShortUrlService implements ShortUrlServiceInterface
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());
$findShortUrl->shouldHaveBeenCalled();
$flush->shouldHaveBeenCalled();
$this->urlValidator->validateUrl(
$this->urlValidator->validateUrlWithTitle(
$shortUrlEdit->longUrl(),
$shortUrlEdit->doValidateUrl(),
)->shouldHaveBeenCalledTimes($expectedValidateCalls);