|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twilio; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
|
7
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
|
8
|
|
|
use Illuminate\Notifications\Notification; |
|
9
|
|
|
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification; |
|
10
|
|
|
|
|
11
|
|
|
class TwilioChannel |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Twilio |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $twilio; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Dispatcher |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $events; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* TwilioChannel constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Twilio $twilio |
|
27
|
|
|
* @param Dispatcher $events |
|
28
|
|
|
*/ |
|
29
|
13 |
|
public function __construct(Twilio $twilio, Dispatcher $events) |
|
30
|
|
|
{ |
|
31
|
13 |
|
$this->twilio = $twilio; |
|
32
|
13 |
|
$this->events = $events; |
|
33
|
13 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Send the given notification. |
|
37
|
|
|
* |
|
38
|
|
|
* @param mixed $notifiable |
|
39
|
|
|
* @param Notification $notification |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
* @throws Exception |
|
43
|
|
|
*/ |
|
44
|
13 |
|
public function send($notifiable, Notification $notification) |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
13 |
|
$to = $this->getTo($notifiable); |
|
48
|
12 |
|
$message = $notification->toTwilio($notifiable); |
|
|
|
|
|
|
49
|
12 |
|
$useSender = $this->canReceiveAlphanumericSender($notifiable); |
|
50
|
|
|
|
|
51
|
12 |
|
if (is_string($message)) { |
|
52
|
4 |
|
$message = new TwilioSmsMessage($message); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
12 |
|
if (! $message instanceof TwilioMessage) { |
|
56
|
1 |
|
throw CouldNotSendNotification::invalidMessageObject($message); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
11 |
|
return $this->twilio->sendMessage($message, $to, $useSender); |
|
60
|
5 |
|
} catch (Exception $exception) { |
|
61
|
5 |
|
$event = new NotificationFailed( |
|
62
|
5 |
|
$notifiable, |
|
63
|
|
|
$notification, |
|
64
|
5 |
|
'twilio', |
|
65
|
5 |
|
['message' => $exception->getMessage(), 'exception' => $exception] |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
5 |
|
if (function_exists('event')) { // Use event helper when possible to add Lumen support |
|
69
|
|
|
event($event); |
|
70
|
|
|
} else { |
|
71
|
5 |
|
$this->events->fire($event); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
5 |
|
if ($this->twilio->config->isIgnoredErrorCode($exception->getCode())) { |
|
75
|
2 |
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
throw $exception; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the address to send a notification to. |
|
84
|
|
|
* |
|
85
|
|
|
* @param mixed $notifiable |
|
86
|
|
|
* |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
* @throws CouldNotSendNotification |
|
89
|
|
|
*/ |
|
90
|
13 |
|
protected function getTo($notifiable) |
|
91
|
|
|
{ |
|
92
|
13 |
|
if ($notifiable->routeNotificationFor('twilio')) { |
|
93
|
1 |
|
return $notifiable->routeNotificationFor('twilio'); |
|
94
|
|
|
} |
|
95
|
12 |
|
if (isset($notifiable->phone_number)) { |
|
96
|
11 |
|
return $notifiable->phone_number; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
throw CouldNotSendNotification::invalidReceiver(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get the alphanumeric sender. |
|
104
|
|
|
* |
|
105
|
|
|
* @param $notifiable |
|
106
|
|
|
* |
|
107
|
|
|
* @return mixed|null |
|
108
|
|
|
* @throws CouldNotSendNotification |
|
109
|
|
|
*/ |
|
110
|
12 |
|
protected function canReceiveAlphanumericSender($notifiable) |
|
111
|
|
|
{ |
|
112
|
12 |
|
return method_exists($notifiable, 'canReceiveAlphanumericSender') && |
|
113
|
12 |
|
$notifiable->canReceiveAlphanumericSender(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.