Passed
Push — master ( 60463b...4b41f7 )
by Davide
05:35 queued 03:02
created

Ipinfo::jsonDecodeResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace DavidePastore\Ipinfo;
4
5
/**
6
 * ipinfo.io service wrapper.
7
 *
8
 * @author davidepastore
9
 */
10
class Ipinfo
11
{
12
    /**
13
     * The base url of the ipinfo service.
14
     *
15
     * @var string
16
     */
17
    const BASE_URL = 'http://ipinfo.io/';
18
19
    /**
20
     * The ip string.
21
     *
22
     * @var string
23
     */
24
    const IP = 'ip';
25
26
    /**
27
     * The hostname string.
28
     *
29
     * @var string
30
     */
31
    const HOSTNAME = 'hostname';
32
33
    /**
34
     * The loc string.
35
     *
36
     * @var string
37
     */
38
    const LOC = 'loc';
39
40
    /**
41
     * The org string.
42
     *
43
     * @var string
44
     */
45
    const ORG = 'org';
46
47
    /**
48
     * The city string.
49
     *
50
     * @var string
51
     */
52
    const CITY = 'city';
53
54
    /**
55
     * The region string.
56
     *
57
     * @var string
58
     */
59
    const REGION = 'region';
60
61
    /**
62
     * The country string.
63
     *
64
     * @var string
65
     */
66
    const COUNTRY = 'country';
67
68
    /**
69
     * The phone string.
70
     *
71
     * @var string
72
     */
73
    const PHONE = 'phone';
74
75
    /**
76
     * The geo string.
77
     *
78
     * @var string
79
     */
80
    const GEO = 'geo';
81
82
    /**
83
     * The postal string.
84
     *
85
     * @var string
86
     */
87
    const POSTAL = 'postal';
88
89
    /**
90
     * All the settings.
91
     *
92
     * @var array
93
     */
94
    protected $settings;
95
96
    /**
97
     * Create an Ipinfo instance.
98
     *
99
     * @param array $settings An array with all the settings.
100
     *                        Supported keys are:
101
     *                        - token: string the developer token;
102
     *                        - debug: boolean active or not the debug.
103
     */
104 9
    public function __construct($settings = array())
105
    {
106
        //Merge user settings
107 9
        $this->settings = array_merge(array(
108 9
                'token' => '',
109 9
                'debug' => false,
110 9
        ), $settings);
111 9
    }
112
113
    /**
114
     * Get all the info about your own ip address.
115
     *
116
     * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
117
     */
118 1
    public function getYourOwnIpDetails()
119
    {
120 1
        $response = $this->makeCurlRequest($this::BASE_URL.'json');
121 1
        $response = $this->jsonDecodeResponse($response);
122
123 1
        return new Host($response);
124
    }
125
126
    /**
127
     * Get all the info about an ip address.
128
     *
129
     * @param string $ipAddress The ip address.
130
     *
131
     * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
132
     */
133 2
    public function getFullIpDetails($ipAddress)
134
    {
135 2
        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress);
136 2
        $response = $this->jsonDecodeResponse($response);
137
138 2
        return new Host($response);
139
    }
140
141
    /**
142
     * Get a specific field value.
143
     *
144
     * @param string $ipAddress The ip address.
145
     * @param string $field     The field.
146
     *
147
     * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip.
148
     *                                           This could returns an Host object if you call it with for the field
149
     *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
150
     */
151 5 View Code Duplication
    public function getSpecificField($ipAddress, $field)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
    {
153 5
        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field);
154 5
        $response = $this->checkGeo($field, $response);
155
156 5
        return $response;
157
    }
158
159
    /**
160
     * Get a specific field value of your own ip address.
161
     *
162
     * @param string $field The field.
163
     *
164
     * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip.
165
     *                                           This could returns an Host object if you call it with for the field
166
     *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
167
     */
168 1 View Code Duplication
    public function getYourOwnIpSpecificField($field)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170 1
        $response = $this->makeCurlRequest($this::BASE_URL.$field);
171 1
        $response = $this->checkGeo($field, $response);
172
173 1
        return $response;
174
    }
175
176
    /**
177
     * Use the /geo call to get just the geolocation information, which will often be
178
     * faster than getting the full response.
179
     *
180
     * @param string $ipAddress The ip address.
181
     *
182
     * @return \DavidePastore\Ipinfo\Host
183
     */
184 2
    public function getIpGeoDetails($ipAddress)
185
    {
186 2
        return $this->getSpecificField($ipAddress, $this::GEO);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getSpecificField($ipAddress, $this::GEO); of type string|DavidePastore\Ipinfo\Host adds the type string to the return on line 186 which is incompatible with the return type documented by DavidePastore\Ipinfo\Ipinfo::getIpGeoDetails of type DavidePastore\Ipinfo\Host.
Loading history...
187
    }
188
189
    /**
190
     * Check if the response is GEO and set the parameters accordingly.
191
     *
192
     * @param string $field    The field value.
193
     * @param string $response The response from the server.
194
     *
195
     * @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is
196
     *                  of the GEO type, a string otherwise. If the field value is different from the GEO type, it will
197
     *                  delete the last character ('\n').
198
     */
199 6
    private function checkGeo($field, $response)
200
    {
201 6
        if ($field == $this::GEO) {
202 2
            $response = $this->jsonDecodeResponse($response);
203 2
            $response = new Host($response);
204 2
        } else {
205 4
            $response = substr($response, 0, -1);
206
        }
207
208 6
        return $response;
209
    }
210
211
    /**
212
     * Make a curl request.
213
     *
214
     * @param string $address The address of the request.
215
     *
216
     * @return string Returns the response from the request.
217
     */
218 9
    private function makeCurlRequest($address)
219
    {
220 9
        $curl = curl_init();
221
222 9
        if (!empty($this->settings['token'])) {
223 1
            $address .= '?token='.$this->settings['token'];
224 1
        }
225
226 9
        if ($this->settings['debug']) {
227 1
            echo 'Request address: '.$address."\n";
228 1
        }
229
230 9
        curl_setopt_array($curl, array(
231 9
            CURLOPT_RETURNTRANSFER => 1,
232 9
            CURLOPT_URL => $address,
233 9
        ));
234
235 9
        $response = curl_exec($curl);
236
237 9
        curl_close($curl);
238
239 9
        return $response;
240
    }
241
242
    /**
243
     * Returns the json decoded associative array.
244
     * @param  string $response Response from the http call.
245
     * @return array           Returns the associative array with the response.
246
     */
247 5
    private function jsonDecodeResponse($response)
248
    {
249 5
        if ($response) {
250 4
            $response = json_decode($response, true);
251 4
        } else {
252 1
            $response = array();
253
        }
254 5
        return $response;
255
    }
256
}
257