1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\SmscRu; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use NotificationChannels\SmscRu\Exceptions\CouldNotSendNotification; |
8
|
|
|
|
9
|
|
|
class SmscRuChannel |
10
|
|
|
{ |
11
|
|
|
/** @var \NotificationChannels\SmscRu\SmscRuApi */ |
12
|
|
|
protected $smsc; |
13
|
|
|
|
14
|
|
|
public function __construct(SmscRuApi $smsc) |
15
|
|
|
{ |
16
|
|
|
$this->smsc = $smsc; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Send the given notification. |
21
|
|
|
* |
22
|
|
|
* @param mixed $notifiable |
23
|
|
|
* @param Notification $notification |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public function send($notifiable, Notification $notification) |
28
|
|
|
{ |
29
|
|
|
if (! ($to = $this->getRecipients($notifiable, $notification))) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$message = $notification->{'toSmscRu'}($notifiable); |
34
|
|
|
|
35
|
|
|
if (\is_string($message)) { |
36
|
|
|
$message = new SmscRuMessage($message); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->sendMessage($to, $message); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Gets a list of phones from the given notifiable. |
44
|
|
|
* |
45
|
|
|
* @param mixed $notifiable |
46
|
|
|
* @param Notification $notification |
47
|
|
|
* |
48
|
|
|
* @return string[] |
49
|
|
|
*/ |
50
|
|
|
protected function getRecipients($notifiable, Notification $notification) |
51
|
|
|
{ |
52
|
|
|
$to = $notifiable->routeNotificationFor('smscru', $notification); |
53
|
|
|
|
54
|
|
|
if ($to === null || $to === false || $to === '') { |
55
|
|
|
return []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return Arr::wrap($to); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function sendMessage($recipients, SmscRuMessage $message) |
62
|
|
|
{ |
63
|
|
|
if (\mb_strlen($message->content) > 800) { |
64
|
|
|
throw CouldNotSendNotification::contentLengthLimitExceeded(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$params = [ |
68
|
|
|
'phones' => \implode(',', $recipients), |
69
|
|
|
'mes' => $message->content, |
70
|
|
|
'sender' => $message->from, |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
if ($message->sendAt instanceof \DateTimeInterface) { |
74
|
|
|
$params['time'] = '0'.$message->sendAt->getTimestamp(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->smsc->send($params); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|