Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/ipinfolaravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,28 @@ class ipinfolaravel

/**
* Return true to skip IPinfo lookup, otherwise return false.
* @var function
* @var callable|null
*/
public $filter = null;

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

/**
* IPinfo client object.
* @var IPinfoClient
*/
public $ipinfo = null;

/**
* Boolean flag to handle exceptions.
* @var bool
*/
public $no_except = false;

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

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

// users can't catch this exception with their own wrapper
// middleware unfortunately, so we catch it for them. but for
// backwards-compatibility, we throw the exception again unless
// they've told us not to.
if ($this->no_except != true) {
if (!$this->no_except) {
throw $e;
}
}
Expand Down
6 changes: 4 additions & 2 deletions tests/IpinfolaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ public function test_handle_skips_lookup_when_filter_returns_true()

public function test_handle_throws_if_client_throws_and_no_except_false()
{
$this->expectException(\Exception::class);
$expected = new \RuntimeException('Boom! That went wrong.');

$this->expectExceptionObject($expected);

$client = $this->createMock(IPinfoClient::class);
$client
->method("getDetails")
->willThrowException(new \Exception("fail"));
->willThrowException($expected);

$selector = $this->createMock(IPHandlerInterface::class);
$selector->method("getIP")->willReturn("1.2.3.4");
Expand Down