Support dynamic redirects based on an after-date condition

This commit is contained in:
Alejandro Celaya
2025-12-18 09:41:07 +01:00
parent 39585ed87d
commit 9ae2dce261
7 changed files with 42 additions and 4 deletions

View File

@@ -127,6 +127,9 @@ class RedirectRuleHandler implements RedirectRuleHandlerInterface
RedirectConditionType::BEFORE_DATE => RedirectCondition::forBeforeDate(
normalizeDate($this->askMandatory('Date to match?', $io)),
),
RedirectConditionType::AFTER_DATE => RedirectCondition::forAfterDate(
normalizeDate($this->askMandatory('Date to match?', $io)),
),
};
$continue = $io->confirm('Do you want to add another condition?');

View File

@@ -192,6 +192,10 @@ class RedirectRuleHandlerTest extends TestCase
RedirectConditionType::BEFORE_DATE,
[RedirectCondition::forBeforeDate(normalizeDate('2016-05-01T20:34:16+02:00'))],
];
yield 'After date' => [
RedirectConditionType::AFTER_DATE,
[RedirectCondition::forAfterDate(normalizeDate('2016-05-01T20:34:16+02:00'))],
];
}
#[Test]

View File

@@ -82,6 +82,11 @@ class RedirectCondition extends AbstractEntity implements JsonSerializable
return new self(RedirectConditionType::BEFORE_DATE, $date->toAtomString());
}
public static function forAfterDate(Chronos $date): self
{
return new self(RedirectConditionType::AFTER_DATE, $date->toAtomString());
}
public static function fromRawData(array $rawData): self
{
$type = RedirectConditionType::from($rawData[RedirectRulesInputFilter::CONDITION_TYPE]);
@@ -108,6 +113,7 @@ class RedirectCondition extends AbstractEntity implements JsonSerializable
RedirectConditionType::GEOLOCATION_COUNTRY_CODE => self::forGeolocationCountryCode($cond->matchValue),
RedirectConditionType::GEOLOCATION_CITY_NAME => self::forGeolocationCityName($cond->matchValue),
RedirectConditionType::BEFORE_DATE => self::forBeforeDate(normalizeDate($cond->matchValue)),
RedirectConditionType::AFTER_DATE => self::forAfterDate(normalizeDate($cond->matchValue)),
};
}
@@ -126,6 +132,7 @@ class RedirectCondition extends AbstractEntity implements JsonSerializable
RedirectConditionType::GEOLOCATION_COUNTRY_CODE => $this->matchesGeolocationCountryCode($request),
RedirectConditionType::GEOLOCATION_CITY_NAME => $this->matchesGeolocationCityName($request),
RedirectConditionType::BEFORE_DATE => $this->matchesBeforeDate(),
RedirectConditionType::AFTER_DATE => $this->matchesAfterDate(),
};
}
@@ -214,6 +221,11 @@ class RedirectCondition extends AbstractEntity implements JsonSerializable
return Chronos::now()->lessThan(Chronos::parse($this->matchValue));
}
private function matchesAfterDate(): bool
{
return Chronos::now()->greaterThan(Chronos::parse($this->matchValue));
}
public function jsonSerialize(): array
{
return [
@@ -244,7 +256,8 @@ class RedirectCondition extends AbstractEntity implements JsonSerializable
RedirectConditionType::IP_ADDRESS => sprintf('IP address matches %s', $this->matchValue),
RedirectConditionType::GEOLOCATION_COUNTRY_CODE => sprintf('country code is %s', $this->matchValue),
RedirectConditionType::GEOLOCATION_CITY_NAME => sprintf('city name is %s', $this->matchValue),
RedirectConditionType::BEFORE_DATE => sprintf('date before %s', $this->matchValue),
RedirectConditionType::BEFORE_DATE => sprintf('date is before %s', $this->matchValue),
RedirectConditionType::AFTER_DATE => sprintf('date is after %s', $this->matchValue),
};
}
}

View File

@@ -21,6 +21,7 @@ enum RedirectConditionType: string
case GEOLOCATION_COUNTRY_CODE = 'geolocation-country-code';
case GEOLOCATION_CITY_NAME = 'geolocation-city-name';
case BEFORE_DATE = 'before-date';
case AFTER_DATE = 'after-date';
/**
* Tells if a value is valid for the condition type

View File

@@ -211,4 +211,19 @@ class RedirectConditionTest extends TestCase
yield 'date later than current' => [Chronos::now()->addHours(1), true];
yield 'date earlier than current' => [Chronos::now()->subHours(1), false];
}
#[Test, DataProvider('provideVisitsWithAfterDateCondition')]
public function matchesAfterDate(Chronos $date, bool $expectedResult): void
{
$request = ServerRequestFactory::fromGlobals();
$result = RedirectCondition::forAfterDate($date)->matchesRequest($request);
self::assertEquals($expectedResult, $result);
}
public static function provideVisitsWithAfterDateCondition(): iterable
{
yield 'date later than current' => [Chronos::now()->addHours(1), false];
yield 'date earlier than current' => [Chronos::now()->subHours(1), true];
}
}