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