Skip to content

Commit

Permalink
feat: add support for configuring region
Browse files Browse the repository at this point in the history
  • Loading branch information
owenvoke committed Oct 1, 2024
1 parent 42ffed3 commit 5c26bc9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^8.2",
"onfido/onfido-php": "^7.3",
"onfido/onfido-php": "^7.5",
"illuminate/support": "^10.0 || ^11.0"
},
"require-dev": {
Expand Down
16 changes: 16 additions & 0 deletions config/onfido.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@

declare(strict_types=1);

use Onfido\Region;

return [

/*
|--------------------------------------------------------------------------
| API Key
|--------------------------------------------------------------------------
|
| The cache driver is a decorator that will store rates retrieved from the
| given strategy in your application cache for the specified timeout. By
| default, we set the timeout to 24 hours, but you're free to alter it
| to suit the needs of your app.
|
*/

'api_key' => env('ONFIDO_API_KEY', 'api_testing.default'),

'region' => env('ONFIDO_REGION', Region::EU->name),

];
21 changes: 19 additions & 2 deletions src/OnfidoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
use Illuminate\Support\ServiceProvider;
use Onfido\Api\DefaultApi;
use Onfido\Configuration;
use Onfido\Region;

class OnfidoServiceProvider extends ServiceProvider implements DeferrableProvider
{
public function register(): void
{
$this->app->singleton('onfido', function (Container $app) {
$token = $app->make(ConfigRepository::class)->get('onfido.api_key');
$config = $app->make(ConfigRepository::class);

$token = $config->get('onfido.api_key');

$region = $this->getRegionFromKey($config->get('onfido.region', Region::EU->name));

$config = Configuration::getDefaultConfiguration()
->setApiToken($token);
->setApiToken($token)
->setRegion($region);

return new DefaultApi(config: $config);
});
Expand All @@ -45,4 +51,15 @@ public function boot(): void

$this->mergeConfigFrom($config, 'onfido');
}

private function getRegionFromKey(string $key): Region
{
$key = strtoupper($key);

if (! in_array($key, array_map(fn (Region $region) => $region->name, Region::cases()))) {
return Region::EU;
}

return constant(Region::class . '::' . $key);
}
}

0 comments on commit 5c26bc9

Please sign in to comment.