Completed
Push — master ( fbfec6...5d8274 )
by Elf
05:33
created

Sms::verify()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 22
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 34
ccs 0
cts 26
cp 0
crap 90
rs 4.909
1
<?php
2
3
namespace ElfSundae\Laravel\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, ['appkey' => config('services.mobsms.key')]))
25
            ->fetchJson('/sms/verify', 'POST');
26
27
        if (! is_array($response)) {
28
            throw new ActionFailureException('短信网关请求失败');
29
        }
30
31
        $status = (int) array_get($response, 'status', -1);
32
33
        switch ($status) {
34
            case 200:
35
            return;
36
37
            case 405:
38
            case 406:
39
            case 474:
40
            throw new ActionFailureException('Server Error #'.$status);
41
42
            case 457:
43
            throw new InvalidInputException('请填写正确的手机号', 10);
44
45
            case 466:
46
            throw new InvalidInputException('验证码不能为空');
47
48
            case 467:
49
            throw new InvalidInputException('请求验证码过于频繁,请稍后再试');
50
51
            default:
52
            throw new InvalidInputException('验证码无效,请重新获取');
53
        }
54
    }
55
}
56