Added forwardQuery property in short URLs, that determines if the query should be forwarded to the long URL

This commit is contained in:
Alejandro Celaya 2021-10-02 09:32:04 +02:00
parent 60c8f23a63
commit 1ed6458b39
4 changed files with 39 additions and 1 deletions

View File

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration;
final class Version20211002072605 extends AbstractMigration
{
public function up(Schema $schema): void
{
$shortUrls = $schema->getTable('short_urls');
$this->skipIf($shortUrls->hasColumn('forward_query'));
$shortUrls->addColumn('forward_query', Types::BOOLEAN, ['default' => true]);
}
public function down(Schema $schema): void
{
$shortUrls = $schema->getTable('short_urls');
$this->skipIf(! $shortUrls->hasColumn('forward_query'));
$shortUrls->dropColumn('forward_query');
}
}

View File

@ -100,4 +100,9 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
->columnName('crawlable')
->option('default', false)
->build();
$builder->createField('forwardQuery', Types::BOOLEAN)
->columnName('forward_query')
->option('default', true)
->build();
};

View File

@ -43,6 +43,7 @@ class ShortUrl extends AbstractEntity
private ?string $title = null;
private bool $titleWasAutoResolved = false;
private bool $crawlable = false;
private bool $forwardQuery = true;
private function __construct()
{
@ -207,6 +208,11 @@ class ShortUrl extends AbstractEntity
return $this->crawlable;
}
public function forwardQuery(): bool
{
return $this->forwardQuery;
}
public function update(
ShortUrlEdit $shortUrlEdit,
?ShortUrlRelationResolverInterface $relationResolver = null,

View File

@ -21,9 +21,10 @@ class ShortUrlRedirectionBuilder implements ShortUrlRedirectionBuilderInterface
public function buildShortUrlRedirect(ShortUrl $shortUrl, array $currentQuery, ?string $extraPath = null): string
{
$uri = Uri::createFromString($shortUrl->getLongUrl());
$shouldForwardQuery = $shortUrl->forwardQuery();
return $uri
->withQuery($this->resolveQuery($uri, $currentQuery))
->withQuery($shouldForwardQuery ? $this->resolveQuery($uri, $currentQuery) : $uri->getQuery())
->withPath($this->resolvePath($uri, $extraPath))
->__toString();
}