Extract logic to match every type of redirect condition to its own private method

This commit is contained in:
Alejandro Celaya 2024-02-25 17:13:54 +01:00
parent 4e87affb0b
commit 202d0b86b3

View File

@ -25,22 +25,38 @@ class RedirectCondition extends AbstractEntity
*/ */
public function matchesRequest(ServerRequestInterface $request): bool public function matchesRequest(ServerRequestInterface $request): bool
{ {
if ($this->type === RedirectConditionType::QUERY_PARAM && $this->matchKey !== null) { return match ($this->type) {
$query = $request->getQueryParams(); RedirectConditionType::QUERY_PARAM => $this->matchesQueryParam($request),
$queryValue = $query[$this->matchKey] ?? null; RedirectConditionType::LANGUAGE => $this->matchesLanguage($request),
return $queryValue === $this->matchValue; default => false,
};
}
public function matchesQueryParam(ServerRequestInterface $request): bool
{
if ($this->matchKey !== null) {
return false;
} }
if ($this->type === RedirectConditionType::LANGUAGE && $request->hasHeader('Accept-Language')) { $query = $request->getQueryParams();
$acceptedLanguages = explode(',', $request->getHeaderLine('Accept-Language')); $queryValue = $query[$this->matchKey] ?? null;
$normalizedLanguage = normalizeLocale($this->matchValue);
return some( return $queryValue === $this->matchValue;
$acceptedLanguages, }
static fn (string $lang) => normalizeLocale($lang) === $normalizedLanguage,
); public function matchesLanguage(ServerRequestInterface $request): bool
{
$acceptLanguage = $request->getHeaderLine('Accept-Language');
if ($acceptLanguage === '' || $acceptLanguage === '*') {
return false;
} }
return false; $acceptedLanguages = explode(',', $acceptLanguage);
$normalizedLanguage = normalizeLocale($this->matchValue);
return some(
$acceptedLanguages,
static fn (string $lang) => normalizeLocale($lang) === $normalizedLanguage,
);
} }
} }