1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\ZonerSmsGateway; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification; |
6
|
|
|
use NotificationChannels\ZonerSmsGateway\Exceptions\ZonerSmsGatewayException; |
7
|
|
|
|
8
|
|
|
class ZonerSmsGatewayChannel |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ZonerSmsGateway |
12
|
|
|
*/ |
13
|
|
|
protected $gateway; |
14
|
|
|
|
15
|
11 |
|
public function __construct(ZonerSmsGateway $gateway) |
16
|
|
|
{ |
17
|
11 |
|
$this->gateway = $gateway; |
18
|
11 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Send the given notification. |
22
|
|
|
* |
23
|
|
|
* @param mixed $notifiable |
24
|
|
|
* @param Notification $notification |
25
|
|
|
* |
26
|
|
|
* @return tracking number |
27
|
|
|
* @throws ZonerSmsGatewayException |
28
|
|
|
*/ |
29
|
11 |
|
public function send($notifiable, Notification $notification) |
30
|
|
|
{ |
31
|
11 |
|
$message = $notification->toZonerSmsGateway($notifiable); |
32
|
|
|
|
33
|
11 |
|
if (is_string($message)) { |
34
|
|
|
$message = new ZonerSmsGatewayMessage($message); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Use the receiver from message, if defined: |
38
|
11 |
|
$receiver = $message->receiver; |
39
|
|
|
|
40
|
|
|
// Otherwise use the receiver given by notifiable routeNotificationForZonerSmsGateway: |
41
|
11 |
|
if (empty($receiver) && method_exists($notifiable, 'routeNotificationForZonerSmsGateway')) { |
42
|
8 |
|
$receiver = $notifiable->routeNotificationForZonerSmsGateway(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Otherwise use the receiver given by notifiable generic routing method: |
46
|
11 |
|
if (empty($receiver)) { |
47
|
2 |
|
$receiver = $notifiable->routeNotificationFor(self::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// As the last resort, try to get the phone_number attribute from notifiable: |
51
|
11 |
|
if (empty($receiver) && isset($notifiable->phone_number)) { |
52
|
1 |
|
$receiver = $notifiable->phone_number; |
53
|
|
|
} |
54
|
|
|
|
55
|
11 |
|
if (empty($receiver)) { |
56
|
1 |
|
return; // Give up |
57
|
|
|
} |
58
|
|
|
|
59
|
10 |
|
return $this->gateway->sendMessage($receiver, trim($message->content), $message->sender); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|