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

BaiduIp::formatResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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\ServerErrorException;
10
use DucCnzj\Ip\Exceptions\InvalidArgumentException;
11
12
class BaiduIp implements IpImp
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $ak;
18
19
    /**
20
     * @var string
21
     */
22
    protected $url = 'http://api.map.baidu.com/location/ip';
23
24
    /**
25
     * BaiduIp constructor.
26
     *
27
     * @param array $config
28
     */
29 9
    public function __construct($config = [])
30
    {
31 9
        $this->setConfig($config);
32 9
    }
33
34
    /**
35
     * @param array|string $config
36
     *
37
     * @return IpImp
38
     *
39
     * @author duc <[email protected]>
40
     */
41 9
    public function setConfig($config): IpImp
42
    {
43 9
        if (is_array($config)) {
44 9
            $this->ak = isset($config['ak']) ? $config['ak'] : '';
45
        } else {
46
            $this->ak = $config;
47
        }
48
49 9
        return $this;
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55 5
    public function getAk()
56
    {
57 5
        return $this->ak;
58
    }
59
60
    /**
61
     * @param ClientInterface $client
62
     * @param string          $ip
63
     *
64
     * @return array
65
     * @throws InvalidArgumentException
66
     * @throws ServerErrorException
67
     *
68
     * @author duc <[email protected]>
69
     */
70 4
    public function send(ClientInterface $client, string $ip): array
71
    {
72
        try {
73 4
            $originalStr = $client->request('get', $this->url . '?ip=' . $ip . '&ak=' . $this->getAk())
74 2
                ->getBody();
75
76 2
            $result = json_decode($originalStr, true);
77
78 2
            if ($result['status'] !== 0) {
79 1
                throw new InvalidArgumentException($result['message']);
80
            }
81
82 1
            $data = ['ip' => $ip];
83 1
            $data = $this->formatResult($data, $result);
84
85 1
            return $data;
86 3
        } catch (ServerException $e) {
87 1
            throw new ServerErrorException($e->getMessage());
88 2
        } catch (ClientException $e) {
89 1
            throw new ServerErrorException($e->getMessage());
90
        }
91
    }
92
93
    /**
94
     * @param array $data
95
     * @param array $result
96
     *
97
     * @return mixed
98
     *
99
     * @author duc <[email protected]>
100
     */
101 1
    public function formatResult(array $data, array $result)
102
    {
103 1
        $data['country'] = '中国';
104 1
        $data['isp'] = '';
105 1
        $data['address'] = $data['country'] . $result['content']['address'];
106 1
        $data['region'] = $result['content']['address_detail']['province'];
107 1
        $data['city'] = $result['content']['address_detail']['city'];
108 1
        $data['point_x'] = $result['content']['point']['x'];
109 1
        $data['point_y'] = $result['content']['point']['y'];
110
111 1
        return $data;
112
    }
113
}
114