SmsGateway::getSenderid()   A
last analyzed

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 $templateid;
25
26
    protected $securekey;
27
28
    protected $tag;
29
30
    protected $payload;
31
32
    protected $response;
33
34
    public function __construct()
35
    {
36
        return $this->setGateway()
37
            ->setApiEndpoint()
38
            ->setUsername()
39
            ->setSenderid()
40
            ->setSecurekey()
41
            ->setPayload($this->buildConfigParams());
42
    }
43
44
    public function sendSms()
45
    {
46
        $this->hashPayload();
47
        $post = curl_init();
48
        //curl_setopt($post, CURLOPT_SSLVERSION, 5); // uncomment for systems supporting TLSv1.1 only
49
        //curl_setopt($post, CURLOPT_SSLVERSION, 6); // use for systems supporting TLSv1.2 or comment the line
50
        curl_setopt($post, CURLOPT_SSL_VERIFYHOST, 0);
51
        curl_setopt($post, CURLOPT_SSL_VERIFYPEER, false);
52
        curl_setopt($post, CURLOPT_URL, $this->getApiEndpoint());
53
        curl_setopt($post, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->getSecurekey()));
54
        curl_setopt($post, CURLOPT_POSTFIELDS, http_build_query($this->getPayload()));
55
        curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
56
        $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...
57
        $resp['payload'] = $this->getPayload();
58
        if($resp['data'] === false) {
59
            $resp['error'] =  curl_error($post);
60
        }
61
        curl_close($post);
62
        $this->setResponse($resp);
63
        return $this->getResponse();
64
    }
65
66
    protected function hashPayload()
67
    {
68
        $payload = $this->getPayload();
69
        $form_params = [
70
            config('smsgateway.' . config('smsgateway.default') . '.apiMobileNoParam') => $this->getRecipients(),
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

70
            /** @scrutinizer ignore-call */ 
71
            config('smsgateway.' . config('smsgateway.default') . '.apiMobileNoParam') => $this->getRecipients(),
Loading history...
71
            config('smsgateway.' . config('smsgateway.default') . '.apiSmsParam') => $this->getContents(),
72
            config('smsgateway.' . config('smsgateway.default') . '.apiTemplateIdParam') => $this->getTemplateId(),
73
            config('smsgateway.' . config('smsgateway.default') . '.apiTagParam') => $this->getTag(),
74
        ];
75
        $payload = array_merge($form_params, $payload);
76
77
        if (array_key_exists('key', $payload)) {
78
            $payload['key'] = hash('sha512', $this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey());
79
        }
80
        //dump("data to hash => ".$this->getUsername().$this->getSenderid().$this->getContents().$this->getSecurekey());
81
        $this->setPayload($payload);
82
        return $this;
83
    }
84
85
86
    protected function buildConfigParams()
87
    {
88
        $configParams = array_combine(
89
            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

89
            /** @scrutinizer ignore-call */ 
90
            config('smsgateway.' . config('smsgateway.default') . '.apiParams'),
Loading history...
90
            config('smsgateway.' . config('smsgateway.default') . '.apiValues')
91
        );
92
93
        $form_params = [
94
            "smsservicetype" =>"singlemsg",
95
        ];
96
        $payload = array_merge($form_params, $configParams);
97
98
        return $payload;
99
    }
100
101
    /**
102
     * Gets the Message for the SMS
103
     * @param  string
104
     * @return self
105
     */
106
    public function withSms($contents = '')
