Passed
Push — master ( 10b845...c93f04 )
by du
01:52
created

TaobaoIp   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 23
dl 0
loc 56
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A formatResult() 0 11 1
A send() 0 21 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\ServerErrorException;
10
11
class TaobaoIp implements IpImp
12
{
13
    protected $url = 'http://ip.taobao.com/service/getIpInfo.php';
14
15
    /**
16
     * @param ClientInterface $httpClient
17
     *
18
     * @param string          $ip
19
     *
20
     * @return array
21
     * @throws ServerErrorException
22
     * @author duc <[email protected]>
23
     */
24 5
    public function send(ClientInterface $httpClient, string $ip):array
25
    {
26
        try {
27 5
            $originalStr = $httpClient->request('get', $this->url . '?ip=' . $ip)
28 3
                ->getBody();
29
30 3
            $result = json_decode($originalStr, true);
31
32 3
            if ($result['code'] !== 0) {
33 1
                throw new ServerErrorException();
34
            }
35
36 2
            $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...
37
38 2
            $data = $this->formatResult($data, $result);
39
40 2
            return $data;
41 3
        } catch (ServerException $e) {
42 1
            throw new ServerErrorException();
43 2
        } catch (ClientException $e) {
44 1
            throw new ServerErrorException();
45
        }
46
    }
47
48
    /**
49
     * @param array $result
50
     * @param array $data
51
     *
52
     * @return array
53
     *
54
     * @author duc <[email protected]>
55
     */
56 2
    public function formatResult(array $data, array $result)
57
    {
58 2
        $data['country'] = $result['data']['country'];
59 2
        $data['region'] = $result['data']['region'];
60 2
        $data['city'] = $result['data']['city'];
61 2
        $data['address'] = $data['country'] . $data['region'] . $data['city'];
62 2
        $data['point_x'] = '';
63 2
        $data['point_y'] = '';
64 2
        $data['isp'] = $result['data']['isp'];
65
66 2
        return $data;
67
    }
68
}
69