2
2
3
3
namespace NotificationChannels \Twilio ;
4
4
5
+ use Exception ;
5
6
use Illuminate \Notifications \Notification ;
6
7
use NotificationChannels \Twilio \Events \MessageWasSent ;
7
8
use NotificationChannels \Twilio \Events \SendingMessage ;
10
11
11
12
class TwilioChannel
12
13
{
13
- /**
14
- * @var \Services_Twilio
15
- */
14
+ /** @var \Services_Twilio */
16
15
protected $ twilio ;
17
16
18
17
public function __construct (Services_Twilio $ twilio )
@@ -25,60 +24,73 @@ public function __construct(Services_Twilio $twilio)
25
24
*
26
25
* @param mixed $notifiable
27
26
* @param \Illuminate\Notifications\Notification $notification
28
- * @return void
27
+ *
29
28
* @throws CouldNotSendNotification
30
29
*/
31
30
public function send ($ notifiable , Notification $ notification )
32
31
{
33
- if (! $ to = $ notifiable ->routeNotificationFor ('twilio ' )) {
32
+ if (!$ to = $ notifiable ->routeNotificationFor ('twilio ' )) {
34
33
return ;
35
34
}
36
35
37
36
$ message = $ notification ->toTwilio ($ notifiable );
38
37
39
38
if (is_string ($ message )) {
40
- // Default to SMS message if only a string is provided
41
39
$ message = new TwilioSmsMessage ($ message );
42
40
}
43
41
44
- if (! ($ message instanceof TwilioSmsMessage) &&
45
- ! ($ message instanceof TwilioCallMessage)) {
46
- $ class = get_class ($ message ) ?: 'Unknown ' ;
47
-
48
- throw CouldNotSendNotification::invalidMessageObject ($ class );
42
+ if (!in_array (get_class ($ message ), [TwilioSmsMessage::class, TwilioCallMessage::class])) {
43
+ throw CouldNotSendNotification::invalidMessageObject ($ message );
49
44
}
50
45
51
- if (! $ from = $ message ->from ?: config ('services.twilio.from ' )) {
46
+ if (!$ from = $ message ->from ?: config ('services.twilio.from ' )) {
52
47
throw CouldNotSendNotification::missingFrom ();
53
48
}
54
49
55
50
$ shouldSendMessage = event (new SendingMessage ($ notifiable , $ notification , $ message ), [], true ) !== false ;
56
51
57
- if (! $ shouldSendMessage ) {
52
+ if (!$ shouldSendMessage ) {
58
53
return ;
59
54
}
60
55
61
56
$ response = null ;
62
57
63
- /* @var TwilioSmsMessage|TwilioCallMessage $message */
64
58
try {
65
- if ($ message instanceof TwilioSmsMessage) {
66
- $ response = $ this ->twilio ->account ->messages ->sendMessage (
67
- $ from ,
68
- $ to ,
69
- trim ($ message ->content )
70
- );
71
- } elseif ($ message instanceof TwilioCallMessage) {
72
- $ response = $ this ->twilio ->account ->calls ->create (
73
- $ from ,
74
- $ to ,
75
- trim ($ message ->url )
76
- );
77
- }
78
- } catch (\Exception $ exception ) {
59
+ $ response = $ this ->sendMessage ($ message , $ from , $ to );
60
+ } catch (Exception $ exception ) {
79
61
throw CouldNotSendNotification::serviceRespondedWithAnException ($ exception );
80
62
}
81
63
82
64
event (new MessageWasSent ($ notifiable , $ notification , $ response ));
83
65
}
66
+
67
+ /**
68
+ * @param $message
69
+ * @param $from
70
+ * @param $to
71
+ * @return mixed
72
+ *
73
+ * @throws \NotificationChannels\Twilio\Exceptions\CouldNotSendNotification
74
+ */
75
+ protected function sendMessage ($ message , $ from , $ to )
76
+ {
77
+ if ($ message instanceof TwilioSmsMessage) {
78
+ return $ this ->twilio ->account ->messages ->sendMessage (
79
+ $ from ,
80
+ $ to ,
81
+ trim ($ message ->content )
82
+ );
83
+ return $ response ;
84
+ }
85
+
86
+ if ($ message instanceof TwilioCallMessage) {
87
+ return $ this ->twilio ->account ->calls ->create (
88
+ $ from ,
89
+ $ to ,
90
+ trim ($ message ->url )
91
+ );
92
+ }
93
+
94
+ throw CouldNotSendNotification::invalidMessageObject ($ message );
95
+ }
84
96
}
0 commit comments