BaiduIp::send()   A
last analyzed

Complexity

Conditions 4
Paths 11

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

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