Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Console/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace InteractionDesignFoundation\GeoIP\Console;

use Illuminate\Console\Command;
use InteractionDesignFoundation\GeoIP\Exceptions\MissingConfigurationException;

class Update extends Command
{
Expand Down Expand Up @@ -41,7 +42,13 @@ public function handle()
public function fire()
{
// Get default service
$service = app('geoip')->getService();
try {
$service = app('geoip')->getService();
} catch(MissingConfigurationException $e) {
$this->components->error($e->getMessage()) ;

return static::FAILURE ;
}

// Ensure the selected service supports updating
if (method_exists($service, 'update') === false) {
Expand Down
11 changes: 11 additions & 0 deletions src/Exceptions/MissingConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace InteractionDesignFoundation\GeoIP\Exceptions;

use RuntimeException;

class MissingConfigurationException extends RuntimeException {

}
25 changes: 25 additions & 0 deletions src/Services/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InteractionDesignFoundation\GeoIP\Location;
use Illuminate\Support\Arr;
use InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface;
use InteractionDesignFoundation\GeoIP\Exceptions\MissingConfigurationException;

abstract class AbstractService implements ServiceInterface
{
Expand Down Expand Up @@ -56,4 +57,28 @@ public function config($key, $default = null)
{
return Arr::get($this->config, $key, $default);
}

/**
* This method ensures that the given key was filled
* by the user, so that the service can be called without
* errors raised linked to missing configuration.
*
* @param string|string[] $key
* @return void
*/
public function ensureConfigurationParameterDefined($keys) {
// Be able to accept a string and an array of strings.
$keys = is_string($keys) ? [$keys] : $keys ;

foreach($keys as $key) {
$config = $this->config($key) ;

// If the config is not defined / is empty.
if(empty($config)) {
$service = (new \ReflectionClass($this))->getShortName() ;

throw new MissingConfigurationException("Missing '$key' parameter (service: $service)") ;
}
}
}
}
2 changes: 2 additions & 0 deletions src/Services/IPData.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class IPData extends AbstractService
*/
public function boot()
{
$this->ensureConfigurationParameterDefined('key') ;

$this->client = new HttpClient([
'base_uri' => 'https://api.ipdata.co/',
'query' => [
Expand Down
2 changes: 2 additions & 0 deletions src/Services/IPFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class IPFinder extends AbstractService
*/
public function boot()
{
$this->ensureConfigurationParameterDefined('key') ;

$this->client = new HttpClient([
'base_uri' => 'https://api.ipfinder.io/v1/',
'headers' => [
Expand Down
2 changes: 2 additions & 0 deletions src/Services/MaxMindDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class MaxMindDatabase extends AbstractService
*/
public function boot()
{
$this->ensureConfigurationParameterDefined('database_path') ;

$path = $this->config('database_path');
assert(is_string($path), 'Invalid "database_path" config value');

Expand Down
2 changes: 2 additions & 0 deletions src/Services/MaxMindWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class MaxMindWebService extends AbstractService
*/
public function boot()
{
$this->ensureConfigurationParameterDefined(['user_id', 'license_key']) ;

$this->client = new Client(
$this->config('user_id'),
$this->config('license_key'),
Expand Down