mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-17 07:49:54 +03:00
Created an EmptyIpLocationResolver which always returns an empty resolution and can be used as a fallback while resolving IP addresses
This commit is contained in:
parent
1aa78f766a
commit
c7339f6cfa
7 changed files with 78 additions and 8 deletions
|
@ -37,6 +37,7 @@ return [
|
|||
|
||||
IpGeolocation\IpApiLocationResolver::class => ConfigAbstractFactory::class,
|
||||
IpGeolocation\GeoLite2LocationResolver::class => ConfigAbstractFactory::class,
|
||||
IpGeolocation\EmptyIpLocationResolver::class => InvokableFactory::class,
|
||||
IpGeolocation\ChainIpLocationResolver::class => ConfigAbstractFactory::class,
|
||||
IpGeolocation\GeoLite2\GeoLite2Options::class => ConfigAbstractFactory::class,
|
||||
IpGeolocation\GeoLite2\DbUpdater::class => ConfigAbstractFactory::class,
|
||||
|
@ -83,6 +84,7 @@ return [
|
|||
IpGeolocation\ChainIpLocationResolver::class => [
|
||||
IpGeolocation\GeoLite2LocationResolver::class,
|
||||
IpGeolocation\IpApiLocationResolver::class,
|
||||
IpGeolocation\EmptyIpLocationResolver::class,
|
||||
],
|
||||
IpGeolocation\GeoLite2\GeoLite2Options::class => ['config.geolite2'],
|
||||
IpGeolocation\GeoLite2\DbUpdater::class => [
|
||||
|
|
|
@ -18,8 +18,6 @@ class ChainIpLocationResolver implements IpLocationResolverInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $ipAddress
|
||||
* @return array
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array
|
||||
|
|
25
module/Common/src/IpGeolocation/EmptyIpLocationResolver.php
Normal file
25
module/Common/src/IpGeolocation/EmptyIpLocationResolver.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common\IpGeolocation;
|
||||
|
||||
use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
||||
|
||||
class EmptyIpLocationResolver implements IpLocationResolverInterface
|
||||
{
|
||||
/**
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array
|
||||
{
|
||||
return [
|
||||
'country_code' => '',
|
||||
'country_name' => '',
|
||||
'region_name' => '',
|
||||
'city' => '',
|
||||
'latitude' => '',
|
||||
'longitude' => '',
|
||||
'time_zone' => '',
|
||||
];
|
||||
}
|
||||
}
|
|
@ -24,8 +24,6 @@ class GeoLite2LocationResolver implements IpLocationResolverInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $ipAddress
|
||||
* @return array
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array
|
||||
|
|
|
@ -25,8 +25,6 @@ class IpApiLocationResolver implements IpLocationResolverInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $ipAddress
|
||||
* @return array
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array
|
||||
|
|
|
@ -8,8 +8,6 @@ use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
|||
interface IpLocationResolverInterface
|
||||
{
|
||||
/**
|
||||
* @param string $ipAddress
|
||||
* @return array
|
||||
* @throws WrongIpException
|
||||
*/
|
||||
public function resolveIpLocation(string $ipAddress): array;
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Common\IpGeolocation;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\EmptyIpLocationResolver;
|
||||
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
|
||||
use function Functional\map;
|
||||
use function range;
|
||||
|
||||
class EmptyIpLocationResolverTest extends TestCase
|
||||
{
|
||||
use StringUtilsTrait;
|
||||
|
||||
private const EMPTY_RESP = [
|
||||
'country_code' => '',
|
||||
'country_name' => '',
|
||||
'region_name' => '',
|
||||
'city' => '',
|
||||
'latitude' => '',
|
||||
'longitude' => '',
|
||||
'time_zone' => '',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var EmptyIpLocationResolver
|
||||
*/
|
||||
private $resolver;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->resolver = new EmptyIpLocationResolver();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideEmptyResponses
|
||||
*/
|
||||
public function alwaysReturnsAnEmptyResponse(array $expected, string $ipAddress)
|
||||
{
|
||||
$this->assertEquals($expected, $this->resolver->resolveIpLocation($ipAddress));
|
||||
}
|
||||
|
||||
public function provideEmptyResponses(): array
|
||||
{
|
||||
return map(range(0, 5), function () {
|
||||
return [self::EMPTY_RESP, $this->generateRandomString(10)];
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue