Skip to content

Commit

Permalink
chore: something
Browse files Browse the repository at this point in the history
  • Loading branch information
Burial0268 committed Jul 30, 2024
1 parent 4a2b6ec commit 76c5d99
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion js/src/forum/ProcessData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type Data = {
} & Record<string, NestedStringArray>

export default class ProcessData {
private data: Data
private readonly data: Data

constructor(ipInfo: ipinfo) {
this.data = {
Expand Down
16 changes: 10 additions & 6 deletions src/Api/GeoIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace GBCLStudio\GeoIp\Api;

use Flarum\Settings\SettingsRepositoryInterface;
use GBCLStudio\GeoIp\Api\Service\BaseService;
use GBCLStudio\GeoIp\ServiceResponse;

class GeoIp
Expand All @@ -19,7 +20,6 @@ class GeoIp
* @var SettingsRepositoryInterface
*/
private SettingsRepositoryInterface $settings;
private mixed $serviceSelected;

public function __construct(SettingsRepositoryInterface $settings)
{
Expand All @@ -33,21 +33,25 @@ public function __construct(SettingsRepositoryInterface $settings)
*/
public function get(string $ip)
{
$this->serviceSelected = $this->settings->get('gbcl-userip.service');
$serviceSelected = $this->settings->get('gbcl-userip.service');
$name = $this->settings->get('gbcl-userip.service');
$services = resolve('container')->tagged('gbcl-userip.services');

foreach ($services as $service) {
if (!$service instanceof BaseService) {
throw new \InvalidArgumentException(sprintf("%d should extends class %s", (string)$service, BaseService::class));
}

if ($service->name() === $name) {
$this->serviceSelected = $service;
continue;
$serviceSelected = $service;
break;
}
}

if (! $this->serviceSelected) {
if (!$serviceSelected) {
return;
}

return $this->serviceSelected->get($ip);
return $serviceSelected->get($ip);
}
}
32 changes: 32 additions & 0 deletions src/Api/Service/BaseService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of GBCLStudio Project.
*
* Copyright (c) 2023 GBCLStudio PHP Project Team.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace GBCLStudio\GeoIp\Api\Service;

use Flarum\Settings\SettingsRepositoryInterface;
use GuzzleHttp\Client;

abstract class BaseService
{
protected Client $client;

public function __construct(protected SettingsRepositoryInterface $settings)
{
$this->client = new Client([
'base_uri' => $this->setBaseRequestUrl()
]);
}

/** Returns the base request url (including HTTP scheme)
*
* @return string
*/
abstract public function setBaseRequestUrl(): string;
}
16 changes: 5 additions & 11 deletions src/Api/Service/IpInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@

namespace GBCLStudio\GeoIp\Api\Service;

use Flarum\Settings\SettingsRepositoryInterface;
use GBCLStudio\GeoIp\Api\GeoIpInterface;
use GBCLStudio\GeoIp\ServiceResponse;
use GuzzleHttp\Client;

class IpInfo implements GeoIpInterface
class IpInfo extends BaseService implements GeoIpInterface
{

private Client $client;

public function __construct(protected SettingsRepositoryInterface $settings)
public function setBaseRequestUrl(): string
{
$this->client = new Client([
'base_uri' => 'https://ipinfo.io/'
]);
return 'https://ipinfo.io';
}

/**
/**
* @inheritDoc
*/
public function name(): string
Expand All @@ -52,7 +46,7 @@ public function get(string $ip): ServiceResponse
return (new ServiceResponse())
->setCountryCode($body->country)
->setRegion($body->region)
->setIsp($ispASN)
->setIsp($ispASN[0])
->setAddress($ip);
}
}
11 changes: 3 additions & 8 deletions src/Api/Service/IpSb.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@

use GBCLStudio\GeoIp\Api\GeoIpInterface;
use GBCLStudio\GeoIp\ServiceResponse;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class IpSb implements GeoIpInterface
class IpSb extends BaseService implements GeoIpInterface
{
private Client $client;

public function __construct()
public function setBaseRequestUrl(): string
{
$this->client = new Client([
'base_uri' => 'https://api.ip.sb/',
]);
return 'https://api.ip.sb';
}

public function name(): string
Expand Down

0 comments on commit 76c5d99

Please sign in to comment.