Completed
Push — master ( 2e0c76...7c4a20 )
by Vincent
12:05
created

SMS::getBalance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * BongaTech SMS Client Library for PHP.
4
 *
5
 * @copyright Copyright (c) 2017
6
 * @author   Vincent Mosoti <[email protected]>
7
 * @license https://github.com/VMosoti/bongatech-sms/blob/master/LICENSE
8
 */
9
10
namespace VMosoti\BongaTech;
11
12
use VMosoti\BongaTech\Exceptions\BongaTechException;
13
14
/**
15
 * Class SMS
16
 */
17
class SMS
18
{
19
    /**
20
     * Version number of the SMS API.
21
     *
22
     * @var string
23
     */
24
    const VERSION = '1.0.0';
25
26
    /**
27
     * token is generated by md5(password).
28
     *
29
     * @var string
30
     */
31
    protected $token;
32
33
    /**
34
     * timestamp is the current datetime.
35
     *
36
     * @var string
37
     */
38
    protected $timestamp;
39
40
    /**
41
     * type of SMS to sent.
42
     *
43
     * @var int message
44
     */
45
    protected $message_type;
46
47
    /**
48
     * batch type of the SMS being sent.
49
     *
50
     * @var int batch
51
     */
52
    protected $batch_type;
53
54
    /**
55
     * sms configurations.
56
     *
57
     * @array config
58
     */
59
    protected $config;
60
61
    /**
62
     * end point url.
63
     *
64
     * @string endpoint
65
     */
66
    protected $endpoint;
67
68
    /**
69
     * the message being sent (array of messages in case message is different for each user.
70
     *
71
     * @var string/array.
72
     */
73
    protected $message;
74
75
    /**
76
     * the recipients .
77
     *
78
     * @var string/array.
79
     */
80
    protected $recipients;
81
82
    /**
83
     * SMS constructor.
84
     */
85
    public function __construct()
86
    {
87
        $this->config = Config::get();
88
        $this->setTimestamp();
89
        $this->setToken();
90
    }
91
92
    /**
93
     * set the timestamp.
94
     */
95
    private function setTimestamp()
96
    {
97
        $this->timestamp = date('YmdHis');
98
    }
99
100
    /**
101
     * set the token.
102
     */
103
    private function setToken()
104
    {
105
        $this->token = md5($this->config['password']);
106
    }
107
108
    /**
109
     * invoke if SMS being sent is of type subscribable.
110
     *
111
     * @return $this
112
     */
113
    public function messageTypeSubscribable()
114
    {
115
        $this->message_type = MessageType::SUBSCIBABLE;
116
117
        return $this;
118
    }
119
120
    /**
121
     * invoke if SMS being sent is of type on demand.
122
     *
123
     * @return $this
124
     */
125
    public function messageTypeOnDemand()
126
    {
127
        $this->message_type = MessageType::ON_DEMAND;
128
129
        return $this;
130
    }
131
132
    /**
133
     * invoke if SMS being sent is of type bulk SMS. This will be the common one.
134
     *
135
     * @return $this
136
     */
137
    public function messageTypeBulk()
138
    {
139
        $this->message_type = MessageType::BULK;
140
141
        return $this;
142
    }
143
144
    /**
145
     * invoke if SMS is being sent to a single recipient.
146
     *
147
     * @return $this
148
     */
149
    public function batchTypeNoBatch()
150
    {
151
152
        $this->batch_type = BatchType::NOT_BATCH;
153
154
        return $this;
155
    }
156
157
    /**
158
     * invoke if SMS is being sent to a different recipients.
159
     *
160
     * @return $this
161
     */
162
    public function batchTypeSameMessage()
163
    {
164
        $this->batch_type = BatchType::SAME_MESSAGE;
165
166
        return $this;
167
    }
168
169
    /**
170
     * invoke if each recipient will receive a different message.
171
     *
172
     * @return $this
173
     */
174
    public function batchTypeDifferentMessages()
175
    {
176
        $this->batch_type = BatchType::DIFFERENT_MESSAGE;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @param $recipients
183
     * @param  $message
184
     * @throws BongaTechException
185
     * @return  string response
186
     */
187
    public function send($recipients, $message)
188
    {
189
        $this->recipients = $recipients;
190
        $this->message = $message;
191
        $this->endpoint = $this->config['base_url'] . $this->config['sms_endpoint'];
192
        $response = '';
193
194
195
        if ($this->batch_type === BatchType::NOT_BATCH) {
196
197
            $response = $this->sendForNoBatch();
198
199
        } elseif ($this->batch_type === BatchType::SAME_MESSAGE) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
200
201
        } elseif ($this->batch_type === BatchType::DIFFERENT_MESSAGE) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
202
203
        } else {
204
205
            throw new BongaTechException('Message Batch Type has not been set.');
206
        }
207
208
        return $response;
209
210
    }
211
212
    /**
213
     * send a message to a single recipient.
214
     *
215
     * @return Response
216
     */
217
    private function sendForNoBatch()
218
    {
219
        $headers = array(
220
            'Accept' => 'application/json'
221
        );
222
223
        $body = array(
224
            'AuthDetails' => array(
225
                array(
226
                    'UserID' => $this->config['user_id'],
227
                    'Token' => $this->token,
228
                    'Timestamp' => $this->timestamp
229
230
                )
231
            ),
232
            'MessageType' => array(
233
                (string)$this->message_type
234
            ),
235
            'BatchType' => array(
236
                (string)$this->batch_type
237
            ),
238
            'SourceAddr' => array(
239
                (string)$this->config['sender_id']
240
            ),
241
            'MessagePayload' => array(
242
                array(
243
                    'Text' => $this->message
244
                )
245
            ),
246
            'DestinationAddr' => array(
247
                array(
248
                    'MSISDN' => $this->recipients,
249
                    'LinkID' => '',
250
                    'SourceID' => rand(2, 9)
251
                )
252
            ),
253
            'DeliveryRequest' => array(
254
                array(
255
                    'EndPoint' => $this->config['callback_url'],
256
                    'Correlator' => mt_rand()
257
                )
258
            )
259
        );
260
261
        $request = new Request($this->endpoint, $headers, $body);
262
        $response = $request->send();
263
264
        return new Response($response->body[0]);
265
266
    }
267
268
    /**
269
     * send same message to different recipients.
270
     *
271
     * @param Request $request
272
     * @return request
273
     */
274
    private function sendForSameMessage(Request $request)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
275
    {
276
        return $request;
277
    }
278
279
    /**
280
     * send different message to different recipients.
281
     *
282
     * @param Request $request
283
     */
284
    private function sendForDifferentMessages(Request $request)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
285
    {
286
    }
287
288
    public static function getBalance()
289
    {
290
291
        $config = Config::get();
292
293
        $endpoint = $config['base_url'] . $config['balance_endpoint'] . '?UserID=' . $config['user_id'] . '&Token=' . md5($config['password']);
294
295
        $request = new Request($endpoint);
296
        $response = $request->getBalance();
297
298
        return new Response($response->body);
299
    }
300
301
302
}
303