Passed
Push — master ( 84fe4e...38ed25 )
by du
01:45
created

AliIp   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
wmc 11
eloc 41
dl 0
loc 119
ccs 40
cts 41
cp 0.9756
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAppCode() 0 3 1
A setConfig() 0 9 3
A formatResult() 0 16 1
A send() 0 31 5
A __construct() 0 3 1
1
<?php
2
3
namespace DucCnzj\Ip\Strategies;
4
5
use DucCnzj\Ip\Imp\IpImp;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Exception\ClientException;
8
use GuzzleHttp\Exception\ServerException;
9
use DucCnzj\Ip\Exceptions\AnalysisException;
10
use DucCnzj\Ip\Exceptions\ServerErrorException;
11
use DucCnzj\Ip\Exceptions\UnauthorizedException;
12
13
class AliIp implements IpImp
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $appCode;
19
20
    /**
21
     * @var string
22
     */
23
    protected $url = 'http://iploc.market.alicloudapi.com/v3/ip';
24
25
    /**
26
     * AliIp constructor.
27
     *
28
     * @param array $config
29
     */
30 10
    public function __construct($config = [])
31
    {
32 10
        $this->setConfig($config);
33 10
    }
34
35
    /**
36
     * @param array|string $config
37
     *
38
     * @return IpImp
39
     *
40
     * @author duc <[email protected]>
41
     */
42 10
    public function setConfig($config): IpImp
43
    {
44 10
        if (is_array($config)) {
45 10
            $this->appCode = isset($config['app_code']) ? $config['app_code'] : '';
46
        } else {
47
            $this->appCode = $config;
48
        }
49
50 10
        return $this;
51
    }
52
53
    /**
54
     * @return string
55
     *
56
     * @author duc <[email protected]>
57
     */
58 6
    public function getAppCode()
59
    {
60 6
        return $this->appCode;
61
    }
62
63
    /**
64
     * @param ClientInterface $client
65
     * @param string          $ip
66
     *
67
     * @return array
68
     * @throws AnalysisException
69
     * @throws ServerErrorException
70
     * @throws UnauthorizedException
71
     *
72
     * @author duc <[email protected]>
73
     */
74 5
    public function send(ClientInterface $client, string $ip): array
75
    {
76
        try {
77 5
            $originalStr = $client->request(
78 5
                'get',
79 5
                $this->url . '?ip=' . $ip,
80
                [
81
                'headers' => [
82 5
                    'Authorization' => "APPCODE {$this->getAppCode()}",
83
                ],
84
            ]
85 2
            )->getBody();
86
87 2
            $result = json_decode($originalStr, true);
88
89 2
            if ($result['rectangle'] === []) {
90 1
                throw new AnalysisException('ip 地址解析失败');
91
            }
92
93 1
            $data = ['ip' => $ip];
94 1
            $data = $this->formatResult($data, $result);
95
96 1
            return $data;
97 4
        } catch (ServerException $e) {
98 1
            throw new ServerErrorException($e->getMessage());
99 3
        } catch (ClientException $e) {
100 2
            if ($e->getResponse()->getStatusCode() === 401) {
101 1
                throw new UnauthorizedException($e->getMessage());
102
            }
103
104 1
            throw new ServerErrorException($e->getMessage());
105
        }
106
    }
107
108
    /**
109
     * @param array $data
110
     * @param array $result
111
     *
112
     * @return array
113
     *
114
     * @author duc <[email protected]>
115
     */
116 1
    public function formatResult(array $data, array $result)
117
    {
118
        // 116.0119343,39.66127144;116.7829835,40.2164962
119 1
        $arr = explode(';', $result['rectangle']);
120 1
        $rectangle = explode(',', $arr[0]);
121 1
        $pointX = $rectangle[0];
122 1
        $pointY = $rectangle[1];
123 1
        $data['city'] = $result['city'];
124 1
        $data['region'] = $result['province'];
125 1
        $data['country'] = '中国';
126 1
        $data['point_x'] = $pointX;
127 1
        $data['point_y'] = $pointY;
128 1
        $data['isp'] = '';
129 1
        $data['address'] = $data['country'] . $data['region'] . $data['city'];
130
131 1
        return $data;
132
    }
133
}
134