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

Sms   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 45
ccs 0
cts 28
cp 0
rs 10
wmc 9
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D verify() 0 32 9
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