Skip to content

Commit 20d692c

Browse files
committed
Fire NotificationFailed event on error
1 parent abfeaf5 commit 20d692c

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

src/Exceptions/CouldNotSendNotification.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77

88
class CouldNotSendNotification extends \Exception
99
{
10-
/**
11-
* @param \Exception $exception
12-
*
13-
* @return static
14-
*/
15-
public static function serviceRespondedWithAnException($exception)
16-
{
17-
return new static("Notification was not sent. Twilio responded with `{$exception->getCode()}: {$exception->getMessage()}`");
18-
}
19-
2010
/**
2111
* @param mixed $message
2212
*

src/TwilioChannel.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,71 @@
44

55
use Exception;
66
use Illuminate\Notifications\Notification;
7+
use Illuminate\Events\Dispatcher;
78
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
9+
use Illuminate\Notifications\Events\NotificationFailed;
810
use Services_Twilio;
911

1012
class TwilioChannel
1113
{
12-
/** @var \Services_Twilio */
14+
/**
15+
* @var \Services_Twilio
16+
*/
1317
protected $twilio;
1418

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)
1631
{
1732
$this->twilio = $twilio;
33+
$this->events = $events;
1834
}
1935

2036
/**
2137
* Send the given notification.
2238
*
2339
* @param mixed $notifiable
2440
* @param \Illuminate\Notifications\Notification $notification
25-
*
41+
* @return mixed
2642
* @throws CouldNotSendNotification
2743
*/
2844
public function send($notifiable, Notification $notification)
2945
{
3046
if (! $to = $notifiable->routeNotificationFor('twilio')) {
3147
if (! $to = $notifiable->phone_number) {
32-
return;
48+
return null;
3349
}
3450
}
3551

36-
$message = $notification->toTwilio($notifiable);
52+
try {
53+
$message = $notification->toTwilio($notifiable);
3754

38-
if (is_string($message)) {
39-
$message = new TwilioSmsMessage($message);
40-
}
55+
if (is_string($message)) {
56+
$message = new TwilioSmsMessage($message);
57+
}
4158

42-
if (! $message instanceof TwilioAbstractMessage) {
43-
throw CouldNotSendNotification::invalidMessageObject($message);
44-
}
59+
if (! $message instanceof TwilioAbstractMessage) {
60+
throw CouldNotSendNotification::invalidMessageObject($message);
61+
}
4562

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+
}
4966

50-
try {
51-
$this->sendMessage($message, $from, $to);
67+
return $this->sendMessage($message, $from, $to);
5268
} catch (Exception $exception) {
53-
throw CouldNotSendNotification::serviceRespondedWithAnException($exception);
69+
$this->events->fire(
70+
new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage()])
71+
);
5472
}
5573
}
5674

0 commit comments

Comments
 (0)