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
|
4 |
|
public function __construct(SmscRuApi $smsc) |
14
|
|
|
{ |
15
|
4 |
|
$this->smsc = $smsc; |
16
|
4 |
|
} |
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
|
4 |
|
public function send($notifiable, Notification $notification): ?array |
29
|
|
|
{ |
30
|
4 |
|
if (! ($to = $this->getRecipients($notifiable, $notification))) { |
31
|
1 |
|
return null; |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
$message = $notification->{'toSmscRu'}($notifiable); |
35
|
|
|
|
36
|
3 |
|
if (\is_string($message)) { |
37
|
|
|
$message = new SmscRuMessage($message); |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
return $this->sendMessage($to, $message); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets a list of phones from the given notifiable. |
45
|
|
|
* |
46
|
|
|
* @param mixed $notifiable |
47
|
|
|
* @param Notification $notification |
48
|
|
|
* |
49
|
|
|
* @return string[] |
50
|
|
|
*/ |
51
|
4 |
|
protected function getRecipients($notifiable, Notification $notification): array |
52
|
|
|
{ |
53
|
4 |
|
$to = $notifiable->routeNotificationFor('smscru', $notification); |
54
|
|
|
|
55
|
4 |
|
if (empty($to)) { |
56
|
1 |
|
return []; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
return \is_array($to) ? $to : [$to]; |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
protected function sendMessage($recipients, SmscRuMessage $message) |
63
|
|
|
{ |
64
|
3 |
|
if (\mb_strlen($message->content) > 800) { |
65
|
|
|
throw CouldNotSendNotification::contentLengthLimitExceeded(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$params = [ |
69
|
3 |
|
'phones' => \implode(',', $recipients), |
70
|
3 |
|
'mes' => $message->content, |
71
|
3 |
|
'sender' => $message->from, |
72
|
|
|
]; |
73
|
|
|
|
74
|
3 |
|
if ($message->sendAt instanceof \DateTimeInterface) { |
75
|
1 |
|
$params['time'] = '0'.$message->sendAt->getTimestamp(); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
return $this->smsc->send($params); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|