|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gabievi\LaravelSMSOffice; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification; |
|
6
|
|
|
use Gabievi\LaravelSMSOffice\Exceptions\MissingRecipient; |
|
7
|
|
|
|
|
8
|
|
|
class SMSOfficeChannel |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var \Gabievi\LaravelSMSOffice\SMSOffice */ |
|
11
|
|
|
protected $smsoffice; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* SMSOfficeChannel constructor. |
|
15
|
|
|
* @param SMSOffice $smsoffice |
|
16
|
|
|
*/ |
|
17
|
2 |
|
public function __construct(SMSOffice $smsoffice) |
|
18
|
|
|
{ |
|
19
|
2 |
|
$this->smsoffice = $smsoffice; |
|
20
|
2 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Send the given notification. |
|
24
|
|
|
* |
|
25
|
|
|
* @param mixed $notifiable |
|
26
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
27
|
|
|
* @throws MissingRecipient |
|
28
|
|
|
*/ |
|
29
|
2 |
|
public function send($notifiable, Notification $notification) |
|
30
|
|
|
{ |
|
31
|
2 |
|
$to = $notifiable->routeNotificationFor('smsoffice'); |
|
32
|
|
|
|
|
33
|
2 |
|
if (empty($to)) { |
|
34
|
1 |
|
throw new MissingRecipient; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
$message = $notification->toSMSOffice($notifiable); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
1 |
|
if (is_string($message)) { |
|
40
|
|
|
$message = new SMSOfficeMessage($message); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
$this->sendMessage($to, $message); |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $recipient |
|
48
|
|
|
* @param SMSOfficeMessage $message |
|
49
|
|
|
*/ |
|
50
|
1 |
|
protected function sendMessage($recipient, SMSOfficeMessage $message) |
|
51
|
|
|
{ |
|
52
|
1 |
|
if (strpos($recipient, '+') === 0) { |
|
53
|
1 |
|
$recipient = substr($recipient, 1); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
if (strpos($recipient, '00') === 0) { |
|
57
|
1 |
|
$recipient = substr($recipient, 2); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$params = [ |
|
61
|
1 |
|
'destination' => urlencode($recipient), |
|
62
|
1 |
|
'content' => $message->content, |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
1 |
|
$this->smsoffice->send($params); |
|
66
|
1 |
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|