-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBexioServiceProvider.php
More file actions
76 lines (63 loc) · 1.83 KB
/
BexioServiceProvider.php
File metadata and controls
76 lines (63 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace Bexio;
use Illuminate\Support\ServiceProvider;
use Saloon\Contracts\Authenticator;
use Saloon\Http\Auth\TokenAuthenticator;
class BexioServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->mergeConfigFrom(
__DIR__ . '/../config/bexio.php',
'bexio'
);
$this->app->singleton(BexioClient::class, function ($app) {
$config = $app['config']['bexio'];
$authentication = $this->resolveAuthentication($config);
return new BexioClient($authentication);
});
$this->app->alias(BexioClient::class, 'bexio');
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/bexio.php' => config_path('bexio.php'),
], 'bexio-config');
}
}
/**
* Resolve the authentication method from config.
*/
protected function resolveAuthentication(array $config): ?Authenticator
{
// If an access token is provided directly, use it
if (! empty($config['access_token'])) {
return new TokenAuthenticator($config['access_token']);
}
// If OAuth credentials are provided and we have a stored token
if (! empty($config['oauth']['access_token'])) {
return new TokenAuthenticator($config['oauth']['access_token']);
}
return null;
}
/**
* Get the services provided by the provider.
*
* @return array<int, string>
*/
public function provides(): array
{
return [
BexioClient::class,
'bexio',
];
}
}