Issues (8)

src/ZenzivaSms.php (8 issues)

1
<?php
2
/**
3
 * This file is part of the Zenziva Sms Package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace Steevenz;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Curl;
19
use O2System\Kernel\Http\Message\Uri;
20
use O2System\Spl\DataStructures\SplArrayObject;
21
use O2System\Spl\Traits\Collectors\ConfigCollectorTrait;
22
use O2System\Spl\Traits\Collectors\ErrorCollectorTrait;
23
24
/**
25
 * Class ZenzivaSms
26
 */
27
class ZenzivaSms
28
{
29
    use ConfigCollectorTrait;
30
    use ErrorCollectorTrait;
31
32
    /**
33
     * ZenzivaSms::$deliveryStatuses
34
     *
35
     * List of Zenziva Sms code status by code numbers.
36
     *
37
     * @var array
38
     */
39
    public $statusCodes = [
40
        0  => 'Success',
41
        1  => 'Nomor tujuan tidak valid',
42
        5  => 'Userkey / Passkey salah',
43
        6  => 'Konten SMS rejected',
44
        89 => 'Pengiriman SMS berulang-ulang ke satu nomor dalam satu waktu',
45
        99 => 'Credit tidak mencukupi',
46
    ];
47
48
    /**
49
     * ZenzivaSms::$response
50
     *
51
     * Zenziva Sms original response.
52
     *
53
     * @access  protected
54
     * @type    mixed
55
     */
56
    protected $response;
57
58
    // ------------------------------------------------------------------------
59
60
    /**
61
     * ZenzivaSms::__construct
62
     *
63
     * @param array $config
64
     *
65
     * @access  public
66
     */
67
    public function __construct(array $config = [])
68
    {
69
        $defaultConfig = [
70
            'apiUrl'  => 'https://reguler.zenziva.net/apps/',
71
            'userkey' => null,
72
            'passkey' => null,
73
        ];
74
75
        $this->setConfig(array_merge($defaultConfig, $config));
76
    }
77
    // ------------------------------------------------------------------------
78
79
    /**
80
     * ZenzivaSms::setApiUrl
81
     *
82
     * Set Zenziva Sms API Url.
83
     *
84
     * @param string $serverIp Zenziva Sms API Url.
85
     *
86
     * @access  public
87
     * @return  static
88
     */
89
    public function setApiUrl($apiUrl)
0 ignored issues
show
The parameter $apiUrl is not used and could be removed. ( Ignorable by Annotation )

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

89
    public function setApiUrl(/** @scrutinizer ignore-unused */ $apiUrl)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        $this->setConfig('apiUrl', $serverIp);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $serverIp seems to be never defined.
Loading history...
92
93
        return $this;
94
    }
95
    // ------------------------------------------------------------------------
96
97
    /**
98
     * ZenzivaSms::setUserkey
99
     *
100
     * Set Zenziva Sms Userkey.
101
     *
102
     * @param string $userkey Zenziva Sms Userkey
103
     *
104
     * @access  public
105
     * @return  static
106
     */
107
    public function setUserkey($userkey)
108
    {
109
        $this->setConfig('userkey', $userkey);
110
111
        return $this;
112
    }
113
    // ------------------------------------------------------------------------
114
115
    /**
116
     * ZenzivaSms::setPasskey
117
     *
118
     * Set Zenziva Sms Passkey.
119
     *
120
     * @param string $userkey Zenziva Sms Passkey
121
     *
122
     * @access  public
123
     * @return  static
124
     */
125
    public function setPasskey($passkey)
0 ignored issues
show
The parameter $passkey is not used and could be removed. ( Ignorable by Annotation )

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

125
    public function setPasskey(/** @scrutinizer ignore-unused */ $passkey)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
    {
127
        $this->setConfig('passkey', $userkey);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userkey seems to be never defined.
Loading history...
128
129
        return $this;
130
    }
131
    // ------------------------------------------------------------------------
132
133
    /**
134
     * ZenzivaSms::request
135
     *
136
     * Call API request.
137
     *
138
     * @param string $path
139
     * @param array  $params
140
     * @param string $type
141
     *
142
     * @access  protected
143
     * @return  mixed
144
     * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException
145
     */
146
    protected function request($path, $params = [], $type = 'GET')
0 ignored issues
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

146
    protected function request($path, $params = [], /** @scrutinizer ignore-unused */ $type = 'GET')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
147
    {
148
        // default params
149
        if (empty($this->config[ 'apiUrl' ])) {
150
            throw new \InvalidArgumentException('Zenziva Sms: API Url is not set!');
151
        }
152
153
        if (empty($this->config[ 'userkey' ])) {
154
            throw new \InvalidArgumentException('Zenziva Sms: Userkey is not set');
155
        } else {
156
            $defaultParams[ 'userkey' ] = $this->config[ 'userkey' ];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$defaultParams was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaultParams = array(); before regardless.
Loading history...
157
        }
158
159
        if (empty($this->config[ 'passkey' ])) {
160
            throw new \InvalidArgumentException('Zenziva Sms: Passkey is not set');
161
        } else {
162
            $defaultParams[ 'passkey' ] = $this->config[ 'passkey' ];
163
        }
164
165
        $uri = (new Uri($this->config[ 'apiUrl' ]))->withPath($path);
166
        $request = new Curl\Request();
167
        $request->setConnectionTimeout(500);
168
169
        if ($this->response = $request->setUri($uri)->get(array_merge($defaultParams, $params))) {
170
            if (false !== ($error = $this->response->getError())) {
0 ignored issues
show
The condition false !== $error = $this->response->getError() is always true.
Loading history...
171
                $this->addError($error->code, $error->message);
172
            } elseif ($body = $this->response->getBody()) {
173
                if (isset($body->message->status)) {
174
                    if ($body->message->status == 0) {
175
                        return new SplArrayObject([
176
                            'id'      => $body->message->messageId,
177
                            'status'  => $body->message->status,
178
                            'message' => $body->message->text,
179
                            'balance' => $body->message->balance,
180
                        ]);
181
                    } else {
182
                        $this->addError($body->message->status, $body->message->text);
183
                    }
184
                }
185
            }
186
        }
187
188
        return false;
189
    }
190
    // ------------------------------------------------------------------------
191
192
    /**
193
     * Rajasms::buildSendPackageData
194
     *
195
     * @param array $data
196
     *
197
     * @return array|bool
198
     */
199
    protected function validateMsisdn($msisdn)
200
    {
201
        if (preg_match('/^(62[1-9]{1}[0-9]{1,2})[0-9]{6,8}$/', $msisdn) == 1) {
202
            $msisdn = '0' . substr($msisdn, 2);
203
        } elseif (preg_match('/^(\+62[1-9]{1}[0-9]{1,2})[0-9]{6,8}$/', $msisdn) == 1) {
204
            $msisdn = '0' . substr($msisdn, 3);
205
        }
206
207
        if (preg_match('/^(0[1-9]{1}[0-9]{1,2})[0-9]{6,8}$/', $msisdn) == 1) {
208
            return trim($msisdn);
0 ignored issues
show
Bug Best Practice introduced by
The expression return trim($msisdn) returns the type string which is incompatible with the documented return type array|boolean.
Loading history...
209
        }
210
211
        return false;
212
    }
213
    // ------------------------------------------------------------------------
214
215
    /**
216
     * ZenzivaSms::send
217
     *
218
     * Send SMS
219
     *
220
     * @param string $msisdn  MSISDN Number
221
     * @param string $message Message
222
     *
223
     * @access  public
224
     * @return  mixed
225
     * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException
226
     */
227
    public function send($msisdn, $message)
228
    {
229
        if (false === ($msisdn = $this->validateMsisdn($msisdn))) {
230
            throw new \InvalidArgumentException('Zenziva Sms: Invalid MSISDN Number');
231
        }
232
233
        return $this->request('apps/smsapi.php', [
234
            'nohp'  => $msisdn,
235
            'pesan' => $message,
236
        ]);
237
    }
238
    // ------------------------------------------------------------------------
239
240
    /**
241
     * ZenzivaSms::send
242
     *
243
     * Send SMS
244
     *
245
     * @param string $msisdn  MSISDN Number
246
     * @param string $otpCode Otp Code
247
     *
248
     * @access  public
249
     * @return  mixed
250
     * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException
251
     */
252
    public function sendOtp($msisdn, $otpCode)
253
    {
254
        if (false === ($msisdn = $this->validateMsisdn($msisdn))) {
255
            throw new \InvalidArgumentException('Zenziva Sms: Invalid MSISDN Number');
256
        }
257
258
        $otpCode = trim($otpCode);
259
260
        if (strlen($otpCode) < 4) {
261
            throw new \InvalidArgumentException('Zenziva Sms: OTP Code minimum length is 4 digit');
262
        } elseif (strlen($otpCode) > 8) {
263
            throw new \InvalidArgumentException('Zenziva Sms: OTP Code maximum length is 8 digit');
264
        }
265
266
        return $this->request('apps/smsotp.php', [
267
            'nohp'     => $msisdn,
268
            'kode_otp' => $otpCode,
269
        ]);
270
    }
271
    // ------------------------------------------------------------------------
272
273
    /**
274
     * ZenzivaSms::getResponse
275
     *
276
     * Get original response object.
277
     *
278
     * @access  public
279
     * @return  \O2System\Curl\Response|bool Returns FALSE if failed.
280
     */
281
    public function getResponse()
282
    {
283
        return $this->response;
284
    }
285
}