src/Services/IPGeolocator.php line 52
- <?php
- namespace App\Services;
- use App\Utils\Util;
- use Psr\Log\LoggerInterface;
- class IPGeolocator
- {
- /**
- * @var Client $client
- */
- protected $client;
- /**
- * @var string Ip used at dev and debug env
- */
- protected $fallBackIp;
- /**
- * @var LoggerInterface
- */
- protected $logger;
- /**
- * IPGeolocator constructor.
- * @param string $fallbackIp , Placeholder ip when using a local env
- * @param LoggerInterface $logger
- */
- function __construct($fallbackIp, LoggerInterface $logger)
- {
- $this->fallBackIp = $fallbackIp;
- $this->logger = $logger;
- }
- /**
- * Returns the response from the
- * geolocation service
- * @link http://ip-api.com/json/62.43.192.62
- * @param $ip
- * @return array|null
- */
- public function geoLocate($ip)
- {
- try {
- if ($ip === '127.0.0.1') {
- $ip = $this->fallBackIp;
- }
- $geo = Util::getCountryByIp($ip, true);
- /*$response = $this->client->get($ip);
- $stream = $response->getBody()->getContents();*/
- /** Csa Guzzle is injecting some weird characters into the response */
- // $test = preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $stream);
- $datos = json_decode(json_encode($geo), true);
- return $datos;
- } catch (\Exception $e) {
- $this->logger->error("An error [{$e->getMessage()}] [{$e->getTraceAsString()}] ocurred while trying to geolocate the ip: {$ip}.", ['GEOLOCATE']);
- }
- return null;
- }
- }