1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twilio; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use NotificationChannels\Twilio\Events\MessageWasSent; |
8
|
|
|
use NotificationChannels\Twilio\Events\SendingMessage; |
9
|
|
|
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification; |
10
|
|
|
use Services_Twilio; |
11
|
|
|
|
12
|
|
|
class TwilioChannel |
13
|
|
|
{ |
14
|
|
|
/** @var \Services_Twilio */ |
15
|
|
|
protected $twilio; |
16
|
|
|
|
17
|
|
|
public function __construct(Services_Twilio $twilio) |
18
|
|
|
{ |
19
|
|
|
$this->twilio = $twilio; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Send the given notification. |
24
|
|
|
* |
25
|
|
|
* @param mixed $notifiable |
26
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
27
|
|
|
* |
28
|
|
|
* @throws CouldNotSendNotification |
29
|
|
|
*/ |
30
|
|
|
public function send($notifiable, Notification $notification) |
31
|
|
|
{ |
32
|
|
|
if (!$to = $notifiable->routeNotificationFor('twilio')) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$message = $notification->toTwilio($notifiable); |
37
|
|
|
|
38
|
|
|
if (is_string($message)) { |
39
|
|
|
$message = new TwilioSmsMessage($message); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!in_array(get_class($message), [TwilioSmsMessage::class, TwilioCallMessage::class])) { |
43
|
|
|
throw CouldNotSendNotification::invalidMessageObject($message); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!$from = $message->from ?: config('services.twilio.from')) { |
47
|
|
|
throw CouldNotSendNotification::missingFrom(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$shouldSendMessage = event(new SendingMessage($notifiable, $notification, $message), [], true) !== false; |
51
|
|
|
|
52
|
|
|
if (!$shouldSendMessage) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$response = null; |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$response = $this->sendMessage($message, $from, $to); |
60
|
|
|
} catch (Exception $exception) { |
61
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnException($exception); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
event(new MessageWasSent($notifiable, $notification, $response)); |
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
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.