mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-22 08:56:42 -06:00
Extract logic to match every type of redirect condition to its own private method
This commit is contained in:
parent
4e87affb0b
commit
202d0b86b3
@ -25,22 +25,38 @@ class RedirectCondition extends AbstractEntity
|
||||
*/
|
||||
public function matchesRequest(ServerRequestInterface $request): bool
|
||||
{
|
||||
if ($this->type === RedirectConditionType::QUERY_PARAM && $this->matchKey !== null) {
|
||||
$query = $request->getQueryParams();
|
||||
$queryValue = $query[$this->matchKey] ?? null;
|
||||
return $queryValue === $this->matchValue;
|
||||
return match ($this->type) {
|
||||
RedirectConditionType::QUERY_PARAM => $this->matchesQueryParam($request),
|
||||
RedirectConditionType::LANGUAGE => $this->matchesLanguage($request),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
public function matchesQueryParam(ServerRequestInterface $request): bool
|
||||
{
|
||||
if ($this->matchKey !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->type === RedirectConditionType::LANGUAGE && $request->hasHeader('Accept-Language')) {
|
||||
$acceptedLanguages = explode(',', $request->getHeaderLine('Accept-Language'));
|
||||
$normalizedLanguage = normalizeLocale($this->matchValue);
|
||||
$query = $request->getQueryParams();
|
||||
$queryValue = $query[$this->matchKey] ?? null;
|
||||
|
||||
return some(
|
||||
$acceptedLanguages,
|
||||
static fn (string $lang) => normalizeLocale($lang) === $normalizedLanguage,
|
||||
);
|
||||
return $queryValue === $this->matchValue;
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user