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

Sms   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

1 Method

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