2
2
3
3
namespace NotificationChannels \Messagebird ;
4
4
5
+ use Illuminate \Contracts \Events \Dispatcher ;
6
+ use Illuminate \Notifications \Events \NotificationFailed ;
5
7
use Illuminate \Notifications \Notification ;
8
+ use NotificationChannels \Messagebird \Exceptions \CouldNotSendNotification ;
6
9
7
10
class MessagebirdChannel
8
11
{
9
12
/** @var \NotificationChannels\Messagebird\MessagebirdClient */
10
13
protected $ client ;
14
+ private $ _dispatcher ;
11
15
12
- public function __construct (MessagebirdClient $ client )
16
+ public function __construct (MessagebirdClient $ client, Dispatcher $ dispatcher )
13
17
{
14
18
$ this ->client = $ client ;
19
+ $ this ->_dispatcher = $ dispatcher ;
15
20
}
16
21
17
22
/**
@@ -20,12 +25,15 @@ public function __construct(MessagebirdClient $client)
20
25
* @param mixed $notifiable
21
26
* @param \Illuminate\Notifications\Notification $notification
22
27
*
28
+ * @return array
23
29
* @throws \NotificationChannels\MessageBird\Exceptions\CouldNotSendNotification
24
30
*/
25
31
public function send ($ notifiable , Notification $ notification )
26
32
{
27
33
$ message = $ notification ->toMessagebird ($ notifiable );
28
34
35
+ $ response = [];
36
+
29
37
if (is_string ($ message )) {
30
38
$ message = MessagebirdMessage::create ($ message );
31
39
}
@@ -34,6 +42,19 @@ public function send($notifiable, Notification $notification)
34
42
$ message ->setRecipients ($ to );
35
43
}
36
44
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 ;
38
59
}
39
60
}
0 commit comments