Passed
Push — master ( 10b845...c93f04 )
by du
01:52
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
16
     */
17
    protected $ak;
18
19
    /**
20
     * @var string
21
     */
22
    protected $url = 'http://api.map.baidu.com/location/ip';
23
24 10
    public function __construct($config = [])
25
    {
26 10
        $this->setConfig($config);
27 10
    }
28
29
    /**
30
     * @param array $config
31
     *
32
     * @return IpImp
33
     *
34
     * @author duc <[email protected]>
35
     */
36 10
    public function setConfig($config = []): IpImp
37
    {
38 10
        $this->ak = isset($config['ak']) ? $config['ak'] : '';
39
40 10
        return $this;
41
    }
42
43
    /**
44
     * @return mixed
45
     */
46 6
    public function getAk()
47
    {
48 6
        return $this->ak;
49
    }
50
51
    /**
52
     * @param ClientInterface $client
53
     * @param string          $ip
54
     *
55
     * @return array
56
     * @throws InvalidArgumentException
57
     * @throws ServerErrorException
58
     *
59
     * @author duc <[email protected]>
60
     */
61 5
    public function send(ClientInterface $client, string $ip): array
62
    {
63
        try {
64 5
            $originalStr = $client->request('get', $this->url . '?ip=' . $ip . '&ak=' . $this->getAk())
65 3
                ->getBody();
66
67 3
            $result = json_decode($originalStr, true);
68
69 3
            if ($result['status'] !== 0) {
70 2
                throw new InvalidArgumentException($result['message']);
71
            }
72
73 1
            $data = ['ip' => $ip];
74 1
            $data = $this->formatResult($data, $result);
75
76 1
            return $data;
77 4
        } catch (ServerException $e) {
78 1
            throw new ServerErrorException($e->getMessage());
79 3
        } catch (ClientException $e) {
80 1
            throw new ServerErrorException($e->getMessage());
81
        }
82
    }
83
84
    /**
85
     * @param array $data
86
     * @param array $result
87
     *
88
     * @return mixed
89
     *
90
     * @author duc <[email protected]>
91
     */
92 1
    public function formatResult(array $data, array $result)
93
    {
94 1
        $data['country'] = '中国';
95 1
        $data['isp'] = '';
96 1
        $data['address'] = $data['country'] . $result['content']['address'];
97 1
        $data['region'] = $result['content']['address_detail']['province'];
98 1
        $data['city'] = $result['content']['address_detail']['city'];
99 1
        $data['point_x'] = $result['content']['point']['x'];
100 1
        $data['point_y'] = $result['content']['point']['y'];
101
102 1
        return $data;
103
    }
104
}
105