Create new entities for redirect rules

This commit is contained in:
Alejandro Celaya 2024-02-24 18:17:09 +01:00
parent 752100f1ce
commit c91a534d1a
8 changed files with 145 additions and 0 deletions

View File

@ -28,15 +28,18 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
fieldWithUtf8Charset($builder->createField('baseUrlRedirect', Types::TEXT), $emConfig) fieldWithUtf8Charset($builder->createField('baseUrlRedirect', Types::TEXT), $emConfig)
->columnName('base_url_redirect') ->columnName('base_url_redirect')
->nullable() ->nullable()
->length(2048)
->build(); ->build();
fieldWithUtf8Charset($builder->createField('regular404Redirect', Types::TEXT), $emConfig) fieldWithUtf8Charset($builder->createField('regular404Redirect', Types::TEXT), $emConfig)
->columnName('regular_not_found_redirect') ->columnName('regular_not_found_redirect')
->nullable() ->nullable()
->length(2048)
->build(); ->build();
fieldWithUtf8Charset($builder->createField('invalidShortUrlRedirect', Types::TEXT), $emConfig) fieldWithUtf8Charset($builder->createField('invalidShortUrlRedirect', Types::TEXT), $emConfig)
->columnName('invalid_short_url_redirect') ->columnName('invalid_short_url_redirect')
->nullable() ->nullable()
->length(2048)
->build(); ->build();
}; };

View File

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\Builder\FieldBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
use Shlinkio\Shlink\Core\RedirectRule\Model\RedirectConditionType;
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable(determineTableName('redirect_conditions', $emConfig));
$builder->createField('id', Types::BIGINT)
->columnName('id')
->makePrimaryKey()
->generatedValue('IDENTITY')
->option('unsigned', true)
->build();
fieldWithUtf8Charset($builder->createField('name', Types::STRING), $emConfig)
->columnName('name')
->length(512)
->build();
$builder->addUniqueConstraint(['name'], 'UQ_name');
(new FieldBuilder($builder, [
'fieldName' => 'type',
'type' => Types::STRING,
'enumType' => RedirectConditionType::class,
]))->columnName('type')
->length(255)
->build();
fieldWithUtf8Charset($builder->createField('matchKey', Types::STRING), $emConfig)
->columnName('match_key')
->length(512)
->nullable()
->build();
fieldWithUtf8Charset($builder->createField('matchValue', Types::STRING), $emConfig)
->columnName('match_value')
->length(512)
->build();
};

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable(determineTableName('short_url_redirect_rules', $emConfig));
$builder->createField('id', Types::BIGINT)
->columnName('id')
->makePrimaryKey()
->generatedValue('IDENTITY')
->option('unsigned', true)
->build();
$builder->createField('priority', Types::INTEGER)
->columnName('priority')
->build();
fieldWithUtf8Charset($builder->createField('longUrl', Types::TEXT), $emConfig)
->columnName('long_url')
->length(2048)
->build();
$builder->createManyToOne('shortUrl', ShortUrl\Entity\ShortUrl::class)
->addJoinColumn('short_url_id', 'id', false, false, 'CASCADE')
->build();
$builder->createManyToMany('conditions', RedirectRule\Entity\RedirectCondition::class)
->setJoinTable(determineTableName('redirect_conditions_in_short_url_redirect_rules', $emConfig))
->addInverseJoinColumn('redirect_condition_id', 'id', onDelete: 'CASCADE')
->addJoinColumn('short_url_redirect_rule_id', 'id', onDelete: 'CASCADE')
->build();
};

View File

@ -32,6 +32,7 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
fieldWithUtf8Charset($builder->createField('longUrl', Types::TEXT), $emConfig) fieldWithUtf8Charset($builder->createField('longUrl', Types::TEXT), $emConfig)
->columnName('long_url') ->columnName('long_url')
->length(2048)
->build(); ->build();
$builder->createManyToOne('shortUrl', ShortUrl\Entity\ShortUrl::class) $builder->createManyToOne('shortUrl', ShortUrl\Entity\ShortUrl::class)

View File

@ -25,6 +25,7 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
fieldWithUtf8Charset($builder->createField('longUrl', Types::TEXT), $emConfig) fieldWithUtf8Charset($builder->createField('longUrl', Types::TEXT), $emConfig)
->columnName('original_url') // Rename to long_url some day? ¯\_(ツ)_/¯ ->columnName('original_url') // Rename to long_url some day? ¯\_(ツ)_/¯
->length(2048)
->build(); ->build();
fieldWithUtf8Charset($builder->createField('shortCode', Types::STRING), $emConfig, 'bin') fieldWithUtf8Charset($builder->createField('shortCode', Types::STRING), $emConfig, 'bin')

View File

@ -0,0 +1,17 @@
<?php
namespace Shlinkio\Shlink\Core\RedirectRule\Entity;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\RedirectRule\Model\RedirectConditionType;
class RedirectCondition extends AbstractEntity
{
public function __construct(
public readonly string $name,
public readonly RedirectConditionType $type,
public readonly string $matchValue,
public readonly ?string $matchKey = null,
) {
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Shlinkio\Shlink\Core\RedirectRule\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
class ShortUrlRedirectRule extends AbstractEntity
{
/**
* @param Collection<RedirectCondition> $conditions
*/
public function __construct(
private readonly ShortUrl $shortUrl, // No need to read this field. It's used by doctrine
public readonly int $priority,
public readonly string $longUrl,
public readonly Collection $conditions = new ArrayCollection(),
) {
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Shlinkio\Shlink\Core\RedirectRule\Model;
enum RedirectConditionType: string
{
case DEVICE = 'device';
// case LANGUAGE = 'language';
// case QUERY_PARAM = 'query';
}