1
|
|
|
<?php |
2
|
|
|
namespace DexBarrett\ClockworkSms; |
3
|
|
|
|
4
|
|
|
use DexBarrett\ClockworkSms\Commands\CommandFactory; |
5
|
|
|
use DexBarrett\ClockworkSms\Exception\ClockworkSmsException; |
6
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
7
|
|
|
|
8
|
|
|
class ClockworkSms |
9
|
|
|
{ |
10
|
|
|
const MESSAGE_LIMIT = 500; |
11
|
|
|
|
12
|
|
|
private $apiKey; |
13
|
|
|
private $httpClient; |
14
|
|
|
private $commandFactory; |
15
|
|
|
private $format = 'xml'; |
16
|
|
|
|
17
|
|
|
private $base = 'api.clockworksms.com'; |
18
|
|
|
|
19
|
|
|
private $contentTypes = [ |
20
|
|
|
'xml' => 'text/xml' |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
private $options = [ |
24
|
|
|
'ssl' => true, |
25
|
|
|
'from' => null, |
26
|
|
|
'long' => null, |
27
|
|
|
'truncate' => null, |
28
|
|
|
'invalidCharAction' => null, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
private $requiredParams = [ |
32
|
|
|
'send' => ['to', 'message'] |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
private $validParams = [ |
36
|
|
|
'send' => ['to', 'message', 'from', 'long', 'truncate', 'invalidCharAction'] |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
|
|
$apiKey = null, |
41
|
|
|
array $options = [], |
42
|
|
|
GuzzleClient $httpClient = null, |
43
|
|
|
CommandFactory $commandFactory = null |
44
|
|
|
) { |
45
|
|
|
if ($apiKey === null) { |
46
|
|
|
throw new ClockworkSmsException('No API key provided'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->apiKey = $apiKey; |
50
|
|
|
$this->httpClient = $httpClient ?: new GuzzleClient; |
51
|
|
|
$this->commandFactory = $commandFactory ?: new CommandFactory; |
52
|
|
|
|
53
|
|
|
$this->parseOptions($options); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function checkBalance() |
57
|
|
|
{ |
58
|
|
|
return $this->sendRequest('balance'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function send(array $messages) |
62
|
|
|
{ |
63
|
|
|
$messages = ($this->containsMultipleMessages($messages))? $messages : [$messages]; |
64
|
|
|
|
65
|
|
|
$this->validateMessages($messages); |
66
|
|
|
|
67
|
|
|
return $this->sendRequest('send', $this->mergeMessageOptions($messages)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getOptionValue($optionName) |
71
|
|
|
{ |
72
|
|
|
$optionName = strtolower($optionName); |
73
|
|
|
|
74
|
|
|
if (! array_key_exists($optionName, $this->options)) { |
75
|
|
|
throw new ClockworkSmsException('Option does not exist'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->options[$optionName]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function sendRequest($endpoint, $data = []) |
82
|
|
|
{ |
83
|
|
|
$requestUrl = $this->buildApiEndpointUrl($endpoint); |
84
|
|
|
|
85
|
|
|
$command = $this->commandFactory->createCommand( |
86
|
|
|
$endpoint, |
87
|
|
|
$this->apiKey, |
88
|
|
|
$this->getFormat(), |
89
|
|
|
$data |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$response = $this->httpClient->request('POST', $requestUrl, [ |
93
|
|
|
'headers' => [ |
94
|
|
|
'Content-Type' => $this->getContentType() |
95
|
|
|
], |
96
|
|
|
'body' => $command->serialize() |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
return $command->deserialize((string)$response->getBody()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
protected function parseOptions(array $options) |
105
|
|
|
{ |
106
|
|
|
$normalizedOptions = array_change_key_case($options, CASE_LOWER); |
107
|
|
|
|
108
|
|
|
$this->options = array_merge( |
109
|
|
|
$this->options, |
110
|
|
|
array_intersect_key($normalizedOptions, $this->options) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function getContentType() |
115
|
|
|
{ |
116
|
|
|
return $this->contentTypes[$this->format]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function getFormat() |
120
|
|
|
{ |
121
|
|
|
return $this->format; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function containsMultipleMessages($messages) |
125
|
|
|
{ |
126
|
|
|
return count(array_filter($messages, 'is_array')) > 0; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function buildApiEndpointUrl($endpoint) |
130
|
|
|
{ |
131
|
|
|
return sprintf( |
132
|
|
|
'%s://%s/%s/%s', |
133
|
|
|
($this->getOptionValue('ssl'))? 'https':'http', |
134
|
|
|
$this->base, |
135
|
|
|
$this->format, |
136
|
|
|
$endpoint |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function validateParameters($method, $parameters) |
141
|
|
|
{ |
142
|
|
|
$missingParameters = array_diff( |
143
|
|
|
$this->requiredParams[$method], |
144
|
|
|
array_keys($parameters) |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
if (count($missingParameters)) { |
148
|
|
|
throw new ClockworkSmsException( |
149
|
|
|
"the following parameters are missing in call to method '{$method}': " |
150
|
|
|
. implode(', ', $missingParameters) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function validateMessages($messages) |
156
|
|
|
{ |
157
|
|
|
if ($this->exceedsMessageLimit($messages)) { |
158
|
|
|
throw new ClockworkSmsException(sprintf( |
159
|
|
|
'Please call the send method with a maximum of %d messages', |
160
|
|
|
self::MESSAGE_LIMIT |
161
|
|
|
)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
foreach ($messages as $message) { |
165
|
|
|
$this->validateParameters('send', $message); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function exceedsMessageLimit($messages) |
170
|
|
|
{ |
171
|
|
|
return count($messages) > self::MESSAGE_LIMIT; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
protected function mergeMessageOptions(array $messages) |
175
|
|
|
{ |
176
|
|
|
$mergedMessages = []; |
177
|
|
|
|
178
|
|
|
$globalMessageOptions = array_filter( |
179
|
|
|
$this->options, |
180
|
|
|
function ($value, $key) { |
181
|
|
|
return $key != 'ssl'; |
182
|
|
|
}, |
183
|
|
|
ARRAY_FILTER_USE_BOTH |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
foreach ($messages as $message) { |
187
|
|
|
$validParams = array_intersect_key( |
188
|
|
|
$message, |
189
|
|
|
array_flip($this->validParams['send']) |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$mergedMessages[] = array_filter( |
193
|
|
|
array_merge($globalMessageOptions, $validParams), |
194
|
|
|
function ($value) { |
195
|
|
|
return $value !== null; |
196
|
|
|
} |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $mergedMessages; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|