mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Added tracking to tell if short URL titles were autogenerated or not
This commit is contained in:
parent
7192480751
commit
7824dddef7
@ -21,6 +21,9 @@ final class Version20210202181026 extends AbstractMigration
|
||||
'notnull' => false,
|
||||
'length' => 512,
|
||||
]);
|
||||
$shortUrls->addColumn('title_was_auto_resolved', Types::BOOLEAN, [
|
||||
'default' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
@ -28,5 +31,14 @@ final class Version20210202181026 extends AbstractMigration
|
||||
$shortUrls = $schema->getTable('short_urls');
|
||||
$this->skipIf(! $shortUrls->hasColumn(self::TITLE));
|
||||
$shortUrls->dropColumn(self::TITLE);
|
||||
$shortUrls->dropColumn('title_was_auto_resolved');
|
||||
}
|
||||
|
||||
/**
|
||||
* @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
|
||||
*/
|
||||
public function isTransactional(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -90,4 +90,9 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
|
||||
->length(512)
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('titleWasAutoResolved', Types::BOOLEAN)
|
||||
->columnName('title_was_auto_resolved')
|
||||
->option('default', false)
|
||||
->build();
|
||||
};
|
||||
|
@ -39,6 +39,7 @@ class ShortUrl extends AbstractEntity
|
||||
private ?string $importOriginalShortCode = null;
|
||||
private ?ApiKey $authorApiKey = null;
|
||||
private ?string $title = null;
|
||||
private bool $titleWasAutoResolved = false;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
@ -74,6 +75,7 @@ class ShortUrl extends AbstractEntity
|
||||
$instance->domain = $relationResolver->resolveDomain($meta->getDomain());
|
||||
$instance->authorApiKey = $meta->getApiKey();
|
||||
$instance->title = $meta->getTitle();
|
||||
$instance->titleWasAutoResolved = $meta->titleWasAutoResolved();
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ final class ShortUrlMeta
|
||||
private ?ApiKey $apiKey = null;
|
||||
private array $tags = [];
|
||||
private ?string $title = null;
|
||||
private bool $titleWasAutoResolved = false;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
@ -173,10 +174,16 @@ final class ShortUrlMeta
|
||||
return $this->title !== null;
|
||||
}
|
||||
|
||||
public function withResolvedTitle(?string $title): self
|
||||
public function titleWasAutoResolved(): bool
|
||||
{
|
||||
return $this->titleWasAutoResolved;
|
||||
}
|
||||
|
||||
public function withResolvedTitle(string $title): self
|
||||
{
|
||||
$copy = clone $this;
|
||||
$copy->title = $title;
|
||||
$copy->titleWasAutoResolved = true;
|
||||
|
||||
return $copy;
|
||||
}
|
||||
|
@ -45,13 +45,7 @@ class UrlShortener implements UrlShortenerInterface
|
||||
return $existingShortUrl;
|
||||
}
|
||||
|
||||
if ($meta->hasTitle()) {
|
||||
$this->urlValidator->validateUrl($meta->getLongUrl(), $meta->doValidateUrl());
|
||||
} else {
|
||||
$meta = $meta->withResolvedTitle(
|
||||
$this->urlValidator->validateUrlWithTitle($meta->getLongUrl(), $meta->doValidateUrl()),
|
||||
);
|
||||
}
|
||||
$meta = $this->processTitleAndValidateUrl($meta);
|
||||
|
||||
return $this->em->transactional(function () use ($meta) {
|
||||
$shortUrl = ShortUrl::fromMeta($meta, $this->relationResolver);
|
||||
@ -88,4 +82,15 @@ class UrlShortener implements UrlShortenerInterface
|
||||
throw NonUniqueSlugException::fromSlug($shortUrlToBeCreated->getShortCode(), $domainAuthority);
|
||||
}
|
||||
}
|
||||
|
||||
private function processTitleAndValidateUrl(ShortUrlMeta $meta): ShortUrlMeta
|
||||
{
|
||||
if ($meta->hasTitle()) {
|
||||
$this->urlValidator->validateUrl($meta->getLongUrl(), $meta->doValidateUrl());
|
||||
return $meta;
|
||||
}
|
||||
|
||||
$title = $this->urlValidator->validateUrlWithTitle($meta->getLongUrl(), $meta->doValidateUrl());
|
||||
return $title === null ? $meta : $meta->withResolvedTitle($title);
|
||||
}
|
||||
}
|
||||
|
@ -75,6 +75,20 @@ class UrlShortenerTest extends TestCase
|
||||
$this->urlValidator->validateUrl($longUrl, null)->shouldHaveBeenCalledTimes($validateCallsNum);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideTitles
|
||||
*/
|
||||
public function urlIsProperlyShortenedWithExpectedResolvedTitle(?string $title): void
|
||||
{
|
||||
$validateWithTitle = $this->urlValidator->validateUrlWithTitle(Argument::cetera())->willReturn($title);
|
||||
|
||||
$shortUrl = $this->urlShortener->shorten(ShortUrlMeta::fromRawData(['longUrl' => 'foo']));
|
||||
|
||||
self::assertEquals($title, $shortUrl->getTitle());
|
||||
$validateWithTitle->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideTitles(): iterable
|
||||
{
|
||||
yield 'no title' => [null, 1, 0];
|
||||
|
Loading…
Reference in New Issue
Block a user