forked from brayancruces/twilio
-
Notifications
You must be signed in to change notification settings - Fork 37
/
TwilioProvider.php
executable file
·75 lines (62 loc) · 2.31 KB
/
TwilioProvider.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace NotificationChannels\Twilio;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use NotificationChannels\Twilio\Exceptions\InvalidConfigException;
use Twilio\Rest\Client as TwilioService;
class TwilioProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Bootstrap the application services.
*/
public function boot() {}
/**
* Register the application services.
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/twilio-notification-channel.php', 'twilio-notification-channel');
$this->publishes([
__DIR__.'/../config/twilio-notification-channel.php' => config_path('twilio-notification-channel.php'),
]);
$this->app->bind(TwilioConfig::class, function () {
return new TwilioConfig($this->app['config']['twilio-notification-channel']);
});
$this->app->singleton(TwilioService::class, function (Application $app) {
/** @var TwilioConfig $config */
$config = $app->make(TwilioConfig::class);
if ($config->usingUsernamePasswordAuth()) {
return new TwilioService($config->getUsername(), $config->getPassword(), $config->getAccountSid());
}
if ($config->usingTokenAuth()) {
return new TwilioService($config->getAccountSid(), $config->getAuthToken());
}
throw InvalidConfigException::missingConfig();
});
$this->app->singleton(Twilio::class, function (Application $app) {
return new Twilio(
$app->make(TwilioService::class),
$app->make(TwilioConfig::class)
);
});
$this->app->singleton(TwilioChannel::class, function (Application $app) {
return new TwilioChannel(
$app->make(Twilio::class),
$app->make(Dispatcher::class)
);
});
}
/**
* Get the services provided by the provider.
*/
public function provides(): array
{
return [
TwilioChannel::class,
TwilioConfig::class,
TwilioService::class,
];
}
}