Completed
Push — master ( caef60...751e2d )
by Elf
04:35
created

MobSms::verify()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 23
nc 7
nop 1
dl 0
loc 37
ccs 0
cts 23
cp 0
crap 42
rs 8.439
c 1
b 0
f 0
1
<?php
2
3
namespace App\Support\Vendor;
4
5
use App\Exceptions\ActionFailureException;
6
use App\Exceptions\InvalidInputException;
7
use Exception;
8
9
class MobSms
10
{
11
    /**
12
     * MobSMS 短信验证码。
13
     *
14
     * @see http://wiki.mob.com/webapi2-0/
15
     *
16
     * @param  array  $credentials  `['phone', 'code', 'zone']`
17
     *
18
     * @throws \App\Exceptions\InvalidInputException
19
     * @throws \App\Exceptions\ActionFailureException
20
     */
21
    public static function verify($credentials = [])
22
    {
23
        $credentials['appkey'] = config('services.mobsms.key');
24
25
        try {
26
            $response = (new HttpClient([
27
                'connect_timeout' => 5,
28
                'timeout' => 20,
29
            ]))
30
            ->post('https://webapi.sms.mob.com/sms/verify', [
31
                'form_params' => $credentials,
32
            ])
33
            ->getBody();
34
35
            $response = json_decode($response, true);
36
        } catch (Exception $e) {
37
            throw new ActionFailureException('短信网关请求失败');
38
        }
39
40
        if (! is_array($response)) {
41
            throw new ActionFailureException('短信网关数据异常[sms]');
42
        }
43
44
        $status = (int) array_get($response, 'status', -1);
45
46
        if (200 === $status) {
47
            return;
48
        }
49
50
        if (457 === $status) {
51
            throw new InvalidInputException('请填写正确的手机号', 10);
52
        } elseif (467 === $status) {
53
            throw new InvalidInputException('请求验证码过于频繁,请稍后再试');
54
        } else {
55
            throw new InvalidInputException('验证码无效,请重新获取');
56
        }
57
    }
58
}
59