Completed
Push — master ( 0af6cb...e278f8 )
by Bai
12s queued 10s
created

Sms::querySendSms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 10
dl 0
loc 27
rs 9.488
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Qiniu\Sms;
4
5
use Qiniu\Auth;
6
use Qiniu\Config;
7
use Qiniu\Http\Error;
8
use Qiniu\Http\Client;
9
10
class Sms
11
{
12
    private $auth;
13
    private $baseURL;
14
15
    public function __construct(Auth $auth)
16
    {
17
        $this->auth = $auth;
18
19
        $this->baseURL = sprintf("%s/%s/", Config::SMS_HOST, Config::SMS_VERSION);
20
    }
21
22
    /**
23
     * 创建签名
24
     *
25
     * @param string $signature 签名
26
     * @param string $source 签名来源,申请签名时必须指定签名来源
27
     * @param string $pics 签名对应的资质证明图片进行 base64 编码格式转换后的字符串,可选
28
     * @return array
29
     *
30
     * @link https://developer.qiniu.com/sms/api/5844/sms-api-create-signature
31
     */
32
    public function createSignature($signature, $source, $pics = null)
33
    {
34
        $params['signature'] = $signature;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
35
        $params['source'] = $source;
36
        if (!empty($pics)) {
37
            $params['pics'] = array($this->imgToBase64($pics));
38
        }
39
        $body = json_encode($params);
40
        $url = $this->baseURL . 'signature';
41
        return $this->post($url, $body);
42
    }
43
44
    /**
45
     * 编辑签名
46
     *
47
     * @param string $id 签名 ID
48
     * @param string $signature 签名
49
     * @param string $source 签名来源
50
     * @param string $pics 签名对应的资质证明图片进行 base64 编码格式转换后的字符串,可选
51
     * @return array
52
     * @link https://developer.qiniu.com/sms/api/5890/sms-api-edit-signature
53
     */
54
    public function updateSignature($id, $signature, $source, $pics = null)
55
    {
56
        $params['signature'] = $signature;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
57
        $params['source'] = $source;
58
        if (!empty($pics)) {
59
            $params['pics'] = array($this->imgToBase64($pics));
60
        }
61
        $body = json_encode($params);
62
        $url = $this->baseURL . 'signature/' . $id;
63
        return $this->PUT($url, $body);
64
    }
65
66
    /**
67
     * 列出签名
68
     *
69
     * @param string $audit_status 审核状态:"passed"(通过), "rejected"(未通过), "reviewing"(审核中)
70
     * @param int $page 页码。默认为 1
71
     * @param int $page_size 分页大小。默认为 20
72
     * @return array
73
     * @link https://developer.qiniu.com/sms/api/5889/sms-api-query-signature
74
     */
75
    public function querySignature($audit_status = null, $page = 1, $page_size = 20)
76
    {
77
78
        $url = sprintf(
79
            "%s?audit_status=%s&page=%s&page_size=%s",
80
            $this->baseURL . 'signature',
81
            $audit_status,
82
            $page,
83
            $page_size
84
        );
85
        return $this->get($url);
86
    }
87
88
    /**
89
     * 查询单个签名
90
     *
91
     * @param string $signature_id
92
     * @return array
93
     * @link https://developer.qiniu.com/sms/api/5970/query-a-single-signature
94
     */
95
    public function checkSingleSignature($signature_id)
96
    {
97
98
        $url = sprintf(
99
            "%s/%s",
100
            $this->baseURL . 'signature',
101
            $signature_id
102
        );
103
        return $this->get($url);
104
    }
105
106
    /**
107
     * 删除签名
108
     *
109
     * @param string $signature_id 签名 ID
110
     * @return array
111
     * @link https://developer.qiniu.com/sms/api/5891/sms-api-delete-signature
112
     */
113
    public function deleteSignature($signature_id)
114
    {
115
        $url = $this->baseURL . 'signature/' . $signature_id;
116
        return $this->delete($url);
117
    }
118
119
    /**
120
     * 创建模板
121
     *
122
     * @param string $name 模板名称
123
     * @param string $template 模板内容 可设置自定义变量,发送短信时候使用,参考:${code}
124
     * @param string $type notification:通知类,verification:验证码,marketing:营销类,voice:语音类
125
     * @param string $description 申请理由简述
126
     * @param string $signature_id 已经审核通过的签名
127
     * @return array array
128
     * @link https://developer.qiniu.com/sms/api/5893/sms-api-create-template
129
     */
130
    public function createTemplate(
131
        $name,
132
        $template,
133
        $type,
134
        $description,
135
        $signature_id
136
    ) {
137
        $params['name'] = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
138
        $params['template'] = $template;
139
        $params['type'] = $type;
140
        $params['description'] = $description;
141
        $params['signature_id'] = $signature_id;
142
143
        $body = json_encode($params);
144
        $url = $this->baseURL . 'template';
145
        return $this->post($url, $body);
146
    }
147
148
    /**
149
     * 列出模板
150
     *
151
     * @param string $audit_status 审核状态:passed (通过), rejected (未通过), reviewing (审核中)
152
     * @param int $page 页码。默认为 1
153
     * @param int $page_size 分页大小。默认为 20
154
     * @return array
155
     * @link https://developer.qiniu.com/sms/api/5894/sms-api-query-template
156
     */
157
    public function queryTemplate($audit_status = null, $page = 1, $page_size = 20)
158
    {
159
160
        $url = sprintf(
161
            "%s?audit_status=%s&page=%s&page_size=%s",
162
            $this->baseURL . 'template',
163
            $audit_status,
164
            $page,
165
            $page_size
166
        );
167
        return $this->get($url);
168
    }
169
170
    /**
171
     * 查询单个模版
172
     *
173
     * @param string $template_id 模版ID
174
     * @return array
175
     * @link https://developer.qiniu.com/sms/api/5969/query-a-single-template
176
     */
177
    public function querySingleTemplate($template_id)
178
    {
179
180
        $url = sprintf(
181
            "%s/%s",
182
            $this->baseURL . 'template',
183
            $template_id
184
        );
185
        return $this->get($url);
186
    }
187
188
    /**
189
     * 编辑模板
190
     *
191
     * @param string $id 模板 ID
192
     * @param string $name 模板名称
193
     * @param string $template 模板内容
194
     * @param string $description 申请理由简述
195
     * @param string $signature_id 已经审核通过的签名 ID
196
     * @return array
197
     * @link https://developer.qiniu.com/sms/api/5895/sms-api-edit-template
198
     */
199
    public function updateTemplate(
200
        $id,
201
        $name,
202
        $template,
203
        $description,
204
        $signature_id
205
    ) {
206
        $params['name'] = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
207
        $params['template'] = $template;
208
        $params['description'] = $description;
209
        $params['signature_id'] = $signature_id;
210
        $body = json_encode($params);
211
        $url = $this->baseURL . 'template/' . $id;
212
        return $this->PUT($url, $body);
213
    }
214
215
    /**
216
     * 删除模板
217
     *
218
     * @param string $template_id 模板 ID
219
     * @return array
220
     * @link https://developer.qiniu.com/sms/api/5896/sms-api-delete-template
221
     */
222
    public function deleteTemplate($template_id)
223
    {
224
        $url = $this->baseURL . 'template/' . $template_id;
225
        return $this->delete($url);
226
    }
227
228
    /**
229
     * 发送短信
230
     *
231
     * @param string $template_id 模板 ID
232
     * @param string[] $mobiles 手机号
233
     * @param string $parameters 自定义模板变量,变量设置在创建模板时,参数template指定
234
     * @return array
235
     * @link https://developer.qiniu.com/sms/api/5897/sms-api-send-message
236
     */
237
    public function sendMessage($template_id, $mobiles, $parameters = null)
238
    {
239
        $params['template_id'] = $template_id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
240
        $params['mobiles'] = $mobiles;
241
        if (!empty($parameters)) {
242
            $params['parameters'] = $parameters;
243
        }
244
        $body = json_encode($params);
245
        $url = $this->baseURL . 'message';
246
        return $this->post($url, $body);
247
    }
248
249
    /**
250
     * 查询发送记录
251
     *
252
     * @param string $job_id 发送任务返回的 id
253
     * @param string $message_id 单条短信发送接口返回的 id
254
     * @param string $mobile 接收短信的手机号码
255
     * @param string $status sending: 发送中,success: 发送成功,failed: 发送失败,waiting: 等待发送
256
     * @param string $template_id 模版 id
257
     * @param string $type marketing:营销,notification:通知,verification:验证码,voice:语音
258
     * @param string $start 开始时间,timestamp,例如: 1563280448
259
     * @param int $end 结束时间,timestamp,例如: 1563280471
260
     * @param int $page 页码,默认为 1
261
     * @param int $page_size 每页返回的数据条数,默认20,最大200
262
     * @return array
263
     * @link https://developer.qiniu.com/sms/api/5852/query-send-sms
264
     */
265
    public function querySendSms(
266
        $job_id = null,
267
        $message_id = null,
268
        $mobile = null,
269
        $status = null,
270
        $template_id = null,
271
        $type = null,
272
        $start = null,
273
        $end = null,
274
        $page = 1,
275
        $page_size = 20
276
    ) {
277
        $query = array();
278
        \Qiniu\setWithoutEmpty($query, 'job_id', $job_id);
279
        \Qiniu\setWithoutEmpty($query, 'message_id', $message_id);
280
        \Qiniu\setWithoutEmpty($query, 'mobile', $mobile);
281
        \Qiniu\setWithoutEmpty($query, 'status', $status);
282
        \Qiniu\setWithoutEmpty($query, 'template_id', $template_id);
283
        \Qiniu\setWithoutEmpty($query, 'type', $type);
284
        \Qiniu\setWithoutEmpty($query, 'start', $start);
285
        \Qiniu\setWithoutEmpty($query, 'end', $end);
286
        \Qiniu\setWithoutEmpty($query, 'page', $page);
287
        \Qiniu\setWithoutEmpty($query, 'page_size', $page_size);
288
289
        $url = $this->baseURL . 'messages?' . http_build_query($query);
290
        return $this->get($url);
291
    }
292
293
294
    public function imgToBase64($img_file)
295
    {
296
        $img_base64 = '';
297
        if (file_exists($img_file)) {
298
            $app_img_file = $img_file; // 图片路径
299
            $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
300
            $fp = fopen($app_img_file, "r"); // 图片是否可读权限
301
            if ($fp) {
302
                $filesize = filesize($app_img_file);
303
                if ($filesize > 5 * 1024 * 1024) {
304
                    die("pic size < 5M !");
305
                }
306
                $content = fread($fp, $filesize);
307
                $file_content = chunk_split(base64_encode($content)); // base64编码
308
                switch ($img_info[2]) {           //判读图片类型
309
                    case 1:
310
                        $img_type = 'gif';
311
                        break;
312
                    case 2:
313
                        $img_type = 'jpg';
314
                        break;
315
                    case 3:
316
                        $img_type = 'png';
317
                        break;
318
                }
319
                //合成图片的base64编码
320
                $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;
0 ignored issues
show
Bug introduced by
The variable $img_type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
321
            }
322
            fclose($fp);
323
        }
324
325
        return $img_base64;
326
    }
327
328
    private function get($url, $contentType = 'application/x-www-form-urlencoded')
329
    {
330
        $headers = $this->auth->authorizationV2($url, "GET", null, $contentType);
331
        $headers['Content-Type'] = $contentType;
332
        $ret = Client::get($url, $headers);
333
        if (!$ret->ok()) {
334
            return array(null, new Error($url, $ret));
335
        }
336
        return array($ret->json(), null);
337
    }
338
339
    private function delete($url, $contentType = 'application/json')
340
    {
341
        $headers = $this->auth->authorizationV2($url, "DELETE", null, $contentType);
342
        $headers['Content-Type'] = $contentType;
343
        $ret = Client::delete($url, $headers);
344
        if (!$ret->ok()) {
345
            return array(null, new Error($url, $ret));
346
        }
347
        return array($ret->json(), null);
348
    }
349
350
    private function post($url, $body, $contentType = 'application/json')
351
    {
352
        $headers = $this->auth->authorizationV2($url, "POST", $body, $contentType);
353
354
        $headers['Content-Type'] = $contentType;
355
        $ret = Client::post($url, $body, $headers);
356
        var_dump($body);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($body); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
357
        if (!$ret->ok()) {
358
            return array(null, new Error($url, $ret));
359
        }
360
        $r = ($ret->body === null) ? array() : $ret->json();
361
        return array($r, null);
362
    }
363
364
    private function PUT($url, $body, $contentType = 'application/json')
365
    {
366
        $headers = $this->auth->authorizationV2($url, "PUT", $body, $contentType);
367
        $headers['Content-Type'] = $contentType;
368
        $ret = Client::put($url, $body, $headers);
369
        if (!$ret->ok()) {
370
            return array(null, new Error($url, $ret));
371
        }
372
        $r = ($ret->body === null) ? array() : $ret->json();
373
        return array($r, null);
374
    }
375
}
376