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?access_token={accessToken}'; |
20
|
|
|
|
21
|
|
|
public static function create($notifiable) |
22
|
|
|
{ |
23
|
|
|
$instance = new static(); |
24
|
|
|
|
25
|
|
|
$info = $notifiable->routeNotificationFor('globeLabsSms'); |
26
|
|
|
|
27
|
|
|
$instance->setAddress($info['address']); |
28
|
|
|
$instance->setAccessToken($info['access_token']); |
29
|
|
|
|
30
|
|
|
return $instance; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getApiSendUrl() |
34
|
|
|
{ |
35
|
|
|
// The URL should have the {senderAddress} and {accessToken} values embedded in the string so we can replace |
36
|
|
|
// this with the correct values. |
37
|
|
|
$url = config('broadcasting.connections.globe_labs_sms.api_send_url', $this->globeLabsSmsApiSendUrl); |
38
|
|
|
|
39
|
|
|
$url = str_replace('{senderAddress}', $this->getSenderAddress(), $url); |
40
|
|
|
$url = str_replace('{accessToken}', $this->getAccessToken(), $url); |
41
|
|
|
|
42
|
|
|
return $url; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getSenderAddress() |
46
|
|
|
{ |
47
|
|
|
return substr(config('broadcasting.connections.globe_labs_sms.short_code'), -4); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getAccessToken() |
51
|
|
|
{ |
52
|
|
|
return $this->accessToken; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setAccessToken($accessToken) |
56
|
|
|
{ |
57
|
|
|
$this->accessToken = $accessToken; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getAddress() |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
$msisdn = new Msisdn($this->address); |
66
|
|
|
} catch (Exception $e) { |
67
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return 'tel:'.$msisdn->get(true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setAddress($address) |
74
|
|
|
{ |
75
|
|
|
$this->address = $address; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getClientCorrelator() |
81
|
|
|
{ |
82
|
|
|
return $this->clientCorrelator; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setClientCorrelator($clientCorrelator) |
86
|
|
|
{ |
87
|
|
|
$this->clientCorrelator = $clientCorrelator; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getMessage() |
93
|
|
|
{ |
94
|
|
|
return $this->message; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setMessage($message) |
98
|
|
|
{ |
99
|
|
|
$this->message = $message; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function toJson() |
105
|
|
|
{ |
106
|
|
|
$request['outboundSMSMessageRequest']['address'] = $this->getAddress(); |
|
|
|
|
107
|
|
|
$request['outboundSMSMessageRequest']['senderAddress'] = $this->getSenderAddress(); |
108
|
|
|
|
109
|
|
|
if (! empty($this->getClientCorrelator())) { |
110
|
|
|
$request['outboundSMSMessageRequest']['clientCorrelator'] = $this->getClientCorrelator(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$request['outboundSMSMessageRequest']['outboundSMSTextMessage']['message'] = $this->getMessage(); |
114
|
|
|
|
115
|
|
|
return json_encode($request); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.