Passed
Push — dev ( 76187a...8f1bb8 )
by Abu Salam
02:46
created

SmsGateway::getGateway()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace AbuSalam;
4
5
use AbuSalam\Contracts\SmsGatewayContract;
6
7
/**
8
 * SMS Gateway Implementation
9
 */
10
class SmsGateway implements SmsGatewayContract
11
{
12
    protected $recipients;
13
14
    protected $contents;
15
16
    protected $gateway;
17
18
    protected $apiEndpoint;
19
20
    protected $username;
21
22
    protected $senderid;
23
24
    protected $securekey;
25
26
    protected $tag;
27
28
    protected $payload;
29
30
    protected $response;
31
32
    public function __construct()
33
    {
34
        return $this->setGateway()
35
            ->setApiEndpoint()
36
            ->setUsername()
37
            ->setSenderid()
38
            ->setSecurekey();
39
    }
40
41
    public function sendSms()
42
    {
43
        $post = curl_init();
44
        //curl_setopt($post, CURLOPT_SSLVERSION, 5); // uncomment for systems supporting TLSv1.1 only
45
        //curl_setopt($post, CURLOPT_SSLVERSION, 6); // use for systems supporting TLSv1.2 or comment the line
46
        curl_setopt($post, CURLOPT_SSL_VERIFYHOST, 0);
47
        curl_setopt($post, CURLOPT_SSL_VERIFYPEER, false);
48
        curl_setopt($post, CURLOPT_URL, $this->getApiEndpoint());
49
        curl_setopt($post, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->getSecurekey()));
50
        curl_setopt($post, CURLOPT_POSTFIELDS, http_build_query($this->getPayload()));
51
        curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
52
        $resp['data'] = curl_exec($post);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$resp was never initialized. Although not strictly required by PHP, it is generally a good practice to add $resp = array(); before regardless.
Loading history...
53
        if($resp['data'] === false) {
54
            $resp['error'] =  curl_error($post);
55
        }
56
        curl_close($post);
57
        $this->setResponse($resp);
58
        return $this->getResponse();
59
    }
60
61
    protected function hashPayload()
62
    {
63
        $payload = $this->buildConfigParams();
64
        if (array_key_exists('key', $payload)) {
65
            $payload['key'] = hash('sha512', $this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey());
66
        }
67
        //dump("data to hash => ".$this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey());
68
        $this->setPayload($payload);
69
        return $this;
70
    }
71
72
73
    protected function buildConfigParams()
74
    {
75
        $configParams = array_combine(
76
            config('smsgateway.' . config('smsgateway.default') . '.apiParams'),
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
            /** @scrutinizer ignore-call */ 
77
            config('smsgateway.' . config('smsgateway.default') . '.apiParams'),
Loading history...
77
            config('smsgateway.' . config('smsgateway.default') . '.apiValues')
78
        );
79
80
        $form_params = [
81
            config('smsgateway.' . config('smsgateway.default') . '.apiMobileNoParam') => $this->getRecipients(),
82
            config('smsgateway.' . config('smsgateway.default') . '.apiSmsParam') => $this->getContents(),
83
            config('smsgateway.' . config('smsgateway.default') . '.apiTagParam') => $this->getTag(),
84
            "smsservicetype" =>"singlemsg",
85
        ];
86
        $data = array_merge($form_params, $configParams);
87
88
        //dump($data);
89
        return $data;
90
    }
91
92
    /**
93
     * Gets the Message for the SMS
94
     * @param  string
95
     * @return self
96
     */
97
    public function withSms($contents = '')
98
    {
99
        $this->setContents($contents);
100
        $this->hashPayload();
101
        return $this;
102
    }
103
104
    /**
105
     * Gets the Tag for the SMS
106
     * @param  mixed
107
     * @return self
108
     */
109
    public function withTag($tag)
110
    {
111
        $this->setTag($tag);
112
        return $this;
113
    }
114
115
    public function toRecipient($mobile = '')
116
    {
117
        return $this->setRecipients($mobile);
118
    }
119
120
    public function fromGateway($gateway = '')
121
    {
122
        return $this->setGateway($gateway);
123
    }
124
125
    /**
126
     * @return mixed
127
     */
128
    protected function getRecipients()
129
    {
130
        return $this->recipients;
131
    }
132
133
    /**
134
     * @param mixed $recipients
135
     *
136
     * @return self
137
     */
138
    protected function setRecipients($recipients)
