From c7339f6cfab77008203ebb68b15374a0b88d8a29 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 12 Nov 2018 20:58:14 +0100 Subject: [PATCH] Created an EmptyIpLocationResolver which always returns an empty resolution and can be used as a fallback while resolving IP addresses --- module/Common/config/dependencies.config.php | 2 + .../IpGeolocation/ChainIpLocationResolver.php | 2 - .../IpGeolocation/EmptyIpLocationResolver.php | 25 +++++++++ .../GeoLite2LocationResolver.php | 2 - .../IpGeolocation/IpApiLocationResolver.php | 2 - .../IpLocationResolverInterface.php | 2 - .../EmptyIpLocationResolverTest.php | 51 +++++++++++++++++++ 7 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 module/Common/src/IpGeolocation/EmptyIpLocationResolver.php create mode 100644 module/Common/test/IpGeolocation/EmptyIpLocationResolverTest.php diff --git a/module/Common/config/dependencies.config.php b/module/Common/config/dependencies.config.php index a43d1e5c..b03520a3 100644 --- a/module/Common/config/dependencies.config.php +++ b/module/Common/config/dependencies.config.php @@ -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 => [ diff --git a/module/Common/src/IpGeolocation/ChainIpLocationResolver.php b/module/Common/src/IpGeolocation/ChainIpLocationResolver.php index 8528c89e..2b48a70f 100644 --- a/module/Common/src/IpGeolocation/ChainIpLocationResolver.php +++ b/module/Common/src/IpGeolocation/ChainIpLocationResolver.php @@ -18,8 +18,6 @@ class ChainIpLocationResolver implements IpLocationResolverInterface } /** - * @param string $ipAddress - * @return array * @throws WrongIpException */ public function resolveIpLocation(string $ipAddress): array diff --git a/module/Common/src/IpGeolocation/EmptyIpLocationResolver.php b/module/Common/src/IpGeolocation/EmptyIpLocationResolver.php new file mode 100644 index 00000000..c9e1f4a4 --- /dev/null +++ b/module/Common/src/IpGeolocation/EmptyIpLocationResolver.php @@ -0,0 +1,25 @@ + '', + 'country_name' => '', + 'region_name' => '', + 'city' => '', + 'latitude' => '', + 'longitude' => '', + 'time_zone' => '', + ]; + } +} diff --git a/module/Common/src/IpGeolocation/GeoLite2LocationResolver.php b/module/Common/src/IpGeolocation/GeoLite2LocationResolver.php index 0bced338..24607ff5 100644 --- a/module/Common/src/IpGeolocation/GeoLite2LocationResolver.php +++ b/module/Common/src/IpGeolocation/GeoLite2LocationResolver.php @@ -24,8 +24,6 @@ class GeoLite2LocationResolver implements IpLocationResolverInterface } /** - * @param string $ipAddress - * @return array * @throws WrongIpException */ public function resolveIpLocation(string $ipAddress): array diff --git a/module/Common/src/IpGeolocation/IpApiLocationResolver.php b/module/Common/src/IpGeolocation/IpApiLocationResolver.php index fe6680a8..8ea8be4b 100644 --- a/module/Common/src/IpGeolocation/IpApiLocationResolver.php +++ b/module/Common/src/IpGeolocation/IpApiLocationResolver.php @@ -25,8 +25,6 @@ class IpApiLocationResolver implements IpLocationResolverInterface } /** - * @param string $ipAddress - * @return array * @throws WrongIpException */ public function resolveIpLocation(string $ipAddress): array diff --git a/module/Common/src/IpGeolocation/IpLocationResolverInterface.php b/module/Common/src/IpGeolocation/IpLocationResolverInterface.php index f9e41572..6017db61 100644 --- a/module/Common/src/IpGeolocation/IpLocationResolverInterface.php +++ b/module/Common/src/IpGeolocation/IpLocationResolverInterface.php @@ -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; diff --git a/module/Common/test/IpGeolocation/EmptyIpLocationResolverTest.php b/module/Common/test/IpGeolocation/EmptyIpLocationResolverTest.php new file mode 100644 index 00000000..c394f85e --- /dev/null +++ b/module/Common/test/IpGeolocation/EmptyIpLocationResolverTest.php @@ -0,0 +1,51 @@ + '', + '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)]; + }); + } +}