Skip to content

Commit

Permalink
Merge pull request #9 from worksome/feature/region
Browse files Browse the repository at this point in the history
feat: add support for configuring region
  • Loading branch information
owenvoke authored Oct 1, 2024
2 parents 951bf2a + 3b2f7dc commit 89ea37b
Show file tree
Hide file tree
Showing 3 changed files with 28 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
4 changes: 4 additions & 0 deletions config/onfido.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

declare(strict_types=1);

use Onfido\Region;

return [

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

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

];
25 changes: 23 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));

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

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

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

private function getRegionFromKey(Region|string $region): Region
{
if ($region instanceof Region) {
return $region;
}

$region = strtoupper($region);

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

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

0 comments on commit 89ea37b

Please sign in to comment.