From d3c2f4ed2a5b0fb294d80b98ed35cc8a806e58a3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 20 Jul 2016 12:37:48 +0200 Subject: [PATCH] Created service to resolve IP locations --- .../Common/src/Exception/WrongIpException.php | 10 +++++ .../Common/src/Service/IpLocationResolver.php | 42 +++++++++++++++++++ .../Service/IpLocationResolverInterface.php | 11 +++++ 3 files changed, 63 insertions(+) create mode 100644 module/Common/src/Exception/WrongIpException.php create mode 100644 module/Common/src/Service/IpLocationResolver.php create mode 100644 module/Common/src/Service/IpLocationResolverInterface.php diff --git a/module/Common/src/Exception/WrongIpException.php b/module/Common/src/Exception/WrongIpException.php new file mode 100644 index 00000000..8aa3a7bf --- /dev/null +++ b/module/Common/src/Exception/WrongIpException.php @@ -0,0 +1,10 @@ +httpClient = $httpClient; + } + + /** + * @param $ipAddress + * @return array + */ + public function resolveIpLocation($ipAddress) + { + try { + $response = $this->httpClient->get(sprintf(self::SERVICE_PATTERN, $ipAddress)); + return json_decode($response->getBody(), true); + } catch (GuzzleException $e) { + throw WrongIpException::fromIpAddress($ipAddress, $e); + } + } +} diff --git a/module/Common/src/Service/IpLocationResolverInterface.php b/module/Common/src/Service/IpLocationResolverInterface.php new file mode 100644 index 00000000..350c2b9c --- /dev/null +++ b/module/Common/src/Service/IpLocationResolverInterface.php @@ -0,0 +1,11 @@ +