Completed
Branch master (eb8d44)
by José Ginés
01:13
created

IpinfoApi   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 98
ccs 0
cts 38
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getYourOwnIpDetails() 0 6 1
A getFullIpDetails() 0 6 1
A getIpGeoDetails() 0 6 1
A sendRequest() 0 22 2
1
<?php
2
3
namespace IpInfo\Lib;
4
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
7
/**
8
 * Class IpinfoApi, ipinfo.io service wrapper.
9
 *
10
 * @package IpInfo\Lib
11
 */
12
class IpinfoApi
13
{
14
    const BASE_URL = 'http://ipinfo.io/';
15
    const GEO      = 'geo';
16
    const TOKEN    = 'token';
17
18
19
    /** @var array  */
20
    protected $settings;
21
22
23
    /**
24
     * IpinfoApi constructor.
25
     * @param array|null $settings An array with all the settings.
26
     *                        Supported keys are:
27
     *                        - token: string the developer token;
28
     */
29
    public function __construct($settings = array())
30
    {
31
        $this->settings = array_merge(array(
32
                static::TOKEN => '',
33
        ), $settings);
34
    }
35
36
37
    /**
38
     * Get all the info about your own ip address.
39
     *
40
     * @return IpinfoResponse
41
     */
42
    public function getYourOwnIpDetails()
43
    {
44
        $response = $this->sendRequest($this::BASE_URL.'json');
45
46
        return new IpinfoResponse($response);
47
    }
48
49
50
    /**
51
     * Get all the info about an ip address.
52
     *
53
     * @param string $ipAddress
54
     * @return IpinfoResponse
55
     */
56
    public function getFullIpDetails($ipAddress)
57
    {
58
        $response = $this->sendRequest($this::BASE_URL.$ipAddress);
59
60
        return new IpinfoResponse($response);
61
    }
62
63
64
    /**
65
     * Use the /geo call to get just the geolocation information, which will often be
66
     * faster than getting the full response.
67
     *
68
     * @param string $ipAddress
69
     *
70
     * @return IpinfoResponse
71
     */
72
    public function getIpGeoDetails($ipAddress)
73
    {
74
        $response = $this->sendRequest($this::BASE_URL.$ipAddress.'/'.static::GEO);
75
76
        return new IpinfoResponse($response);
77
    }
78
79
80
    /**
81
     * Make a curl request.
82
     *
83
     * @param string $address The address of the request.
84
     *
85
     * @return JsonResponse Returns the response from the request.
86
     */
87
    protected function sendRequest($address)
88
    {
89
        $curl = curl_init();
90
91
        if (!empty($this->settings[static::TOKEN])) {
92
            $address .= '?token='.$this->settings[static::TOKEN];
93
        }
94
95
        curl_setopt_array($curl, array(
96
            CURLOPT_HEADER         => 0,
97
            CURLINFO_HEADER_OUT    => 1,
98
            CURLOPT_RETURNTRANSFER => 1,
99
            CURLOPT_URL            => $address,
100
        ));
101
102
        $response = curl_exec($curl);
103
        $headers  = curl_getinfo($curl);
104
105
        curl_close($curl);
106
107
        return new JsonResponse(json_decode($response, true), $headers['http_code'], $headers);
108
    }
109
}
110