src/Services/IPGeolocator.php line 52

  1. <?php
  2. namespace App\Services;
  3. use App\Utils\Util;
  4. use Psr\Log\LoggerInterface;
  5. class IPGeolocator
  6. {
  7.     /**
  8.      * @var Client $client
  9.      */
  10.     protected $client;
  11.     /**
  12.      * @var string Ip used at dev and debug env
  13.      */
  14.     protected $fallBackIp;
  15.     /**
  16.      * @var LoggerInterface
  17.      */
  18.     protected $logger;
  19.     /**
  20.      * IPGeolocator constructor.
  21.      * @param string $fallbackIp , Placeholder ip when using a local env
  22.      * @param LoggerInterface $logger
  23.      */
  24.     function __construct($fallbackIpLoggerInterface $logger)
  25.     {
  26.         $this->fallBackIp $fallbackIp;
  27.         $this->logger $logger;
  28.     }
  29.     /**
  30.      * Returns the response from the
  31.      * geolocation service
  32.      * @link http://ip-api.com/json/62.43.192.62
  33.      * @param $ip
  34.      * @return array|null
  35.      */
  36.     public function geoLocate($ip)
  37.     {
  38.         try {
  39.             if ($ip === '127.0.0.1') {
  40.                 $ip $this->fallBackIp;
  41.             }
  42.             $geo Util::getCountryByIp($iptrue);
  43.             /*$response = $this->client->get($ip);
  44.             $stream = $response->getBody()->getContents();*/
  45.             /** Csa Guzzle is injecting some weird characters into the response */
  46. //             $test = preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $stream);
  47.             $datos json_decode(json_encode($geo), true);
  48.             return $datos;
  49.         } catch (\Exception $e) {
  50.             $this->logger->error("An error [{$e->getMessage()}] [{$e->getTraceAsString()}] ocurred while trying to geolocate the ip: {$ip}.", ['GEOLOCATE']);
  51.         }
  52.         return null;
  53.     }
  54. }