Completed
Push — master ( 09e519...2e0c76 )
by Vincent
01:56
created

SMS::batchTypeNoBatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
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
     * user ID registered in Bongatech.
28
     *
29
     * @var string
30
     */
31
    protected $user_id;
32
33
    /**
34
     * password for Bongatech.
35
     *
36
     * @var string
37
     */
38
    protected $password;
39
40
    /**
41
     * token is generated by md5(password).
42
     *
43
     * @var string
44
     */
45
    protected $token;
46
47
    /**
48
     * timestamp is the current datetime.
49
     *
50
     * @var string
51
     */
52
    protected $timestamp;
53
54
    /**
55
     * type of SMS to sent.
56
     *
57
     * @var int message
58
     */
59
    protected $message_type;
60
61
    /**
62
     * batch type of the SMS being sent.
63
     *
64
     * @var int batch
65
     */
66
    protected $batch_type;
67
68
    /**
69
     * the alphanumeric of the sender.
70
     *
71
     * @var string
72
     */
73
    protected $sender_id;
74
75
    /**
76
     * the url on which delivery reports will be sent.
77
     *
78
     * @var string callback_url.
79
     */
80
    protected $callback_url;
81
82
    /**
83
     * recipient/recipients of the SMS being sent.
84
     * @var string/array.
85
     */
86
    protected $recipients;
87
88
    /**
89
     * the message being sent (array of messages in case message is different for each user.
90
     *
91
     * @var string/array.
92
     */
93
    protected $message;
94
95
    /**
96
     * Bongatech end point.
97
     *
98
     * @var string end point.
99
     */
100
    protected $endpoint = 'http://197.248.2.55/smsapi_test/submit.php';
101
102
    /**
103
     * SMS constructor.
104
     *
105
     * @param string $user_id
106
     * @param string $password
107
     * @param string $sender_id
108
     * @param string $callback_url
109
     */
110
    public function __construct($user_id, $password, $sender_id, $callback_url)
111
    {
112
        $this->user_id = $user_id;
113
        $this->password = $password;
114
        $this->sender_id = $sender_id;
115
        $this->callback_url = $callback_url;
116
        $this->setTimestamp();
117
        $this->setToken();
118
    }
119
120
    /**
121
     * set the timestamp.
122
     */
123
    private function setTimestamp()
124
    {
125
        $this->timestamp = date('YmdHis');
126
    }
127
128
    /**
129
     * set the token.
130
     */
131
    private function setToken()
132
    {
133
        $this->token = md5($this->password);
134
    }
135
136
    /**
137
     * invoke if SMS being sent is of type subscribable.
138
     *
139
     * @return $this
140
     */
141
    public function messageTypeSubscribable()
142
    {
143
        $this->message_type = MessageType::SUBSCIBABLE;
144
145
        return $this;
146
    }
147
148
    /**
149
     * invoke if SMS being sent is of type on demand.
150
     *
151
     * @return $this
152
     */
153
    public function messageTypeOnDemand()
154
    {
155
        $this->message_type = MessageType::ON_DEMAND;
156
157
        return $this;
158
    }
159
160
    /**
161
     * invoke if SMS being sent is of type bulk SMS. This will be the common one.
162
     *
163
     * @return $this
164
     */
165
    public function messageTypeBulk()
166
    {
167
        $this->message_type = MessageType::BULK;
168
169
        return $this;
170
    }
171
172
    /**
173
     * invoke if SMS is being sent to a single recipient.
174
     *
175
     * @return $this
176
     */
177
    public function batchTypeNoBatch()
178
    {
179
180
        $this->batch_type = BatchType::NOT_BATCH;
181
182
        return $this;
183
    }
184
185
    /**
186
     * invoke if SMS is being sent to a different recipients.
187
     *
188
     * @return $this
189
     */
190
    public function batchTypeSameMessage()
191
    {
192
        $this->batch_type = BatchType::SAME_MESSAGE;
193
194
        return $this;
195
    }
196
197
    /**
198
     * invoke if each recipient will receive a different message.
199
     *
200
     * @return $this
201
     */
202
    public function batchTypeDifferentMessages()
203
    {
204
        $this->batch_type = BatchType::DIFFERENT_MESSAGE;
205
206
        return $this;
207
    }
208
209
    /**
210
     * @param $recipients
211
     * @param  $message
212
     * @throws BongaTechException
213
     */
214
    public function send($recipients, $message)
215
    {
216
        $this->recipients = $recipients;
217
        $this->message = $message;
218
219
220
        if ($this->batch_type === BatchType::NOT_BATCH) {
221
222
            $this->sendForNoBatch();
223
224
        } 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...
225
226
        } 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...
227
228
        } else {
229
230
            throw new BongaTechException('Message Batch Type has not been set.');
231
        }
232
233
    }
234
235
    /**
236
     * send a message to a single recipient.
237
     *
238
     * @return string response
239
     */
240
    private function sendForNoBatch()
241
    {
242
        $headers = array(
243
            'Accept' => 'application/json'
244
        );
245
246
        $body = array(
247
            'AuthDetails' => array(
248
                array(
249
                    'UserID' => $this->user_id,
250
                    'Token' => $this->token,
251
                    'Timestamp' => $this->timestamp
252
253
                )
254
            ),
255
            'MessageType' => array(
256
                (string)$this->message_type
257
            ),
258
            'BatchType' => array(
259
                (string)$this->batch_type
260
            ),
261
            'SourceAddr' => array(
262
                (string)$this->sender_id
263
            ),
264
            'MessagePayload' => array(
265
                $this->message
266
            ),
267
            'DestinationAddr' => array(
268
                array(
269
                    'MSISDN' => $this->recipients,
270
                    'LinkID' => ''
271
                )
272
            ),
273
            'DeliveryRequest' => array(
274
                array(
275
                    'EndPoint' => $this->callback_url,
276
                    'Correlator' => mt_rand()
277
                )
278
            )
279
        );
280
281
        $request = new Request($this->endpoint, $headers, $body);
282
        $response = $request->send();
283
284
        return $response;
285
    }
286
287
    /**
288
     * send same message to different recipients.
289
     *
290
     * @param Request $request
291
     * @return request
292
     */
293
    private function sendForSameMessage(Request $request)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
294
    {
295
        return $request;
296
    }
297
298
    /**
299
     * send different message to different recipients.
300
     *
301
     * @param Request $request
302
     */
303
    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...
304
    {
305
    }
306
307
308
}
309