<?php

namespace App\Helpers;

use Exception;
use GeoIp2\Database\Reader;

class IpDetector
{

    public static function getCountryCodeFromClientIp()
    {
        $ip = request()->ip();
        return self::getCountryCodeFromIp($ip);
    }

    public static function getCountryCodeFromIp($ip)
    {
        $reader = new Reader(database_path('data/GeoLite2-Country.mmdb'));

        try {
            return strtolower($reader->country($ip)->country->isoCode);
        } catch (Exception $e) {
            return null;
        }
    }
}
