Completed
Push — master ( 8c9c54...6d4889 )
by Elf
44:15
created

Sms::verify()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 28
cp 0
rs 4.909
c 0
b 0
f 0
cc 9
eloc 23
nc 9
nop 1
crap 90
1
<?php
2
3
namespace ElfSundae\Support\Services\Mob;
4
5
use ElfSundae\HttpClient;
6
use ElfSundae\Laravel\Api\Exceptions\InvalidInputException;
7
use ElfSundae\Laravel\Api\Exceptions\ActionFailureException;
8
9
class Sms
10
{
11
    /**
12
     * Mob SMS verification.
13
     *
14
     * @see http://wiki.mob.com/webapi2-0/
15
     *
16
     * @param  array  $credentials  `['phone', 'code', 'zone']`
17
     * @return void
18
     *
19
     * @throws \ElfSundae\Laravel\Api\Exceptions\ApiResponseException
20
     */
21
    public static function verify($credentials = [])
22
    {
23
        $response = (new HttpClient('https://webapi.sms.mob.com/'))
24
            ->formParams(array_merge($credentials, [
25
                'appkey' => config('services.mobsms.key'),
26
            ]))
27
            ->fetchJson('/sms/verify', 'POST');
28
29
        if (! is_array($response)) {
30
            throw new ActionFailureException('短信网关请求失败');
31
        }
32
33
        $status = (int) array_get($response, 'status', -1);
34
35
        switch ($status) {
36
            case 200:
37
            return;
38
39
            case 405:
40
            case 406:
41
            case 474:
42
            throw new ActionFailureException('Server Error #'.$status);
43
            case 457:
44
            throw new InvalidInputException('请填写正确的手机号', 10);
45
            case 466:
46
            throw new InvalidInputException('验证码不能为空');
47
            case 467:
48
            throw new InvalidInputException('请求验证码过于频繁,请稍后再试');
49
            default:
50
            throw new InvalidInputException('验证码无效,请重新获取');
51
        }
52
    }
53
}
54