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