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 | 7 | 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 | 7 | public function send($notifiable, Notification $notification) |
|
44 | { |
||
45 | try { |
||
46 | 7 | $to = $this->getTo($notifiable); |
|
47 | 6 | $message = $notification->toTwilio($notifiable); |
|
48 | |||
49 | 6 | if (is_string($message)) { |
|
50 | 1 | $message = new TwilioSmsMessage($message); |
|
51 | } |
||
52 | |||
53 | 6 | if (! $message instanceof TwilioMessage) { |
|
54 | 1 | throw CouldNotSendNotification::invalidMessageObject($message); |
|
55 | } |
||
56 | |||
57 | 5 | return $this->twilio->sendMessage($message, $to); |
|
58 | 2 | } catch (Exception $exception) { |
|
59 | 2 | $this->events->fire( |
|
60 | 2 | new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage()]) |
|
61 | ); |
||
62 | } |
||
63 | 2 | } |
|
64 | |||
65 | 7 | protected function getTo($notifiable) |
|
76 | } |
||
77 |