1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Simonkub\Laravel\Notifications\Sipgate; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification; |
6
|
|
|
use Simonkub\Laravel\Notifications\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
|
7 |
|
public function __construct(SipgateClient $client, string $smsId) |
21
|
|
|
{ |
22
|
7 |
|
$this->client = $client; |
23
|
7 |
|
$this->smsId = $smsId; |
24
|
7 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Send the given notification. |
28
|
|
|
* |
29
|
|
|
* @param mixed $notifiable |
30
|
|
|
* @param Notification $notification |
31
|
|
|
* |
32
|
|
|
* @throws CouldNotSendNotification |
33
|
|
|
*/ |
34
|
7 |
|
public function send($notifiable, Notification $notification) |
35
|
|
|
{ |
36
|
|
|
/** @var SipgateMessage $message */ |
37
|
7 |
|
$message = $notification->toSipgate($notifiable); |
38
|
|
|
|
39
|
7 |
|
$this->addRecipient($message, $notifiable); |
40
|
|
|
|
41
|
6 |
|
$this->addSmsId($message); |
42
|
|
|
|
43
|
6 |
|
$this->client->send($message); |
44
|
6 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param SipgateMessage $message |
48
|
|
|
* @param $notifiable |
49
|
|
|
* @throws CouldNotSendNotification |
50
|
|
|
*/ |
51
|
7 |
|
protected function addRecipient(SipgateMessage $message, $notifiable) |
52
|
|
|
{ |
53
|
7 |
|
if ($message->getRecipient()) { |
54
|
1 |
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
if ($recipient = $notifiable->routeNotificationFor('sipgate', $notifiable)) { |
58
|
5 |
|
$message->recipient($recipient); |
59
|
|
|
|
60
|
5 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
throw CouldNotSendNotification::noRecipient(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param SipgateMessage $message |
68
|
|
|
*/ |
69
|
6 |
|
protected function addSmsId(SipgateMessage $message) |
70
|
|
|
{ |
71
|
6 |
|
if ($message->getSmsId()) { |
72
|
1 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
$message->smsId($this->smsId); |
76
|
5 |
|
} |
77
|
|
|
} |
78
|
|
|
|