From f147e4e6e6a4114efd5d033afaaa09dc9eca199b Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Fri, 10 Nov 2023 10:35:31 +0000 Subject: [PATCH] centralize ip retrieval --- src/Api/GeoIP.php | 14 ++---------- src/Jobs/RetrieveIP.php | 34 +++++----------------------- src/Repositories/GeoIPRepository.php | 6 ++--- 3 files changed, 11 insertions(+), 43 deletions(-) diff --git a/src/Api/GeoIP.php b/src/Api/GeoIP.php index 6aa867b..1ab47f7 100644 --- a/src/Api/GeoIP.php +++ b/src/Api/GeoIP.php @@ -65,25 +65,15 @@ public function get(string $ip) /** * @param array $ips * - * @return ServiceResponse[]|void + * @return ServiceResponse[] */ public function getBatch(array $ips) { return $this->getService()->getBatch($ips); } - public function getSaved(string $ip) + public function getSaved(string $ip): ?IPInfo { - $response = $this->checkErrors(); - - if ($response) { - $ipInfo = new IPInfo(); - $ipInfo->address = $ip; - $ipInfo->fill($response->toJSON()); - - return $ipInfo; - } - return IPInfo::where('address', $ip)->first(); } diff --git a/src/Jobs/RetrieveIP.php b/src/Jobs/RetrieveIP.php index 58028ce..e3834b7 100644 --- a/src/Jobs/RetrieveIP.php +++ b/src/Jobs/RetrieveIP.php @@ -12,8 +12,9 @@ namespace FoF\GeoIP\Jobs; use Flarum\Queue\AbstractJob; -use FoF\GeoIP\Api\GeoIP; +use FoF\GeoIP\Command\FetchIPInfo; use FoF\GeoIP\Model\IPInfo; +use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Contracts\Cache\Repository; use Illuminate\Support\Arr; @@ -29,7 +30,7 @@ public function __construct(protected string $ip) self::$queued[] = $ip; } - public function handle(GeoIP $geoIP, Repository $cache): void + public function handle(Repository $cache, Dispatcher $bus): void { $ip = $this->ip; $cacheKey = "fof-geoip.retrieving.$ip"; @@ -44,33 +45,10 @@ public function handle(GeoIP $geoIP, Repository $cache): void static::$retrieving[] = $ip; $cache->add($cacheKey, true, 60 * 60); - $response = $geoIP->get($ip); + /** @var IPInfo $ipInfo */ + $ipInfo = $bus->dispatch(new FetchIPInfo($ip)); - if ($response) { - $ipInfo = IPInfo::where('address', $ip)->first(); - - if (!$ipInfo) { - $ipInfo = new IPInfo(); - $ipInfo->address = $ip; - $ipInfo->fill($response->toJson()); - - // If response is fake, it means an error occurred that was logged to the admin dashboard. - // We don't want to save fake responses. - if (!$response->fake) { - $ipInfo->save(); - } - } - - if (!$response->fake) { - // If using sync queue driver, this will be immediately available. - // If using another driver (eg. redis), it will remember this IP has been retrieved until the process ends. - static::$retrieved[$ip] = $ipInfo; - } - } - - // Only remove from cache if we didn't get a fake response - if (!$response || !$response->fake) { - // Remove from retrieving list and cache + if ($ipInfo->exists) { static::$retrieving = array_diff(static::$retrieving, [$ip]); $cache->forget("fof-geoip.retrieving.$ip"); } diff --git a/src/Repositories/GeoIPRepository.php b/src/Repositories/GeoIPRepository.php index 55ea085..feaad67 100644 --- a/src/Repositories/GeoIPRepository.php +++ b/src/Repositories/GeoIPRepository.php @@ -27,12 +27,12 @@ public function __construct(protected GeoIP $geoIP, protected Queue $queue) /** * @param string|null $ip * - * @return IPInfo|void + * @return IPInfo|null */ - public function get($ip) + public function get(?string $ip): ?IPInfo { if (!$ip) { - return; + return null; } return $this->geoIP->getSaved($ip);