1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coreproc\GlobeLabsSms; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Coreproc\MsisdnPh\Msisdn; |
7
|
|
|
use Coreproc\GlobeLabsSms\Exceptions\CouldNotSendNotification; |
8
|
|
|
|
9
|
|
|
class GlobeLabsSmsMessage |
10
|
|
|
{ |
11
|
|
|
protected $message; |
12
|
|
|
|
13
|
|
|
protected $clientCorrelator; |
14
|
|
|
|
15
|
|
|
protected $accessToken; |
16
|
|
|
|
17
|
|
|
protected $address; |
18
|
|
|
|
19
|
|
|
protected $globeLabsSmsApiSendUrl = 'https://devapi.globelabs.com.ph/smsmessaging/v1/outbound/{senderAddress}/requests'; |
20
|
|
|
|
21
|
|
|
protected $requestParams; |
22
|
|
|
|
23
|
|
|
public static function create($notifiable) |
24
|
|
|
{ |
25
|
|
|
$instance = new static(); |
26
|
|
|
|
27
|
|
|
$info = $notifiable->routeNotificationFor('globeLabsSms'); |
28
|
|
|
|
29
|
|
|
$instance->setAddress($info['address']); |
30
|
|
|
$instance->setAccessToken($info['access_token']); |
31
|
|
|
|
32
|
|
|
return $instance; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getApiSendUrl() |
36
|
|
|
{ |
37
|
|
|
// The URL should have the {senderAddress} and {accessToken} values embedded in the string so we can replace |
38
|
|
|
// this with the correct values. |
39
|
|
|
$url = config('broadcasting.connections.globe_labs_sms.api_send_url', $this->globeLabsSmsApiSendUrl); |
40
|
|
|
|
41
|
|
|
$url = str_replace('{senderAddress}', $this->getSenderAddress(), $url); |
42
|
|
|
|
43
|
|
|
$urlParams = []; |
44
|
|
|
|
45
|
|
|
if (! empty($this->getAccessToken())) { |
46
|
|
|
$urlParams['access_token'] = $this->getAccessToken(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$httpQuery = http_build_query($urlParams); |
50
|
|
|
|
51
|
|
|
if (! empty($httpQuery)) { |
52
|
|
|
$httpQuery = '?'.$httpQuery; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$url = $url.$httpQuery; |
56
|
|
|
|
57
|
|
|
return $url; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getSenderAddress() |
61
|
|
|
{ |
62
|
|
|
return substr(config('broadcasting.connections.globe_labs_sms.short_code'), -4); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getAccessToken() |
66
|
|
|
{ |
67
|
|
|
return $this->accessToken; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setAccessToken($accessToken) |
71
|
|
|
{ |
72
|
|
|
$this->accessToken = $accessToken; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getAddress() |
78
|
|
|
{ |
79
|
|
|
try { |
80
|
|
|
$msisdn = new Msisdn($this->address); |
81
|
|
|
} catch (Exception $e) { |
82
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return 'tel:'.$msisdn->get(true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setAddress($address) |
89
|
|
|
{ |
90
|
|
|
$this->address = $address; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getClientCorrelator() |
96
|
|
|
{ |
97
|
|
|
return $this->clientCorrelator; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setClientCorrelator($clientCorrelator) |
101
|
|
|
{ |
102
|
|
|
$this->clientCorrelator = $clientCorrelator; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getMessage() |
108
|
|
|
{ |
109
|
|
|
return $this->message; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function setMessage($message) |
113
|
|
|
{ |
114
|
|
|
$this->message = $message; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* An additional method to accommodate any additional parameters needed by the API. |
121
|
|
|
* |
122
|
|
|
* @param $requestParams |
123
|
|
|
* @return GlobeLabsSmsMessage |
124
|
|
|
*/ |
125
|
|
|
public function setRequestParams($requestParams) |
126
|
|
|
{ |
127
|
|
|
$this->requestParams = $requestParams; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getRequestParams() |
133
|
|
|
{ |
134
|
|
|
return $this->requestParams; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function toJson() |
138
|
|
|
{ |
139
|
|
|
$request['outboundSMSMessageRequest']['address'] = $this->getAddress(); |
|
|
|
|
140
|
|
|
$request['outboundSMSMessageRequest']['senderAddress'] = $this->getSenderAddress(); |
141
|
|
|
|
142
|
|
|
if (! empty($this->getClientCorrelator())) { |
143
|
|
|
$request['outboundSMSMessageRequest']['clientCorrelator'] = $this->getClientCorrelator(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$request['outboundSMSMessageRequest']['outboundSMSTextMessage']['message'] = $this->getMessage(); |
147
|
|
|
|
148
|
|
|
if (! empty($this->getRequestParams())) { |
149
|
|
|
$request = array_merge($request, $this->getRequestParams()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return json_encode($request); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.