Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Nov 10, 2023
1 parent 5326306 commit e169f30
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/Api/Services/BaseGeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function get(string $ip): ?ServiceResponse
/** @phpstan-ignore-next-line */
if (!$this->isRateLimited() || ($this->isRateLimited() && $this->singleLookupsRemaining > 0)) {
$response = $this->client->get($this->buildUrl($ip, $apiKey), $this->getRequestOptions($apiKey));

if ($this->isRateLimited()) {
$this->updateRateLimitsFromResponse($response);
}
Expand All @@ -74,6 +74,7 @@ public function get(string $ip): ?ServiceResponse
return $this->parseResponse($body);
} else {
$this->storeForLaterLookup($ip);

return null;
}
}
Expand Down Expand Up @@ -105,7 +106,7 @@ public function getBatch(array $ips)
/** @phpstan-ignore-next-line */
if (!$this->isRateLimited() || ($this->isRateLimited() && $this->batchLookupsRemaining > 0)) {
$response = $this->client->request('POST', $this->buildBatchUrl($ips, $apiKey), $this->getRequestOptions($apiKey, $ips));

if ($this->isRateLimited()) {
$this->updateRateLimitsFromResponse($response, 'batch');
}
Expand All @@ -129,7 +130,7 @@ protected function initRateLimitsFromCache(): void
$this->batchLookupsRemaining = $this->cache->get("{$this->settingPrefix}.batch", $this->batchLookupsRemaining);
}

abstract function isRateLimited(): bool;
abstract public function isRateLimited(): bool;

abstract protected function updateRateLimitsFromResponse(ResponseInterface $response, string $requestType = 'single'): void;

Expand Down
14 changes: 7 additions & 7 deletions src/Api/Services/IPApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,34 @@ class IPApi extends BaseGeoService

/**
* 45 requests per minute.
*
*
* @see https://ip-api.com/docs/api:json
*
* @var integer
* @var int
*/
protected int $singleLookupsRemaining = 45;

/**
* 15 requests per minute.
*
*
* @see https://ip-api.com/docs/api:batch
*
* @var integer
* @var int
*/
protected int $batchLookupsRemaining = 15;

protected function updateRateLimitsFromResponse(ResponseInterface $response, string $requestType = 'single'): void
{
/**
/**
* The number of requests remaining in the current time window.
*
*
* @var int
*/
$remaining = (int) $response->getHeaderLine('X-Rl');

/**
* The number of seconds until the current time window resets.
*
*
* @var int
*/
$ttl = Carbon::now()->addSeconds((int) $response->getHeaderLine('X-Ttl'));
Expand Down
9 changes: 9 additions & 0 deletions src/Api/Services/IPApiPro.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/geoip.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\GeoIP\Api\Services;

class IPApiPro extends IPApi
Expand Down
8 changes: 4 additions & 4 deletions src/Api/Services/IPData.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ class IPData extends BaseGeoService

/**
* 1500 lookups per day, on the free plan.
*
*
* @see https://ipdata.co/pricing.html
*
* @var integer
* @var int
*/
protected int $singleLookupsRemaining = 1500;

protected function updateRateLimitsFromResponse(ResponseInterface $response, string $requestType = 'single'): void
{
/**
/**
* The number of requests remaining in the current time window.
*
*
* @var int
*/
$remaining = 1500 - (int) Arr::get(json_decode($response->getBody(), true), 'count', $this->cache->get("$this->settingPrefix.$requestType"));
Expand Down

0 comments on commit e169f30

Please sign in to comment.