1 | <?php |
||
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) |
|
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 | } |
||
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 | $this->events->fire( |
|
61 | 2 | new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage()]) |
|
62 | ); |
||
63 | } |
||
64 | 2 | } |
|
65 | |||
66 | /** |
||
67 | * Get the address to send a notification to. |
||
68 | * |
||
69 | * @param mixed $notifiable |
||
70 | * @return mixed |
||
71 | * @throws CouldNotSendNotification |
||
72 | */ |
||
73 | 10 | protected function getTo($notifiable) |
|
84 | |||
85 | /** |
||
86 | * Get the alphanumeric sender. |
||
87 | * |
||
88 | * @param $notifiable |
||
89 | * @return mixed|null |
||
90 | * @throws CouldNotSendNotification |
||
91 | */ |
||
92 | 9 | protected function canReceiveAlphanumericSender($notifiable) |
|
97 | } |
||
98 |