|
4 | 4 |
|
5 | 5 | use Exception; |
6 | 6 | use Illuminate\Notifications\Notification; |
| 7 | +use Illuminate\Events\Dispatcher; |
7 | 8 | use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification; |
| 9 | +use Illuminate\Notifications\Events\NotificationFailed; |
8 | 10 | use Services_Twilio; |
9 | 11 |
|
10 | 12 | class TwilioChannel |
11 | 13 | { |
12 | | - /** @var \Services_Twilio */ |
| 14 | + /** |
| 15 | + * @var \Services_Twilio |
| 16 | + */ |
13 | 17 | protected $twilio; |
14 | 18 |
|
15 | | - public function __construct(Services_Twilio $twilio) |
| 19 | + /** |
| 20 | + * @var \Illuminate\Events\Dispatcher |
| 21 | + */ |
| 22 | + private $events; |
| 23 | + |
| 24 | + /** |
| 25 | + * TwilioChannel constructor. |
| 26 | + * |
| 27 | + * @param Services_Twilio $twilio |
| 28 | + * @param Dispatcher $events |
| 29 | + */ |
| 30 | + public function __construct(Services_Twilio $twilio, Dispatcher $events) |
16 | 31 | { |
17 | 32 | $this->twilio = $twilio; |
| 33 | + $this->events = $events; |
18 | 34 | } |
19 | 35 |
|
20 | 36 | /** |
21 | 37 | * Send the given notification. |
22 | 38 | * |
23 | 39 | * @param mixed $notifiable |
24 | 40 | * @param \Illuminate\Notifications\Notification $notification |
25 | | - * |
| 41 | + * @return mixed |
26 | 42 | * @throws CouldNotSendNotification |
27 | 43 | */ |
28 | 44 | public function send($notifiable, Notification $notification) |
29 | 45 | { |
30 | 46 | if (! $to = $notifiable->routeNotificationFor('twilio')) { |
31 | 47 | if (! $to = $notifiable->phone_number) { |
32 | | - return; |
| 48 | + return null; |
33 | 49 | } |
34 | 50 | } |
35 | 51 |
|
36 | | - $message = $notification->toTwilio($notifiable); |
| 52 | + try { |
| 53 | + $message = $notification->toTwilio($notifiable); |
37 | 54 |
|
38 | | - if (is_string($message)) { |
39 | | - $message = new TwilioSmsMessage($message); |
40 | | - } |
| 55 | + if (is_string($message)) { |
| 56 | + $message = new TwilioSmsMessage($message); |
| 57 | + } |
41 | 58 |
|
42 | | - if (! $message instanceof TwilioAbstractMessage) { |
43 | | - throw CouldNotSendNotification::invalidMessageObject($message); |
44 | | - } |
| 59 | + if (! $message instanceof TwilioAbstractMessage) { |
| 60 | + throw CouldNotSendNotification::invalidMessageObject($message); |
| 61 | + } |
45 | 62 |
|
46 | | - if (! $from = $message->from ?: config('services.twilio.from')) { |
47 | | - throw CouldNotSendNotification::missingFrom(); |
48 | | - } |
| 63 | + if (! $from = $message->from ?: config('services.twilio.from')) { |
| 64 | + throw CouldNotSendNotification::missingFrom(); |
| 65 | + } |
49 | 66 |
|
50 | | - try { |
51 | | - $this->sendMessage($message, $from, $to); |
| 67 | + return $this->sendMessage($message, $from, $to); |
52 | 68 | } catch (Exception $exception) { |
53 | | - throw CouldNotSendNotification::serviceRespondedWithAnException($exception); |
| 69 | + $this->events->fire( |
| 70 | + new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage()]) |
| 71 | + ); |
54 | 72 | } |
55 | 73 | } |
56 | 74 |
|
|
0 commit comments