1 | <?php |
||
2 | |||
3 | namespace NotificationChannels\Sipgate; |
||
4 | |||
5 | use Illuminate\Notifications\Notification; |
||
6 | use NotificationChannels\Sipgate\Exceptions\CouldNotSendNotification; |
||
7 | |||
8 | class SipgateChannel |
||
9 | { |
||
10 | /** |
||
11 | * @var SipgateClient |
||
12 | */ |
||
13 | protected $client; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $smsId; |
||
19 | |||
20 | /** |
||
21 | * @var bool |
||
22 | */ |
||
23 | protected $channelEnabled; |
||
24 | |||
25 | 8 | public function __construct(SipgateClient $client, string $smsId, bool $channelEnabled) |
|
26 | { |
||
27 | 8 | $this->client = $client; |
|
28 | 8 | $this->smsId = $smsId; |
|
29 | 8 | $this->channelEnabled = $channelEnabled; |
|
30 | 8 | } |
|
31 | |||
32 | /** |
||
33 | * Send the given notification. |
||
34 | * |
||
35 | * @param mixed $notifiable |
||
36 | * @param Notification $notification |
||
37 | * |
||
38 | * @throws CouldNotSendNotification |
||
39 | */ |
||
40 | 8 | public function send($notifiable, Notification $notification) |
|
41 | { |
||
42 | 8 | if (! $this->channelEnabled) { |
|
43 | 1 | return; |
|
44 | } |
||
45 | |||
46 | /** @var SipgateMessage $message */ |
||
47 | 7 | $message = $notification->toSipgate($notifiable); |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
48 | |||
49 | 7 | $this->addRecipient($message, $notifiable); |
|
50 | |||
51 | 6 | $this->addSmsId($message); |
|
52 | |||
53 | 6 | $this->client->send($message); |
|
54 | 6 | } |
|
55 | |||
56 | /** |
||
57 | * @param SipgateMessage $message |
||
58 | * @param $notifiable |
||
59 | * @throws CouldNotSendNotification |
||
60 | */ |
||
61 | 7 | protected function addRecipient(SipgateMessage $message, $notifiable) |
|
62 | { |
||
63 | 7 | if ($message->getRecipient()) { |
|
64 | 1 | return; |
|
65 | } |
||
66 | |||
67 | 6 | if ($recipient = $notifiable->routeNotificationFor('sipgate', $notifiable)) { |
|
68 | 5 | $message->recipient($recipient); |
|
69 | |||
70 | 5 | return; |
|
71 | } |
||
72 | |||
73 | 1 | throw CouldNotSendNotification::noRecipient(); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param SipgateMessage $message |
||
78 | */ |
||
79 | 6 | protected function addSmsId(SipgateMessage $message) |
|
80 | { |
||
81 | 6 | if ($message->getSmsId()) { |
|
82 | 1 | return; |
|
83 | } |
||
84 | |||
85 | 5 | $message->smsId($this->smsId); |
|
86 | 5 | } |
|
87 | } |
||
88 |