1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LBHurtado\EngageSpark; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
|
8
|
|
|
class EngageSparkChannel |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
private $mode; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $clientRef; |
15
|
|
|
|
16
|
|
|
/** @var EngageSpark */ |
17
|
|
|
protected $smsc; |
18
|
|
|
|
19
|
|
|
public function __construct(EngageSpark $smsc) |
20
|
|
|
{ |
21
|
|
|
$this->smsc = $smsc; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Send the given notification. |
26
|
|
|
* |
27
|
|
|
* @param mixed $notifiable |
28
|
|
|
* @param Notification $notification |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function send($notifiable, Notification $notification) |
33
|
|
|
{ |
34
|
|
|
if (! ($to = $this->getRecipients($notifiable, $notification))) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$message = $notification->{'toEngageSpark'}($notifiable); |
39
|
|
|
|
40
|
|
|
if (\is_string($message)) { |
41
|
|
|
$message = new EngageSparkMessage($message); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->setClientRef($notification)->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
|
|
|
protected function getRecipients($notifiable, Notification $notification) |
56
|
|
|
{ |
57
|
|
|
$to = $notifiable->routeNotificationFor('engage_spark', $notification); |
58
|
|
|
|
59
|
|
|
if ($to === null || $to === false || $to === '') { |
60
|
|
|
return []; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return \is_array($to) ? $to : [$to]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function sendMessage($recipients, EngageSparkMessage $message) |
67
|
|
|
{ |
68
|
|
|
$this->setMode($message); |
69
|
|
|
|
70
|
|
|
switch ($mode = $this->getMode()) { |
71
|
|
|
case 'sms': |
72
|
|
|
$params = [ |
73
|
|
|
'organization_id' => $this->getOrgId(), |
74
|
|
|
'mobile_numbers' => $recipients, |
75
|
|
|
'message' => $message->content, |
76
|
|
|
'recipient_type' => $message->recipient_type, |
77
|
|
|
'sender_id' => $this->getSenderId($message), |
78
|
|
|
]; |
79
|
|
|
break; |
80
|
|
|
|
81
|
|
|
case 'topup': |
82
|
|
|
$params = [ |
83
|
|
|
'organizationId' => $this->getOrgId(), |
84
|
|
|
'phoneNumber' => Arr::first($recipients), |
85
|
|
|
'maxAmount' => $message->air_time, |
86
|
|
|
'clientRef' => $this->getClientRef(), |
87
|
|
|
]; |
88
|
|
|
break; |
89
|
|
|
|
90
|
|
|
default: |
91
|
|
|
# code... |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// if ($message->sendAt instanceof \DateTimeInterface) { |
96
|
|
|
// $params['time'] = '0'.$message->sendAt->getTimestamp(); |
97
|
|
|
// } |
98
|
|
|
|
99
|
|
|
$this->smsc->send($params, $mode); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getWebHook(EngageSparkMessage $message) |
103
|
|
|
{ |
104
|
|
|
return $this->smsc->getWebHook($message->mode); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function getOrgId() |
108
|
|
|
{ |
109
|
|
|
return $this->smsc->getOrgId(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getClientRef() |
113
|
|
|
{ |
114
|
|
|
return $this->clientRef; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function getSenderId(EngageSparkMessage $message) |
118
|
|
|
{ |
119
|
|
|
return $message->sender_id ? $message->sender_id : $this->smsc->getSenderId(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function setClientRef(Notification $notification) |
123
|
|
|
{ |
124
|
|
|
$this->clientRef = $notification->id; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function getMode() |
130
|
|
|
{ |
131
|
|
|
return $this->mode; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function setMode(EngageSparkMessage $message) |
135
|
|
|
{ |
136
|
|
|
$this->mode = $message->mode; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: