Skip to content

Commit

Permalink
receive ip handler implementation and use it for ip selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Soban Mahmod committed Jul 7, 2022
1 parent 6338fbe commit a41851b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/ipinfolaravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use ipinfo\ipinfo\IPinfo as IPinfoClient;
use ipinfo\ipinfolaravel\DefaultCache;
use ipinfo\ipinfolaravel\iphandler\DefaultIPHandler;

class ipinfolaravel
{
Expand All @@ -26,6 +27,12 @@ class ipinfolaravel
*/
public $filter = null;

/**
* Provides ip.
* @var ipinfo\ipinfolaravel\iphandler\IPHandlerInterface
*/
public $ip_selector = null;

const CACHE_MAXSIZE = 4096;
const CACHE_TTL = 60 * 24;

Expand All @@ -43,7 +50,7 @@ public function handle($request, Closure $next)
$details = null;
} else {
try {
$details = $this->ipinfo->getDetails($request->ip());
$details = $this->ipinfo->getDetails($this->ip_selector->getIP($request));
} catch (\Exception $e) {
$details = null;

Expand All @@ -70,6 +77,7 @@ public function configure()
$this->access_token = config('services.ipinfo.access_token', null);
$this->filter = config('services.ipinfo.filter', [$this, 'defaultFilter']);
$this->no_except = config('services.ipinfo.no_except', false);
$this->ip_selector = config('services.ipinfo.ip_selector', new DefaultIPHandler());

if ($custom_countries = config('services.ipinfo.countries_file', null)) {
$this->settings['countries_file'] = $custom_countries;
Expand All @@ -83,12 +91,6 @@ public function configure()
$this->settings['cache'] = new DefaultCache($maxsize, $ttl);
}

if ($custom_iphandler = config('services.ipinfo.iphandler', null)) {
$this->settings['iphandler'] = $custom_iphandler;
} else {
// TODO: Handle this case, with default options
}

$this->ipinfo = new IPinfoClient($this->access_token, $this->settings);
}

Expand Down

0 comments on commit a41851b

Please sign in to comment.