-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor services to eliminate code duplication (#27)
* chore: refactor services to eliminate code duplication * Apply fixes from StyleCI * remove dupe line --------- Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
77766fb
commit 46e64e6
Showing
9 changed files
with
217 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?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; | ||
|
||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use FoF\GeoIP\Api\ServiceInterface; | ||
use FoF\GeoIP\Api\ServiceResponse; | ||
use GuzzleHttp\Client; | ||
use Psr\Log\LoggerInterface; | ||
|
||
abstract class BaseGeoService implements ServiceInterface | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
protected $client; | ||
|
||
protected $host; | ||
protected $settingPrefix; | ||
|
||
public function __construct(protected SettingsRepositoryInterface $settings, protected LoggerInterface $logger) | ||
{ | ||
$this->client = new Client([ | ||
'base_uri' => $this->host, | ||
'verify' => false, | ||
]); | ||
} | ||
|
||
public function get(string $ip): ?ServiceResponse | ||
{ | ||
$apiKey = $this->settings->get("{$this->settingPrefix}.access_key"); | ||
|
||
if ($this->requiresApiKey() && !$apiKey) { | ||
$this->logger->error("No API key found for {$this->host}"); | ||
|
||
return null; | ||
} | ||
|
||
$res = $this->client->get($this->buildUrl($ip, $apiKey), $this->getRequestOptions($apiKey)); | ||
|
||
$body = json_decode($res->getBody()); | ||
|
||
if ($this->hasError($body)) { | ||
$this->logger->error("Error detected in response from {$this->host}"); | ||
|
||
return $this->handleError($body); | ||
} | ||
|
||
$data = $this->parseResponse($body); | ||
|
||
return $data; | ||
} | ||
|
||
protected function requiresApiKey(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
abstract protected function buildUrl(string $ip, ?string $apiKey): string; | ||
|
||
abstract protected function getRequestOptions(?string $apiKey): array; | ||
|
||
abstract protected function hasError(object $body): bool; | ||
|
||
abstract protected function handleError(object $body): ?ServiceResponse; | ||
|
||
abstract protected function parseResponse(object $body): ServiceResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.