Simplify logic in RedirectRule when checking geolocation conditions

This commit is contained in:
Alejandro Celaya 2024-11-15 09:00:59 +01:00
parent b5ff568651
commit 6aaea2ac26
3 changed files with 14 additions and 34 deletions

View File

@ -293,5 +293,10 @@ function ipAddressFromRequest(ServerRequestInterface $request): string|null
function geolocationFromRequest(ServerRequestInterface $request): Location|null function geolocationFromRequest(ServerRequestInterface $request): Location|null
{ {
return $request->getAttribute(Location::class); $geolocation = $request->getAttribute(Location::class);
if ($geolocation !== null && ! $geolocation instanceof Location) {
// TODO Throw exception
}
return $geolocation;
} }

View File

@ -9,11 +9,10 @@ use Shlinkio\Shlink\Core\Model\DeviceType;
use Shlinkio\Shlink\Core\RedirectRule\Model\RedirectConditionType; use Shlinkio\Shlink\Core\RedirectRule\Model\RedirectConditionType;
use Shlinkio\Shlink\Core\RedirectRule\Model\Validation\RedirectRulesInputFilter; use Shlinkio\Shlink\Core\RedirectRule\Model\Validation\RedirectRulesInputFilter;
use Shlinkio\Shlink\Core\Util\IpAddressUtils; use Shlinkio\Shlink\Core\Util\IpAddressUtils;
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
use function Shlinkio\Shlink\Core\acceptLanguageToLocales; use function Shlinkio\Shlink\Core\acceptLanguageToLocales;
use function Shlinkio\Shlink\Core\ArrayUtils\some; use function Shlinkio\Shlink\Core\ArrayUtils\some;
use function Shlinkio\Shlink\Core\geolocationFromRequest;
use function Shlinkio\Shlink\Core\ipAddressFromRequest; use function Shlinkio\Shlink\Core\ipAddressFromRequest;
use function Shlinkio\Shlink\Core\normalizeLocale; use function Shlinkio\Shlink\Core\normalizeLocale;
use function Shlinkio\Shlink\Core\splitLocale; use function Shlinkio\Shlink\Core\splitLocale;
@ -134,9 +133,8 @@ class RedirectCondition extends AbstractEntity implements JsonSerializable
private function matchesGeolocationCountryCode(ServerRequestInterface $request): bool private function matchesGeolocationCountryCode(ServerRequestInterface $request): bool
{ {
$geolocation = $request->getAttribute(Location::class); $geolocation = geolocationFromRequest($request);
// TODO We should eventually rely on `Location` type only if ($geolocation === null) {
if (! ($geolocation instanceof Location) && ! ($geolocation instanceof VisitLocation)) {
return false; return false;
} }
@ -145,14 +143,12 @@ class RedirectCondition extends AbstractEntity implements JsonSerializable
private function matchesGeolocationCityName(ServerRequestInterface $request): bool private function matchesGeolocationCityName(ServerRequestInterface $request): bool
{ {
$geolocation = $request->getAttribute(Location::class); $geolocation = geolocationFromRequest($request);
// TODO We should eventually rely on `Location` type only if ($geolocation === null) {
if (! ($geolocation instanceof Location) && ! ($geolocation instanceof VisitLocation)) {
return false; return false;
} }
$cityName = $geolocation instanceof Location ? $geolocation->city : $geolocation->cityName; return strcasecmp($geolocation->city, $this->matchValue) === 0;
return strcasecmp($cityName, $this->matchValue) === 0;
} }
public function jsonSerialize(): array public function jsonSerialize(): array

View File

@ -10,7 +10,6 @@ use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory; use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
use Shlinkio\Shlink\Core\Model\DeviceType; use Shlinkio\Shlink\Core\Model\DeviceType;
use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition; use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition;
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
use Shlinkio\Shlink\IpGeolocation\Model\Location; use Shlinkio\Shlink\IpGeolocation\Model\Location;
use const ShlinkioTest\Shlink\ANDROID_USER_AGENT; use const ShlinkioTest\Shlink\ANDROID_USER_AGENT;
@ -99,7 +98,7 @@ class RedirectConditionTest extends TestCase
#[Test, DataProvider('provideVisitsWithCountry')] #[Test, DataProvider('provideVisitsWithCountry')]
public function matchesGeolocationCountryCode( public function matchesGeolocationCountryCode(
Location|VisitLocation|null $location, Location|null $location,
string $countryCodeToMatch, string $countryCodeToMatch,
bool $expected, bool $expected,
): void { ): void {
@ -114,21 +113,11 @@ class RedirectConditionTest extends TestCase
yield 'non-matching location' => [new Location(countryCode: 'ES'), 'US', false]; yield 'non-matching location' => [new Location(countryCode: 'ES'), 'US', false];
yield 'matching location' => [new Location(countryCode: 'US'), 'US', true]; yield 'matching location' => [new Location(countryCode: 'US'), 'US', true];
yield 'matching case-insensitive' => [new Location(countryCode: 'US'), 'us', true]; yield 'matching case-insensitive' => [new Location(countryCode: 'US'), 'us', true];
yield 'matching visit location' => [
VisitLocation::fromGeolocation(new Location(countryCode: 'US')),
'US',
true,
];
yield 'matching visit case-insensitive' => [
VisitLocation::fromGeolocation(new Location(countryCode: 'es')),
'ES',
true,
];
} }
#[Test, DataProvider('provideVisitsWithCity')] #[Test, DataProvider('provideVisitsWithCity')]
public function matchesGeolocationCityName( public function matchesGeolocationCityName(
Location|VisitLocation|null $location, Location|null $location,
string $cityNameToMatch, string $cityNameToMatch,
bool $expected, bool $expected,
): void { ): void {
@ -143,15 +132,5 @@ class RedirectConditionTest extends TestCase
yield 'non-matching location' => [new Location(city: 'Los Angeles'), 'New York', false]; yield 'non-matching location' => [new Location(city: 'Los Angeles'), 'New York', false];
yield 'matching location' => [new Location(city: 'Madrid'), 'Madrid', true]; yield 'matching location' => [new Location(city: 'Madrid'), 'Madrid', true];
yield 'matching case-insensitive' => [new Location(city: 'Los Angeles'), 'los angeles', true]; yield 'matching case-insensitive' => [new Location(city: 'Los Angeles'), 'los angeles', true];
yield 'matching visit location' => [
VisitLocation::fromGeolocation(new Location(city: 'New York')),
'New York',
true,
];
yield 'matching visit case-insensitive' => [
VisitLocation::fromGeolocation(new Location(city: 'barcelona')),
'Barcelona',
true,
];
} }
} }