1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\TotalVoice; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
8
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
9
|
|
|
use NotificationChannels\TotalVoice\Exceptions\CouldNotSendNotification; |
10
|
|
|
|
11
|
|
|
class TotalVoiceChannel |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var TotalVoice |
15
|
|
|
*/ |
16
|
|
|
protected $totalvoice; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Dispatcher |
20
|
|
|
*/ |
21
|
|
|
protected $events; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* TotalVoiceChannel constructor. |
25
|
|
|
* |
26
|
|
|
* @param TotalVoice $totalvoice |
27
|
|
|
* @param Dispatcher $events |
28
|
|
|
*/ |
29
|
7 |
|
public function __construct(TotalVoice $totalvoice, Dispatcher $events) |
30
|
|
|
{ |
31
|
7 |
|
$this->totalvoice = $totalvoice; |
32
|
7 |
|
$this->events = $events; |
33
|
7 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Send the given notification. |
37
|
|
|
* |
38
|
|
|
* @param mixed $notifiable |
39
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
* @throws \NotificationChannels\TotalVoice\Exceptions\CouldNotSendNotification |
43
|
|
|
*/ |
44
|
7 |
|
public function send($notifiable, Notification $notification) |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
7 |
|
$to = $this->getTo($notifiable); |
48
|
|
|
|
49
|
6 |
|
if (! method_exists($notification, 'toTotalVoice')) { |
50
|
6 |
|
throw CouldNotSendNotification::notificationMethodNotExists($notification); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$message = $notification->{'toTotalVoice'}($notifiable); |
54
|
|
|
|
55
|
|
|
if (is_string($message)) { |
56
|
|
|
$message = new TotalVoiceSmsMessage($message); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (! $message instanceof TotalVoiceMessage) { |
60
|
|
|
throw CouldNotSendNotification::invalidMessageObject($message); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->totalvoice->sendMessage($message, $to); |
64
|
7 |
|
} catch (Exception $exception) { |
65
|
7 |
|
$event = new NotificationFailed($notifiable, $notification, 'totalvoice', ['message' => $exception->getMessage(), 'exception' => $exception]); |
66
|
7 |
|
if (function_exists('event')) { |
67
|
|
|
event($event); |
68
|
7 |
|
} elseif (method_exists($this->events, 'dispacth')) { |
69
|
|
|
$this->events->dispatch($event); |
70
|
|
|
} |
71
|
|
|
} |
72
|
7 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the address to send a notification to. |
76
|
|
|
* |
77
|
|
|
* @param mixed $notifiable |
78
|
|
|
* @return mixed |
79
|
|
|
* @throws CouldNotSendNotification |
80
|
|
|
*/ |
81
|
7 |
|
protected function getTo($notifiable) |
82
|
|
|
{ |
83
|
7 |
|
if ($notifiable->routeNotificationFor('totalVoice')) { |
84
|
1 |
|
return $notifiable->routeNotificationFor('totalVoice'); |
85
|
|
|
} |
86
|
6 |
|
if (isset($notifiable->phone_number)) { |
87
|
5 |
|
return $notifiable->phone_number; |
88
|
|
|
} |
89
|
1 |
|
throw CouldNotSendNotification::invalidReceiver(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|