Completed
Push — master ( 7c4a20...747969 )
by Vincent
09:33
created

SMS::send()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 17
rs 9.2
cc 4
eloc 12
nc 4
nop 2
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
        $this->batch_type = BatchType::NOT_BATCH;
152
153
        return $this;
154
    }
155
156
    /**
157
     * invoke if SMS is being sent to a different recipients.
158
     *
159
     * @return $this
160
     */
161
    public function batchTypeSameMessage()
162
    {
163
        $this->batch_type = BatchType::SAME_MESSAGE;
164
165
        return $this;
166
    }
167
168
    /**
169
     * invoke if each recipient will receive a different message.
170
     *
171
     * @return $this
172
     */
173
    public function batchTypeDifferentMessages()
174
    {
175
        $this->batch_type = BatchType::DIFFERENT_MESSAGE;
176
177
        return $this;
178
    }
179
180
    /**
181
     * @param $recipients
182
     * @param  $message
183
     *
184
     * @throws BongaTechException
185
     *
186
     * @return string response
187
     */
188
    public function send($recipients, $message)
189
    {
190
        $this->recipients = $recipients;
191
        $this->message = $message;
192
        $this->endpoint = $this->config['base_url'] .$this->config['sms_endpoint'];
193
        $response = '';
194
195
        if ($this->batch_type === BatchType::NOT_BATCH) {
196
            $response = $this->sendForNoBatch();
197
        } 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...
198
        } 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...
199
        } else {
200
            throw new BongaTechException('Message Batch Type has not been set.');
201
        }
202
203
        return $response;
204
    }
205
206
    /**
207
     * send a message to a single recipient.
208
     *
209
     * @return Response
210
     */
211
    private function sendForNoBatch()
212
    {
213
        $headers = [
214
            'Accept' => 'application/json'
215
        ];
216
217
        $body = [
218
            'AuthDetails' => [
219
                [
220
                    'UserID' => $this->config['user_id'],
221
                    'Token' => $this->token,
222
                    'Timestamp' => $this->timestamp
223
224
                ]
225
            ],
226
            'MessageType' => [
227
                (string)$this->message_type
228
            ],
229
            'BatchType' => [
230
                (string)$this->batch_type
231
            ],
232
            'SourceAddr' => [
233
                (string)$this->config['sender_id']
234
            ],
235
            'MessagePayload' => [
236
                [
237
                    'Text' => $this->message
238
                ]
239
            ],
240
            'DestinationAddr' => [
241
                [
242
                    'MSISDN' => $this->recipients,
243
                    'LinkID' => '',
244
                    'SourceID' => rand(2, 9)
245
                ]
246
            ],
247
            'DeliveryRequest' => [
248
                [
249
                    'EndPoint' => $this->config['callback_url'],
250
                    'Correlator' => mt_rand()
251
                ]
252
            ]
253
        ];
254
255
        $request = new Request($this->endpoint, $headers, $body);
256
        $response = $request->send();
257
258
        return new Response($response->body[0]);
259
260
    }
261
262
    /**
263
     * send same message to different recipients.
264
     *
265
     * @param Request $request
266
     * @return request
267
     */
268
    private function sendForSameMessage(Request $request)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
269
    {
270
        return $request;
271
    }
272
273
    /**
274
     * send different message to different recipients.
275
     *
276
     * @param Request $request
277
     */
278
    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...
279
    {
280
    }
281
282
    public static function getBalance()
283
    {
284
        $config = Config::get();
285
286
        $endpoint = $config['base_url'].$config['balance_endpoint'].'?UserID='.$config['user_id'].'&Token='.md5($config['password']);
287
288
        $request = new Request($endpoint);
289
        $response = $request->getBalance();
290
291
        return new Response($response->body);
292
    }
293
}
294