Skip to content

Commit 9e6d0f5

Browse files
committed
✨ Add custom exception on configuration issue
1 parent a2a3b4c commit 9e6d0f5

File tree

7 files changed

+52
-1
lines changed

7 files changed

+52
-1
lines changed

src/Console/Update.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace InteractionDesignFoundation\GeoIP\Console;
66

77
use Illuminate\Console\Command;
8+
use InteractionDesignFoundation\GeoIP\Exceptions\MissingConfigurationException;
89

910
class Update extends Command
1011
{
@@ -41,7 +42,13 @@ public function handle()
4142
public function fire()
4243
{
4344
// Get default service
44-
$service = app('geoip')->getService();
45+
try {
46+
$service = app('geoip')->getService();
47+
} catch(MissingConfigurationException $e) {
48+
$this->components->error($e->getMessage()) ;
49+
50+
return static::FAILURE ;
51+
}
4552

4653
// Ensure the selected service supports updating
4754
if (method_exists($service, 'update') === false) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace InteractionDesignFoundation\GeoIP\Exceptions;
6+
7+
use RuntimeException;
8+
9+
class MissingConfigurationException extends RuntimeException {
10+
11+
}

src/Services/AbstractService.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InteractionDesignFoundation\GeoIP\Location;
88
use Illuminate\Support\Arr;
99
use InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface;
10+
use InteractionDesignFoundation\GeoIP\Exceptions\MissingConfigurationException;
1011

1112
abstract class AbstractService implements ServiceInterface
1213
{
@@ -56,4 +57,28 @@ public function config($key, $default = null)
5657
{
5758
return Arr::get($this->config, $key, $default);
5859
}
60+
61+
/**
62+
* This method ensures that the given key was filled
63+
* by the user, so that the service can be called without
64+
* errors raised linked to missing configuration.
65+
*
66+
* @param string|string[] $key
67+
* @return void
68+
*/
69+
public function ensureConfigurationParameterDefined($keys) {
70+
// Be able to accept a string and an array of strings.
71+
$keys = is_string($keys) ? [$keys] : $keys ;
72+
73+
foreach($keys as $key) {
74+
$config = $this->config($key) ;
75+
76+
// If the config is not defined / is empty.
77+
if(empty($config)) {
78+
$service = (new \ReflectionClass($this))->getShortName() ;
79+
80+
throw new MissingConfigurationException("Missing '$key' parameter (service: $service)") ;
81+
}
82+
}
83+
}
5984
}

src/Services/IPData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class IPData extends AbstractService
2424
*/
2525
public function boot()
2626
{
27+
$this->ensureConfigurationParameterDefined('key') ;
28+
2729
$this->client = new HttpClient([
2830
'base_uri' => 'https://api.ipdata.co/',
2931
'query' => [

src/Services/IPFinder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class IPFinder extends AbstractService
2222
*/
2323
public function boot()
2424
{
25+
$this->ensureConfigurationParameterDefined('key') ;
26+
2527
$this->client = new HttpClient([
2628
'base_uri' => 'https://api.ipfinder.io/v1/',
2729
'headers' => [

src/Services/MaxMindDatabase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class MaxMindDatabase extends AbstractService
2424
*/
2525
public function boot()
2626
{
27+
$this->ensureConfigurationParameterDefined('database_path') ;
28+
2729
$path = $this->config('database_path');
2830
assert(is_string($path), 'Invalid "database_path" config value');
2931

src/Services/MaxMindWebService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class MaxMindWebService extends AbstractService
2424
*/
2525
public function boot()
2626
{
27+
$this->ensureConfigurationParameterDefined(['user_id', 'license_key']) ;
28+
2729
$this->client = new Client(
2830
$this->config('user_id'),
2931
$this->config('license_key'),

0 commit comments

Comments
 (0)