1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twilio; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
8
|
|
|
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification; |
9
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
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
|
10 |
|
public function __construct(Twilio $twilio, Dispatcher $events) |
30
|
|
|
{ |
31
|
10 |
|
$this->twilio = $twilio; |
32
|
10 |
|
$this->events = $events; |
33
|
10 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Send the given notification. |
37
|
|
|
* |
38
|
|
|
* @param mixed $notifiable |
39
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
40
|
|
|
* @return mixed |
41
|
|
|
* @throws CouldNotSendNotification |
42
|
|
|
*/ |
43
|
10 |
|
public function send($notifiable, Notification $notification) |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
10 |
|
$to = $this->getTo($notifiable); |
47
|
9 |
|
$message = $notification->toTwilio($notifiable); |
|
|
|
|
48
|
9 |
|
$useSender = $this->canReceiveAlphanumericSender($notifiable); |
49
|
|
|
|
50
|
9 |
|
if (is_string($message)) { |
51
|
1 |
|
$message = new TwilioSmsMessage($message); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
9 |
|
if (! $message instanceof TwilioMessage) { |
55
|
1 |
|
throw CouldNotSendNotification::invalidMessageObject($message); |
56
|
|
|
} |
57
|
|
|
|
58
|
8 |
|
return $this->twilio->sendMessage($message, $to, $useSender); |
59
|
2 |
|
} catch (Exception $exception) { |
60
|
2 |
|
$event = new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage(), 'exception' => $exception]); |
61
|
2 |
|
if (function_exists('event')) { // Use event helper when possible to add Lumen support |
62
|
|
|
event($event); |
63
|
|
|
} else { |
64
|
2 |
|
$this->events->fire($event); |
65
|
|
|
} |
66
|
|
|
} |
67
|
2 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the address to send a notification to. |
71
|
|
|
* |
72
|
|
|
* @param mixed $notifiable |
73
|
|
|
* @return mixed |
74
|
|
|
* @throws CouldNotSendNotification |
75
|
|
|
*/ |
76
|
10 |
|
protected function getTo($notifiable) |
77
|
|
|
{ |
78
|
10 |
|
if ($notifiable->routeNotificationFor('twilio')) { |
79
|
1 |
|
return $notifiable->routeNotificationFor('twilio'); |
80
|
|
|
} |
81
|
9 |
|
if (isset($notifiable->phone_number)) { |
82
|
8 |
|
return $notifiable->phone_number; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
throw CouldNotSendNotification::invalidReceiver(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the alphanumeric sender. |
90
|
|
|
* |
91
|
|
|
* @param $notifiable |
92
|
|
|
* @return mixed|null |
93
|
|
|
* @throws CouldNotSendNotification |
94
|
|
|
*/ |
95
|
9 |
|
protected function canReceiveAlphanumericSender($notifiable) |
96
|
|
|
{ |
97
|
9 |
|
return method_exists($notifiable, 'canReceiveAlphanumericSender') && |
98
|
9 |
|
$notifiable->canReceiveAlphanumericSender(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.