From c91a534d1a09db66d4df5ad11099f31db1ca6463 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 24 Feb 2024 18:17:09 +0100 Subject: [PATCH] Create new entities for redirect rules --- ...inkio.Shlink.Core.Domain.Entity.Domain.php | 3 ++ ....RedirectRule.Entity.RedirectCondition.php | 50 +++++++++++++++++++ ...directRule.Entity.ShortUrlRedirectRule.php | 41 +++++++++++++++ ...ink.Core.ShortUrl.Entity.DeviceLongUrl.php | 1 + ...o.Shlink.Core.ShortUrl.Entity.ShortUrl.php | 1 + .../RedirectRule/Entity/RedirectCondition.php | 17 +++++++ .../Entity/ShortUrlRedirectRule.php | 22 ++++++++ .../Model/RedirectConditionType.php | 10 ++++ 8 files changed, 145 insertions(+) create mode 100644 module/Core/config/entities-mappings/Shlinkio.Shlink.Core.RedirectRule.Entity.RedirectCondition.php create mode 100644 module/Core/config/entities-mappings/Shlinkio.Shlink.Core.RedirectRule.Entity.ShortUrlRedirectRule.php create mode 100644 module/Core/src/RedirectRule/Entity/RedirectCondition.php create mode 100644 module/Core/src/RedirectRule/Entity/ShortUrlRedirectRule.php create mode 100644 module/Core/src/RedirectRule/Model/RedirectConditionType.php diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Domain.Entity.Domain.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Domain.Entity.Domain.php index e02d1f7e..ad77476a 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Domain.Entity.Domain.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Domain.Entity.Domain.php @@ -28,15 +28,18 @@ return static function (ClassMetadata $metadata, array $emConfig): void { fieldWithUtf8Charset($builder->createField('baseUrlRedirect', Types::TEXT), $emConfig) ->columnName('base_url_redirect') ->nullable() + ->length(2048) ->build(); fieldWithUtf8Charset($builder->createField('regular404Redirect', Types::TEXT), $emConfig) ->columnName('regular_not_found_redirect') ->nullable() + ->length(2048) ->build(); fieldWithUtf8Charset($builder->createField('invalidShortUrlRedirect', Types::TEXT), $emConfig) ->columnName('invalid_short_url_redirect') ->nullable() + ->length(2048) ->build(); }; diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.RedirectRule.Entity.RedirectCondition.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.RedirectRule.Entity.RedirectCondition.php new file mode 100644 index 00000000..2c1e1bdc --- /dev/null +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.RedirectRule.Entity.RedirectCondition.php @@ -0,0 +1,50 @@ +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(); +}; diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.RedirectRule.Entity.ShortUrlRedirectRule.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.RedirectRule.Entity.ShortUrlRedirectRule.php new file mode 100644 index 00000000..4ffd60d7 --- /dev/null +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.RedirectRule.Entity.ShortUrlRedirectRule.php @@ -0,0 +1,41 @@ +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(); +}; diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.DeviceLongUrl.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.DeviceLongUrl.php index 3620ea54..bcd3702e 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.DeviceLongUrl.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.DeviceLongUrl.php @@ -32,6 +32,7 @@ return static function (ClassMetadata $metadata, array $emConfig): void { fieldWithUtf8Charset($builder->createField('longUrl', Types::TEXT), $emConfig) ->columnName('long_url') + ->length(2048) ->build(); $builder->createManyToOne('shortUrl', ShortUrl\Entity\ShortUrl::class) diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.ShortUrl.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.ShortUrl.php index f33530ec..4967482e 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.ShortUrl.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.ShortUrl.Entity.ShortUrl.php @@ -25,6 +25,7 @@ return static function (ClassMetadata $metadata, array $emConfig): void { fieldWithUtf8Charset($builder->createField('longUrl', Types::TEXT), $emConfig) ->columnName('original_url') // Rename to long_url some day? ¯\_(ツ)_/¯ + ->length(2048) ->build(); fieldWithUtf8Charset($builder->createField('shortCode', Types::STRING), $emConfig, 'bin') diff --git a/module/Core/src/RedirectRule/Entity/RedirectCondition.php b/module/Core/src/RedirectRule/Entity/RedirectCondition.php new file mode 100644 index 00000000..367d96d9 --- /dev/null +++ b/module/Core/src/RedirectRule/Entity/RedirectCondition.php @@ -0,0 +1,17 @@ + $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(), + ) { + } +} diff --git a/module/Core/src/RedirectRule/Model/RedirectConditionType.php b/module/Core/src/RedirectRule/Model/RedirectConditionType.php new file mode 100644 index 00000000..6e6b8113 --- /dev/null +++ b/module/Core/src/RedirectRule/Model/RedirectConditionType.php @@ -0,0 +1,10 @@ +