1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twilio; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
6
|
|
|
use Illuminate\Contracts\Foundation\Application; |
7
|
|
|
use Illuminate\Contracts\Support\DeferrableProvider; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use NotificationChannels\Twilio\Exceptions\InvalidConfigException; |
10
|
|
|
use Twilio\Rest\Client as TwilioService; |
11
|
|
|
|
12
|
|
|
class TwilioProvider extends ServiceProvider implements DeferrableProvider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Bootstrap the application services. |
16
|
2 |
|
*/ |
17
|
|
|
public function boot() |
18
|
2 |
|
{ |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Register the application services. |
23
|
|
|
*/ |
24
|
|
|
public function register() |
25
|
|
|
{ |
26
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/twilio-notification-channel.php', 'twilio-notification-channel'); |
27
|
|
|
|
28
|
|
|
$this->publishes([ |
29
|
|
|
__DIR__.'/../config/twilio-notification-channel.php' => config_path('twilio-notification-channel.php'), |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$this->app->bind(TwilioConfig::class, function () { |
33
|
|
|
return new TwilioConfig($this->app['config']['twilio-notification-channel']); |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
$this->app->singleton(TwilioService::class, function (Application $app) { |
37
|
|
|
/** @var TwilioConfig $config */ |
38
|
|
|
$config = $app->make(TwilioConfig::class); |
39
|
|
|
|
40
|
|
|
if ($config->usingUsernamePasswordAuth()) { |
41
|
|
|
return new TwilioService($config->getUsername(), $config->getPassword(), $config->getAccountSid()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($config->usingTokenAuth()) { |
45
|
|
|
return new TwilioService($config->getAccountSid(), $config->getAuthToken()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
throw InvalidConfigException::missingConfig(); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
$this->app->singleton(Twilio::class, function (Application $app) { |
52
|
|
|
return new Twilio( |
53
|
|
|
$app->make(TwilioService::class), |
54
|
|
|
$app->make(TwilioConfig::class) |
55
|
|
|
); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$this->app->singleton(TwilioChannel::class, function (Application $app) { |
59
|
|
|
return new TwilioChannel( |
60
|
|
|
$app->make(Twilio::class), |
61
|
|
|
$app->make(Dispatcher::class) |
62
|
|
|
); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the services provided by the provider. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function provides(): array |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
TwilioConfig::class, |
75
|
|
|
TwilioService::class, |
76
|
|
|
TwilioChannel::class, |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|