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 | 3 | public function __construct(SMSOffice $smsoffice) |
|
18 | { |
||
19 | 3 | $this->smsoffice = $smsoffice; |
|
20 | 3 | } |
|
21 | |||
22 | /** |
||
23 | * Send the given notification. |
||
24 | * |
||
25 | * @param mixed $notifiable |
||
26 | * @param \Illuminate\Notifications\Notification $notification |
||
27 | * @throws MissingRecipient |
||
28 | */ |
||
29 | 3 | public function send($notifiable, Notification $notification) |
|
30 | { |
||
31 | 3 | $to = $notifiable->routeNotificationFor('smsoffice'); |
|
32 | |||
33 | 3 | if (empty($to)) { |
|
34 | 1 | throw new MissingRecipient; |
|
35 | } |
||
36 | |||
37 | 2 | $message = $notification->toSMSOffice($notifiable); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
38 | |||
39 | 2 | if (is_string($message)) { |
|
40 | 1 | $message = new SMSOfficeMessage($message); |
|
41 | } |
||
42 | |||
43 | 2 | $this->sendMessage($to, $message); |
|
44 | 2 | } |
|
45 | |||
46 | /** |
||
47 | * @param $recipient |
||
48 | * @param SMSOfficeMessage $message |
||
49 | */ |
||
50 | 2 | protected function sendMessage($recipient, SMSOfficeMessage $message) |
|
51 | { |
||
52 | 2 | if (strpos($recipient, '+') === 0) { |
|
53 | 2 | $recipient = substr($recipient, 1); |
|
54 | } |
||
55 | |||
56 | 2 | if (strpos($recipient, '00') === 0) { |
|
57 | 2 | $recipient = substr($recipient, 2); |
|
58 | } |
||
59 | |||
60 | $params = [ |
||
61 | 2 | 'destination' => urlencode($recipient), |
|
62 | 2 | 'content' => $message->content, |
|
63 | ]; |
||
64 | |||
65 | 2 | $this->smsoffice->send($params); |
|
66 | 2 | } |
|
67 | } |
||
68 |