Completed
Push — master ( 279d4e...5d09e0 )
by Elf
02:49
created

Sms   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 2

1 Method

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