generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLaravelMsGraphMailServiceProvider.php
61 lines (53 loc) · 1.95 KB
/
LaravelMsGraphMailServiceProvider.php
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
<?php
namespace InnoGE\LaravelMsGraphMail;
use Illuminate\Support\Facades\Mail;
use InnoGE\LaravelMsGraphMail\Exceptions\ConfigurationInvalid;
use InnoGE\LaravelMsGraphMail\Exceptions\ConfigurationMissing;
use InnoGE\LaravelMsGraphMail\Services\MicrosoftGraphApiService;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
class LaravelMsGraphMailServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
/*
* This class is a Package Service Provider
*
* More info: https://github.com/spatie/laravel-package-tools
*/
$package
->name('laravel-msgraph-mail');
}
public function boot(): void
{
Mail::extend('microsoft-graph', function (array $config): MicrosoftGraphTransport {
$accessTokenTtl = $config['access_token_ttl'] ?? 3000;
if (! is_int($accessTokenTtl)) {
throw new ConfigurationInvalid('access_token_ttl', $accessTokenTtl);
}
return new MicrosoftGraphTransport(
new MicrosoftGraphApiService(
tenantId: $this->requireConfigString($config, 'tenant_id'),
clientId: $this->requireConfigString($config, 'client_id'),
clientSecret: $this->requireConfigString($config, 'client_secret'),
accessTokenTtl: $accessTokenTtl,
),
);
});
}
/**
* @param array<string, mixed> $config
* @return non-empty-string
*/
protected function requireConfigString(array $config, string $key): string
{
if (! array_key_exists($key, $config)) {
throw new ConfigurationMissing($key);
}
$value = $config[$key];
if (! is_string($value) || $value === '') {
throw new ConfigurationInvalid($key, $value);
}
return $value;
}
}