Sms   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 132
dl 0
loc 200
rs 10
c 0
b 0
f 0
wmc 22

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B sendMultipleSms() 0 52 7
A sendOtpSms() 0 34 4
A sendSingleSms() 0 37 5
A verifyOtpSms() 0 29 5
1
<?php
2
3
namespace Wavecell;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use Wavecell\Config;
8
use Wavecell\Helper;
9
use Wavecell\HttpException;
10
use Wavecell\SmsInterface;
11
12
/**
13
 * Wavecell SMS REST API Library.
14
 *
15
 * @author     Pribumi Technology
16
 * @license    MIT
17
 * @copyright  (c) 2019, Pribumi Technology
18
 */
19
class Sms implements SmsInterface
20
{
21
    /**
22
     * @var object
23
     */
24
    private $response;
25
26
    /**
27
     * The Array of Curl Options.
28
     *
29
     * @var array
30
     */
31
    private $curlOpts;
32
33
    /**
34
     * \Wavecell\Sms constructor.
35
     */
36
    public function __construct()
37
    {
38
        $this->curlOpts = [
39
            CURLOPT_RETURNTRANSFER => true,
40
            CURLOPT_SSL_VERIFYHOST => '0',
41
            CURLOPT_SSL_VERIFYPEER => '0',
42
            CURLOPT_AUTOREFERER => true,
43
            CURLINFO_HEADER_OUT => true,
44
            CURLOPT_FOLLOWLOCATION => '1',
45
            CURLOPT_VERBOSE => true
46
        ];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function sendSingleSms($destination, $smsText, $smsEncoding = 'AUTO', $throws = true)
53
    {
54
        try {
55
            if ($smsEncoding === '') {
56
                $smsEncoding = 'AUTO';
57
            }
58
            $url = Config::getBaseUrl() . '/sms/v1/' . Config::$subAccountId . '/single';
59
            $guzzleClient = new Client();
60
            $body = array();
61
            $body['clientMessageId'] = Helper::random(50);
62
            $body['source'] = Config::$smsFrom;
63
            $body['destination'] = $destination;
64
            $body['text'] = $smsText;
65
            $body['encoding'] = $smsEncoding;
66
            $body['expiry'] = Helper::generateExpired(Config::$smsExpireInMinutes);
67
            uksort($body, 'strcmp');
68
            $curls = array_merge(Config::$curlOptions, $this->curlOpts);
69
            $this->response = $guzzleClient->post($url, [
70
                'headers' => [
71
                    'Authorization' => 'Bearer ' . Config::$secretKey,
72
                    'Content-Type' => 'application/json',
73
                    'curl' => $curls
74
                ],
75
                'json' => $body
76
            ]);
77
        } catch (RequestException $exception) {
78
            $this->response = $exception->getResponse();
79
            if ($throws) {
80
                $body = (string)$this->response->getBody();
81
                $code = (int)$this->response->getStatusCode();
82
                $content = json_decode($body);
83
                throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
84
            } else {
85
                return $this->response;
86
            }
87
        }
88
        return (string)$this->response->getBody();
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function sendMultipleSms($smsText, array $destinationNumber = [], $smsEncoding = 'AUTO', $throws = true)
95
    {
96
        try {
97
            if ($smsEncoding === '') {
98
                $smsEncoding = 'AUTO';
99
            }
100
            if (empty($destinationNumber)) {
101
                throw new HttpException('Destination must not empty.');
102
            }
103
            $url = Config::getBaseUrl() . '/sms/v1/' . Config::$subAccountId . '/single';
104
            $guzzleClient = new Client();
105
            $body = array('clientBatchId' => Helper::random(50),
106
                'messages' => array(),
107
                'template' => array('source' => Config::$smsFrom,
108
                    'text' => $smsText, 'encoding' => $smsEncoding,
109
                    'expiry' => Helper::generateExpired(2880)
110
                ),
111
                'includeMessagesInResponse' => true
112
            );
113
            foreach ($destinationNumber as $key => $value) {
114
                $clientData = array();
115
                $clientData['clientMessageId'] = Helper::random(50);
116
                $clientData['source'] = \Wavecell\Config::$smsFrom;
117
                $clientData['destination'] = $value;
118
                $clientData['text'] = $smsText;
119
                $clientData['encoding'] = $smsEncoding;
120
                $clientData['expiry'] = Helper::generateExpired(Config::$smsExpireInMinutes);
121
                uksort($clientData, 'strcmp');
122
                array_push($body['messages'], $clientData);
123
            }
124
            uksort($body, 'strcmp');
125
            $curls = array_merge(Config::$curlOptions, $this->curlOpts);
126
            $this->response = $guzzleClient->post($url, [
127
                'headers' => [
128
                    'Authorization' => 'Bearer ' . Config::$secretKey,
129
                    'Content-Type' => 'application/json',
130
                    'curl' => $curls
131
                ],
132
                'json' => $body
133
            ]);
134
        } catch (RequestException $exception) {
135
            $this->response = $exception->getResponse();
136
            if ($throws) {
137
                $body = (string)$this->response->getBody();
138
                $code = (int)$this->response->getStatusCode();
139
                $content = json_decode($body);
140
                throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
141
            } else {
142
                return $this->response;
143
            }
144
        }
145
        return (string)$this->response->getBody();
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function sendOtpSms($destination, $throws = true)
152
    {
153
        try {
154
            $url = Config::getBaseUrl() . '/verify/v1/' . Config::$subAccountId;
155
            $guzzleClient = new Client();
156
            $body = array();
157
            $body['country'] = Config::$country;
158
            $body['destination'] = $destination;
159
            $body['productName'] = Config::$smsFrom;
160
            $body['codeLength'] = Config::$otpCodeLength;
161
            $body['codeValidity'] = Config::$otpCodeValidity;
162
            $body['resendingInterval'] = Config::$resendInterval;
163
            uksort($body, 'strcmp');
164
            $curls = array_merge(Config::$curlOptions, $this->curlOpts);
165
            $this->response = $guzzleClient->post($url, [
166
                'headers' => [
167
                    'Authorization' => 'Bearer ' . Config::$secretKey,
168
                    'Content-Type' => 'application/json',
169
                    'curl' => $curls
170
                ],
171
                'json' => $body
172
            ]);
173
        } catch (RequestException $exception) {
174
            $this->response = $exception->getResponse();
175
            if ($throws) {
176
                $body = (string)$this->response->getBody();
177
                $code = (int)$this->response->getStatusCode();
178
                $content = json_decode($body);
179
                throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
180
            } else {
181
                return $this->response;
182
            }
183
        }
184
        return (string)$this->response->getBody();
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function verifyOtpSms($uid, $code, $throws = true)
191
    {
192
        try {
193
            $url = Config::getBaseUrl() . "/verify/v1/" . Config::$subAccountId . "/$uid?code=$code";
194
            $guzzleClient = new Client();
195
            $curls = array_merge(Config::$curlOptions, $this->curlOpts);
196
            $this->response = $guzzleClient->get($url, [
197
                'headers' => [
198
                    'Authorization' => 'Bearer ' . Config::$secretKey,
199
                    'Content-Type' => 'application/json',
200
                    'curl' => $curls
201
                ]
202
            ]);
203
        } catch (RequestException $exception) {
204
            $this->response = $exception->getResponse();
205
            if ($throws) {
206
                $body = (string)$this->response->getBody();
207
                $code = (int)$this->response->getStatusCode();
208
                $content = json_decode($body);
209
                throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
210
            } else {
211
                return $this->response;
212
            }
213
        }
214
        $data = json_decode($this->response->getBody());
215
        if (strtoupper($data->status) !== 'VERIFIED') {
216
            throw new HttpException('Error verifiying code.', 400);
217
        }
218
        return (string)$this->response->getBody();
219
    }
220
}
221