1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Illuminate\Notifications; |
4
|
|
|
|
5
|
|
|
use Ramsey\Uuid\Uuid; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Illuminate\Support\Manager; |
8
|
|
|
use Nexmo\Client as NexmoClient; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use GuzzleHttp\Client as HttpClient; |
11
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
12
|
|
|
use Illuminate\Contracts\Bus\Dispatcher as Bus; |
13
|
|
|
use Nexmo\Client\Credentials\Basic as NexmoCredentials; |
14
|
|
|
use Illuminate\Contracts\Notifications\Factory as FactoryContract; |
15
|
|
|
use Illuminate\Contracts\Notifications\Dispatcher as DispatcherContract; |
16
|
|
|
|
17
|
|
|
class ChannelManager extends Manager implements DispatcherContract, FactoryContract |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The default channels used to deliver messages. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $defaultChannels = ['mail', 'database']; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Send the given notification to the given notifiable entities. |
28
|
|
|
* |
29
|
|
|
* @param \Illuminate\Support\Collection|array|mixed $notifiables |
30
|
|
|
* @param mixed $notification |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
3 |
|
public function send($notifiables, $notification) |
34
|
|
|
{ |
35
|
3 |
|
if (! $notifiables instanceof Collection && ! is_array($notifiables)) { |
36
|
1 |
|
$notifiables = [$notifiables]; |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
if ($notification instanceof ShouldQueue) { |
40
|
1 |
|
return $this->queueNotification($notifiables, $notification); |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
return $this->sendNow($notifiables, $notification); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Send the given notification immediately. |
48
|
|
|
* |
49
|
|
|
* @param \Illuminate\Support\Collection|array|mixed $notifiables |
50
|
|
|
* @param mixed $notification |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
2 |
|
public function sendNow($notifiables, $notification) |
54
|
|
|
{ |
55
|
2 |
|
if (! $notifiables instanceof Collection && ! is_array($notifiables)) { |
56
|
|
|
$notifiables = [$notifiables]; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
$original = clone $notification; |
60
|
|
|
|
61
|
2 |
|
foreach ($notifiables as $notifiable) { |
62
|
2 |
|
$notification = clone $original; |
63
|
|
|
|
64
|
2 |
|
$notification->id = (string) Uuid::uuid4(); |
65
|
|
|
|
66
|
2 |
|
$channels = $notification->via($notifiable); |
67
|
|
|
|
68
|
2 |
|
if (empty($channels)) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
foreach ($channels as $channel) { |
73
|
2 |
|
if (! $this->shouldSendNotification($notifiable, $notification, $channel)) { |
74
|
1 |
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
$response = $this->driver($channel)->send($notifiable, $notification); |
78
|
|
|
|
79
|
2 |
|
$this->app->make('events')->fire( |
80
|
2 |
|
new Events\NotificationSent($notifiable, $notification, $channel, $response) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
2 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Determines if the notification can be sent. |
88
|
|
|
* |
89
|
|
|
* @param mixed $notifiable |
90
|
|
|
* @param mixed $notification |
91
|
|
|
* @param string $channel |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
2 |
|
protected function shouldSendNotification($notifiable, $notification, $channel) |
95
|
|
|
{ |
96
|
2 |
|
return $this->app->make('events')->until( |
97
|
2 |
|
new Events\NotificationSending($notifiable, $notification, $channel) |
98
|
2 |
|
) !== false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Queue the given notification instances. |
103
|
|
|
* |
104
|
|
|
* @param mixed $notifiables |
105
|
|
|
* @param array[\Illuminate\Notifcations\Channels\Notification] $notification |
|
|
|
|
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
1 |
|
protected function queueNotification($notifiables, $notification) |
109
|
|
|
{ |
110
|
1 |
|
if (version_compare($this->app->version(), '5.2', '<')) { |
111
|
1 |
|
$this->app->make(Bus::class)->dispatch( |
112
|
1 |
|
(new SendQueuedNotifications($notifiables, $notification)) |
113
|
1 |
|
->onQueue($notification->queue) |
114
|
1 |
|
->delay($notification->delay) |
115
|
|
|
); |
116
|
|
|
} else { |
117
|
|
|
$this->app->make(Bus::class)->dispatch( |
118
|
|
|
(new SendQueuedNotifications($notifiables, $notification)) |
119
|
|
|
->onConnection($notification->connection) |
120
|
|
|
->onQueue($notification->queue) |
121
|
|
|
->delay($notification->delay) |
122
|
|
|
); |
123
|
|
|
} |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get a channel instance. |
128
|
|
|
* |
129
|
|
|
* @param string|null $name |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function channel($name = null) |
133
|
|
|
{ |
134
|
|
|
return $this->driver($name); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create an instance of the database driver. |
139
|
|
|
* |
140
|
|
|
* @return \Illuminate\Notifications\Channels\DatabaseChannel |
141
|
|
|
*/ |
142
|
|
|
protected function createDatabaseDriver() |
143
|
|
|
{ |
144
|
|
|
return $this->app->make(Channels\DatabaseChannel::class); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Create an instance of the broadcast driver. |
149
|
|
|
* |
150
|
|
|
* @return \Illuminate\Notifications\Channels\BroadcastChannel |
151
|
|
|
*/ |
152
|
|
|
protected function createBroadcastDriver() |
153
|
|
|
{ |
154
|
|
|
return $this->app->make(Channels\BroadcastChannel::class); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Create an instance of the mail driver. |
159
|
|
|
* |
160
|
|
|
* @return \Illuminate\Notifications\Channels\MailChannel |
161
|
|
|
*/ |
162
|
|
|
protected function createMailDriver() |
163
|
|
|
{ |
164
|
|
|
return $this->app->make(Channels\MailChannel::class); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Create an instance of the Nexmo driver. |
169
|
|
|
* |
170
|
|
|
* @return \Illuminate\Notifications\Channels\NexmoSmsChannel |
171
|
|
|
*/ |
172
|
|
|
protected function createNexmoDriver() |
173
|
|
|
{ |
174
|
|
|
return new Channels\NexmoSmsChannel( |
175
|
|
|
new NexmoClient(new NexmoCredentials( |
176
|
|
|
$this->app['config']['services.nexmo.key'], |
177
|
|
|
$this->app['config']['services.nexmo.secret'] |
178
|
|
|
)), |
179
|
|
|
$this->app['config']['services.nexmo.sms_from'] |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Create an instance of the Slack driver. |
185
|
|
|
* |
186
|
|
|
* @return \Illuminate\Notifications\Channels\SlackWebhookChannel |
187
|
|
|
*/ |
188
|
|
|
protected function createSlackDriver() |
189
|
|
|
{ |
190
|
|
|
return new Channels\SlackWebhookChannel(new HttpClient); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Create a new driver instance. |
195
|
|
|
* |
196
|
|
|
* @param string $driver |
197
|
|
|
* @return mixed |
198
|
|
|
* |
199
|
|
|
* @throws \InvalidArgumentException |
200
|
|
|
*/ |
201
|
|
|
protected function createDriver($driver) |
202
|
|
|
{ |
203
|
|
|
try { |
204
|
|
|
return parent::createDriver($driver); |
205
|
|
|
} catch (InvalidArgumentException $e) { |
206
|
|
|
if (class_exists($driver)) { |
207
|
|
|
return $this->app->make($driver); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
throw $e; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get the default channel driver names. |
216
|
|
|
* |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
public function getDefaultDriver() |
220
|
|
|
{ |
221
|
|
|
return $this->defaultChannels; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get the default channel driver names. |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
|
|
public function deliversVia() |
230
|
|
|
{ |
231
|
|
|
return $this->getDefaultDriver(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Set the default channel driver names. |
236
|
|
|
* |
237
|
|
|
* @param array|string $channels |
238
|
|
|
* @return void |
239
|
|
|
*/ |
240
|
|
|
public function deliverVia($channels) |
241
|
|
|
{ |
242
|
|
|
$this->defaultChannels = (array) $channels; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.