139
    {
140
        $this->recipients = $recipients;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return mixed
147
     */
148
    protected function getContents()
149
    {
150
        return $this->contents;
151
    }
152
153
    /**
154
     * @param mixed $contents
155
     *
156
     * @return self
157
     */
158
    protected function setContents($contents)
159
    {
160
        $this->contents = trim($contents);
161
162
        return $this;
163
    }
164
165
    /**
166
     * @return mixed
167
     */
168
    protected function getGateway()
169
    {
170
        return $this->gateway;
171
    }
172
173
    /**
174
     * @param mixed $gateway
175
     *
176
     * @return self
177
     */
178
    protected function setGateway($gateway = '')
179
    {
180
        $this->gateway = $gateway == ''
181
                ? config('smsgateway.default')
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
                ? /** @scrutinizer ignore-call */ config('smsgateway.default')
Loading history...
182
                : trim($gateway);
183
        return $this;
184
    }
185
186
    /**
187
     * @return mixed
188
     */
189
    protected function getUsername()
190
    {
191
        return $this->username;
192
    }
193
194
    /**
195
     * @param mixed $username
196
     *
197
     * @return self
198
     */
199
    protected function setUsername($username = '')
200
    {
201
        $this->username = $username == ''
202
            ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiUser')
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

202
            ? /** @scrutinizer ignore-call */ config('smsgateway.' . $this->getGateway() . '.apiValues.apiUser')
Loading history...
203
            : trim($username);
204
        //dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiUser'));
205
        return $this;
206
    }
207
208
    /**
209
     * @return mixed
210
     */
211
    protected function getSenderid()
212
    {
213
        return $this->senderid;
214
    }
215
216
    /**
217
     * @param mixed $senderid
218
     *
219
     * @return self
220
     */
221
    protected function setSenderid($senderid = '')
222
    {
223
        $this->senderid = $senderid == ''
224
            ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId')
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

224
            ? /** @scrutinizer ignore-call */ config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId')
Loading history...
225
            : trim($senderid);
226
        //dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId'));
227
        return $this;
228
    }
229
230
    /**
231
     * @return mixed
232
     */
233
    protected function getSecurekey()
234
    {
235
        return $this->securekey;
236
    }
237
238
    /**
239
     * @param mixed $securekey
240
     *
241
     * @return self
242
     */
243
    protected function setSecurekey($securekey = '')
244
    {
245
        $this->securekey = $securekey == ''
246
            ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiSecureKey')
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
            ? /** @scrutinizer ignore-call */ config('smsgateway.' . $this->getGateway() . '.apiValues.apiSecureKey')
Loading history...
247
            : trim($securekey);
248
249
        return $this;
250
    }
251
252
    /**
253
     * @return mixed
254
     */
255
    public function getTag()
256
    {
257
        return $this->tag;
258
    }
259
260
    /**
261
     * @param mixed $tag
262
     *
263
     * @return self
264
     */
265
    protected function setTag($tag)
266
    {
267
        $this->tag = $tag;
268
269
        return $this;
270
    }
271
272
    /**
273
     * @return mixed
274
     */
275
    public function getResponse()
276
    {
277
        return json_encode($this->response, JSON_PRETTY_PRINT);
278
    }
279
280
    /**
281
     * @param mixed $response
282
     *
283
     * @return self
284
     */
285
    protected function setResponse($response)
286
    {
287
        $this->response = $response;
288
289
        return $this;
290
    }
291
292
    /**
293
     * @return mixed
294
     */
295
    protected function getPayload()
296
    {
297
        return $this->payload;
298
    }
299
300
    /**
301
     * @param mixed $payload
302
     *
303
     * @return self
304
     */
305
    protected function setPayload($payload)
306
    {
307
        $this->payload = $payload;
308
309
        return $this;
310
    }
311
312
    /**
313
     * @return mixed
314
     */
315
    protected function getApiEndpoint()
316
    {
317
        return $this->apiEndpoint;
318
    }
319
320
    /**
321
     * @param mixed $apiEndpoint
322
     *
323
     * @return self
324
     */
325
    protected function setApiEndpoint()
326
    {
327
        $this->apiEndpoint = config('smsgateway.' . $this->getGateway() . '.apiEndpoint');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

327
        $this->apiEndpoint = /** @scrutinizer ignore-call */ config('smsgateway.' . $this->getGateway() . '.apiEndpoint');
Loading history...
328
        return $this;
329
    }
330
331
    public function asOtpSms()
332
    {
333
        $payload = $this->getPayload();
334
        if (array_key_exists('smsservicetype', $payload)) {
335
            $payload['smsservicetype'] = 'otpmsg';
336
            $this->setPayload($payload);
337
        }
338
        return $this;
339
    }
340
341
    public function asUnicodeOtpSms()
342
    {
343
        $payload = $this->getPayload();
344
        if (array_key_exists('smsservicetype', $payload)) {
345
            $payload['smsservicetype'] = 'unicodeotpmsg';
346
            $this->setPayload($payload);
347
        }
348
        return $this;
349
    }
350
351
    public function asUnicodeSms()
352
    {
353
        $payload = $this->getPayload();
354
        if (array_key_exists('smsservicetype', $payload)) {
355
            $payload['smsservicetype'] = 'unicodemsg';
356
            $this->setPayload($payload);
357
        }
358
        return $this;
359
    }
360
361
    public function asBulkUnicodeSms()
362
    {
363
        return $this->asUnicodeSms();
364
    }
365
    
366
    public function asBulkSms()
367
    {
368
        $payload = $this->getPayload();
369
        if (array_key_exists('smsservicetype', $payload)) {
370
            $payload['smsservicetype'] = 'bulkmsg';
371
            $this->setPayload($payload);
372
        }
373
        return $this;
374
    }
375
}
376