From d28b97dfa7e76c375885583145570f750546b5c3 Mon Sep 17 00:00:00 2001 From: Arthur Monney Date: Sat, 25 May 2024 09:45:19 +0200 Subject: [PATCH] Update session check on middleware --- .github/workflows/tests.yml | 2 +- src/DataObject/Connection.php | 10 +++++----- src/Middleware/AbstractIpGeolocation.php | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 545dd50..0f71d52 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,4 +1,4 @@ -name: Tests +name: tests on: push diff --git a/src/DataObject/Connection.php b/src/DataObject/Connection.php index 7431c23..6231b21 100644 --- a/src/DataObject/Connection.php +++ b/src/DataObject/Connection.php @@ -7,11 +7,11 @@ final class Connection { public function __construct( - public string $connectionType, - public int $autonomousSystemNumber, - public string $autonomousSystemOrganization, - public string $ispName, - public string $organizationName, + public ?string $connectionType, + public ?int $autonomousSystemNumber, + public ?string $autonomousSystemOrganization, + public ?string $ispName, + public ?string $organizationName, ) { } } diff --git a/src/Middleware/AbstractIpGeolocation.php b/src/Middleware/AbstractIpGeolocation.php index 9cc70b2..eb05386 100644 --- a/src/Middleware/AbstractIpGeolocation.php +++ b/src/Middleware/AbstractIpGeolocation.php @@ -24,8 +24,25 @@ public function handle($request, Closure $next): mixed (new IpGeolocation($request))->initialize() ); - session()->put('abstract-ip-geolocation', $response); + $this->manageSession($response); return $next($request); } + + public function manageSession(GeolocationData $geolocation): void + { + $sessionKey = 'abstract-ip-geolocation'; + + if (session()->exists($sessionKey)) { + /** @var GeolocationData $currentValue */ + $currentValue = session()->get($sessionKey); + + if ($currentValue->ipAddress !== $geolocation->ipAddress) { + session()->forget($sessionKey); + session()->put($sessionKey, $geolocation); + } + } else { + session()->put($sessionKey, $geolocation); + } + } }