Skip to content

Commit d05abf9

Browse files
authored
Merge e4effae into 737624c
2 parents 737624c + e4effae commit d05abf9

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/MessagebirdChannel.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
namespace NotificationChannels\Messagebird;
44

5+
use Illuminate\Contracts\Events\Dispatcher;
6+
use Illuminate\Notifications\Events\NotificationFailed;
57
use Illuminate\Notifications\Notification;
8+
use NotificationChannels\Messagebird\Exceptions\CouldNotSendNotification;
69

710
class MessagebirdChannel
811
{
912
/** @var \NotificationChannels\Messagebird\MessagebirdClient */
1013
protected $client;
14+
private $_dispatcher;
1115

12-
public function __construct(MessagebirdClient $client)
16+
public function __construct(MessagebirdClient $client, Dispatcher $dispatcher)
1317
{
1418
$this->client = $client;
19+
$this->_dispatcher = $dispatcher;
1520
}
1621

1722
/**
@@ -20,12 +25,15 @@ public function __construct(MessagebirdClient $client)
2025
* @param mixed $notifiable
2126
* @param \Illuminate\Notifications\Notification $notification
2227
*
28+
* @return array
2329
* @throws \NotificationChannels\MessageBird\Exceptions\CouldNotSendNotification
2430
*/
2531
public function send($notifiable, Notification $notification)
2632
{
2733
$message = $notification->toMessagebird($notifiable);
2834

35+
$response = [];
36+
2937
if (is_string($message)) {
3038
$message = MessagebirdMessage::create($message);
3139
}
@@ -34,6 +42,19 @@ public function send($notifiable, Notification $notification)
3442
$message->setRecipients($to);
3543
}
3644

37-
$this->client->send($message);
45+
try {
46+
$response = $this->client->send($message);
47+
48+
$this->_dispatcher->dispatch('messagebird-sms', [$notifiable, $notification, $response]);
49+
} catch (CouldNotSendNotification $e) {
50+
new NotificationFailed(
51+
$notifiable,
52+
$notification,
53+
'messagebird-sms',
54+
$e->getMessage()
55+
);
56+
}
57+
58+
return $response;
3859
}
3960
}

src/MessagebirdClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function send(MessagebirdMessage $message)
4040
}
4141

4242
try {
43-
$this->client->request('POST', 'https://rest.messagebird.com/messages', [
43+
return $this->client->request('POST', 'https://rest.messagebird.com/messages', [
4444
'body' => $message->toJson(),
4545
'headers' => [
4646
'Authorization' => 'AccessKey '.$this->access_key,

0 commit comments

Comments
 (0)