TencentIp::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
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\BreakLoopException;
10
use DucCnzj\Ip\Exceptions\ServerErrorException;
11
12
/**
13
 *
14
 * Class TencentIp
15
 *
16
 * @package DucCnzj\Ip\Strategies
17
 */
18
class TencentIp implements IpImp
19
{
20
    /**
21
     * @var string
22
     */
23
    public $url = 'https://apis.map.qq.com/ws/location/v1/ip';
24
25
    /**
26
     * @var string
27
     */
28
    public $key;
29
30
    /**
31
     * TencentIp constructor.
32
     *
33
     * @param $config
34
     */
35 5
    public function __construct($config = [])
36
    {
37 5
        $this->setConfig($config);
38 5
    }
39
40
    /**
41
     * @param array|string $config
42
     *
43
     * @return $this
44
     *
45
     * @author duc <[email protected]>
46
     */
47 5
    public function setConfig($config)
48
    {
49 5
        if (is_array($config)) {
50 5
            $this->key = isset($config['key']) ? $config['key'] : '';
51
        } else {
52 2
            $this->key = $config;
53
        }
54
55 5
        return $this;
56
    }
57
58
    /**
59
     * @param ClientInterface $httpClient
60
     * @param string $ip
61
     *
62
     * @return array
63
     * @throws BreakLoopException
64
     * @throws ServerErrorException
65
     * @throws \GuzzleHttp\Exception\GuzzleException
66
     *
67
     * @author duc <[email protected]>
68
     */
69 4
    public function send(ClientInterface $httpClient, string $ip): array
70
    {
71
        try {
72 4
            $originalStr = $httpClient->request('get', $this->url . '?ip=' . $ip . '&key=' . $this->getKey())
73 2
                ->getBody();
74
75 2
            $result = json_decode($originalStr, true);
76
77 2
            if ($result['status'] !== 0) {
78 1
                throw new BreakLoopException($result['message']);
79
            }
80
81 1
            $data['ip'] = $ip;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
82
83 1
            $data = $this->formatResult($data, $result);
84
85 1
            return $data;
86 3
        } catch (ServerException $e) {
87 1
            throw new ServerErrorException();
88 2
        } catch (ClientException $e) {
89 1
            throw new BreakLoopException();
90
        }
91
    }
92
93
    /**
94
     * @return string
95
     *
96
     * @author duc <[email protected]>
97
     */
98 5
    public function getKey()
99
    {
100 5
        return $this->key;
101
    }
102
103
    /**
104
     * @param array $result
105
     * @param array $data
106
     *
107
     * @return array
108
     *
109
     * @author duc <[email protected]>
110
     */
111 1
    public function formatResult(array $data, array $result)
112
    {
113 1
        $data['country'] = $result['result']['ad_info']['nation'];
114 1
        $data['region'] = $result['result']['ad_info']['province'];
115 1
        $data['city'] = $result['result']['ad_info']['city'];
116 1
        $data['address'] = $data['country'] . $data['region'] . $data['city'];
117 1
        $data['point_x'] = $result['result']['location']['lng'];
118 1
        $data['point_y'] = $result['result']['location']['lat'];
119 1
        $data['isp'] = '';
120
121 1
        return $data;
122
    }
123
}
124