1 | <?php |
||
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 | |||
64 | $messages = ($this->containsMultipleMessages($messages))? $messages : [$messages]; |
||
65 | |||
66 | $this->validateMessages($messages); |
||
67 | |||
68 | return $this->sendRequest('send', $this->mergeMessageOptions($messages)); |
||
69 | } |
||
70 | |||
71 | public function getOptionValue($optionName) |
||
81 | |||
82 | protected function sendRequest($endpoint, $data = []) |
||
83 | { |
||
84 | |||
104 | |||
105 | |||
106 | protected function parseOptions(array $options) |
||
115 | |||
116 | protected function getContentType() |
||
120 | |||
121 | protected function getFormat() |
||
125 | |||
126 | private function containsMultipleMessages($messages) |
||
130 | |||
131 | protected function buildApiEndpointUrl($endpoint) |
||
141 | |||
142 | protected function validateParameters($method, $parameters) |
||
156 | |||
157 | protected function validateMessages($messages) |
||
170 | |||
171 | protected function exceedsMessageLimit($messages) |
||
175 | |||
176 | protected function mergeMessageOptions(array $messages) |
||
204 | } |
||
205 |