107
    {
108
        $this->setContents($contents);
109
        $this->hashPayload();
110
        return $this;
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116
    public function getTemplateId()
117
    {
118
        return $this->templateid;
119
    }
120
121
    /**
122
     * @param mixed $templateId
123
     *
124
     * @return self
125
     */
126
    public function withTemplateId($templateId = '')
127
    {
128
        $this->templateid = $templateId == ''
129
        ? config('smsgateway.' . $this->getGateway() . '.apiValues.apiTemplateId')
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

129
        ? /** @scrutinizer ignore-call */ config('smsgateway.' . $this->getGateway() . '.apiValues.apiTemplateId')
Loading history...
130
        : trim($templateId);
131
        $payload = $this->getPayload();
132
        if(is_array($payload)) {
133
            if (array_key_exists('templateid', $payload)) {
134
                $payload['templateid'] = $this->templateid;
135
                $this->setPayload($payload);
136
            }
137
        }
138
        return $this;
139
    }
140
141
    /**
142
     * Gets the Tag for the SMS
143
     * @param  mixed
144
     * @return self
145
     */
146
    public function withTag($tag)
147
    {
148
        $this->setTag($tag);
149
        return $this;
150
    }
151
152
    public function toRecipient($mobile = '')
153
    {
154
        return $this->setRecipients($mobile);
155
    }
156
157
    public function fromGateway($gateway = '')
158
    {
159
        return $this->setGateway($gateway);
160
    }
161
162
    /**
163
     * @return mixed
164
     */
165
    protected function getRecipients()
166
    {
167
        return $this->recipients;
168
    }
169
170
    /**
171
     * @param mixed $recipients
172
     *
173
     * @return self
174
     */
175
    protected function setRecipients($recipients)
176
    {
177
        $this->recipients = $recipients;
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return mixed
184
     */
185
    protected function getContents()
186
    {
187
        return $this->contents;
188
    }
189
190
    /**
191
     * @param mixed $contents
192
     *
193
     * @return self
194
     */
195
    protected function setContents($contents)
196
    {
197
        $this->contents = trim($contents);
198
199
        return $this;
200
    }
201
202
    /**
203
     * @return mixed
204
     */
205
    protected function getGateway()
206
    {
207
        return $this->gateway;
208
    }
209
210
    /**
211
     * @param mixed $gateway
212
     *
213
     * @return self
214
     */
215
    protected function setGateway($gateway = '')
216
    {
217
        $this->gateway = $gateway == ''
218
                ? 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

218
                ? /** @scrutinizer ignore-call */ config('smsgateway.default')
Loading history...
219
                : trim($gateway);
220
        return $this;
221
    }
222
223
    /**
224
     * @return mixed
225
     */
226
    protected function getUsername()
227
    {
228
        return $this->username;
229
    }
230
231
    /**
232
     * @param mixed $username
233
     *
234
     * @return self
235
     */
236
    protected function setUsername($username = '')
237
    {
238
        $this->username = $username == ''
239
            ? 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

239
            ? /** @scrutinizer ignore-call */ config('smsgateway.' . $this->getGateway() . '.apiValues.apiUser')
Loading history...
240
            : trim($username);
241
        //dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiUser'));
242
        return $this;
243
    }
244
245
    /**
246
     * @return mixed
247
     */
248
    protected function getSenderid()
249
    {
250
        return $this->senderid;
251
    }
252
253
    /**
254
     * @param mixed $senderid
255
     *
256
     * @return self
257
     */
258
    protected function setSenderid($senderid = '')
259
    {
260
        $this->senderid = $senderid == ''
261
            ? 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

261
            ? /** @scrutinizer ignore-call */ config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId')
Loading history...
262
            : trim($senderid);
263
        //dump(config('smsgateway.' . $this->getGateway() . '.apiValues.apiSenderId'));
264
        return $this;
265
    }
266
267
    /**
268
     * @return mixed
269
     */
270
    protected function getSecurekey()
271
    {
272
        return $this->securekey;
273
    }
274
275
    /**
276
     * @param mixed $securekey
277
     *
278
     * @return self
279
     */
280
    protected function setSecurekey($securekey = '')
281
    {
282
        $this->securekey = $securekey == ''
283
            ? 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

283
            ? /** @scrutinizer ignore-call */ config('smsgateway.' . $this->getGateway() . '.apiValues.apiSecureKey')
Loading history...
284
            : trim($securekey);
285
286
        return $this;
287
    }
288
289
    /**
290
     * @return mixed
291
     */
292
    public function getTag()
293
    {
294
        return $this->tag;
295
    }
296
297
    /**
298
     * @param mixed $tag
299
     *
300
     * @return self
301
     */
302
    protected function setTag($tag)
303
    {
304
        $this->tag = $tag;
305
306
        return $this;
307
    }
308
309
    /**
310
     * @return mixed
311
     */
312
    public function getResponse()
313
    {
314
        return json_encode($this->response, JSON_PRETTY_PRINT);
315
    }
316
317
    /**
318
     * @param mixed $response
319
     *
320
     * @return self
321
     */
322
    protected function setResponse($response)
323
    {
324
        $this->response = $response;
325
326
        return $this;
327
    }
328
329
    /**
330
     * @return mixed
331
     */
332
    protected function getPayload()
333
    {
334
        return $this->payload;
335
    }
336
337
    /**
338
     * @param mixed $payload
339
     *
340
     * @return self
341
     */
342
    protected function setPayload($payload)
343
    {
344
        $this->payload = $payload;
345
        //dump($payload);
346
        return $this;
347
    }
348
349
    /**
350
     * @return mixed
351
     */
352
    protected function getApiEndpoint()
353
    {
354
        return $this->apiEndpoint;
355
    }
356
357
    /**
358
     * @param mixed $apiEndpoint
359
     *
360
     * @return self
361
     */
362
    protected function setApiEndpoint()
363
    {
364
        $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

364
        $this->apiEndpoint = /** @scrutinizer ignore-call */ config('smsgateway.' . $this->getGateway() . '.apiEndpoint');
Loading history...
365
        return $this;
366
    }
367
368
    public function asOtpSms()
369
    {
370
        $payload = $this->getPayload();
371
        if (array_key_exists('smsservicetype', $payload)) {
372
            $payload['smsservicetype'] = 'otpmsg';
373
            $this->setPayload($payload);
374
        }
375
        return $this;
376
    }
377
378
    public function asUnicodeOtpSms()
379
    {
380
        $payload = $this->getPayload();
381
        if (array_key_exists('smsservicetype', $payload)) {
382
            $payload['smsservicetype'] = 'unicodeotpmsg';
383
            $this->setPayload($payload);
384
        }
385
        return $this;
386
    }
387
388
    public function asUnicodeSms()
389
    {
390
        $payload = $this->getPayload();
391
        if (array_key_exists('smsservicetype', $payload)) {
392
            $payload['smsservicetype'] = 'unicodemsg';
393
            $this->setPayload($payload);
394
        }
395
        return $this;
396
    }
397
398
    public function asBulkUnicodeSms()
399
    {
400
        return $this->asUnicodeSms();
401
    }
402
    
403
    public function asBulkSms()
404
    {
405
        $payload = $this->getPayload();
406
        if (array_key_exists('smsservicetype', $payload)) {
407
            $payload['smsservicetype'] = 'bulkmsg';
408
            $this->setPayload($payload);
409
        }
410
        return $this;
411
    }
412
}
413