Passed
Push — master ( 0695bf...48cd6e )
by Davide
47s queued 10s
created
src/DavidePastore/Ipinfo/Ipinfo.php 2 patches
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
      */
125 125
     public function getYourOwnIpDetails()
126 126
     {
127
-        $response = $this->makeCurlRequest($this::BASE_URL.'json');
127
+        $response = $this->makeCurlRequest($this::BASE_URL . 'json');
128 128
         $response = $this->jsonDecodeResponse($response);
129 129
 
130 130
         return new Host($response);
@@ -141,7 +141,7 @@  discard block
 block discarded – undo
141 141
      */
142 142
     public function getFullIpDetails($ipAddress)
143 143
     {
144
-        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress);
144
+        $response = $this->makeCurlRequest($this::BASE_URL . $ipAddress);
145 145
         $response = $this->jsonDecodeResponse($response);
146 146
 
147 147
         return new Host($response);
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
      */
162 162
     public function getSpecificField($ipAddress, $field)
163 163
     {
164
-        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field);
164
+        $response = $this->makeCurlRequest($this::BASE_URL . $ipAddress . '/' . $field);
165 165
         $response = $this->checkGeo($field, $response);
166 166
 
167 167
         return $response;
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
      */
181 181
     public function getYourOwnIpSpecificField($field)
182 182
     {
183
-        $response = $this->makeCurlRequest($this::BASE_URL.$field);
183
+        $response = $this->makeCurlRequest($this::BASE_URL . $field);
184 184
         $response = $this->checkGeo($field, $response);
185 185
 
186 186
         return $response;
@@ -238,11 +238,11 @@  discard block
 block discarded – undo
238 238
         $curl = curl_init();
239 239
 
240 240
         if (!empty($this->settings['token'])) {
241
-            $address .= '?token='.$this->settings['token'];
241
+            $address .= '?token=' . $this->settings['token'];
242 242
         }
243 243
 
244 244
         if ($this->settings['debug']) {
245
-            echo 'Request address: '.$address."\n";
245
+            echo 'Request address: ' . $address . "\n";
246 246
         }
247 247
 
248 248
         curl_setopt_array(
@@ -262,7 +262,7 @@  discard block
 block discarded – undo
262 262
             $errorMessage = curl_error($curl);
263 263
 
264 264
             if ($this->settings['debug']) {
265
-                echo "The error is".$errorMessage;
265
+                echo "The error is" . $errorMessage;
266 266
             }
267 267
         }
268 268
 
Please login to merge, or discard this patch.
Indentation   +307 added lines, -307 removed lines patch added patch discarded remove patch
@@ -14,311 +14,311 @@
 block discarded – undo
14 14
  */
15 15
 class Ipinfo
16 16
 {
17
-    /**
18
-     * The base url of the ipinfo service.
19
-     *
20
-     * @var string
21
-     */
22
-    const BASE_URL = 'https://ipinfo.io/';
23
-
24
-    /**
25
-     * The ip string.
26
-     *
27
-     * @var string
28
-     */
29
-    const IP = 'ip';
30
-
31
-    /**
32
-     * The hostname string.
33
-     *
34
-     * @var string
35
-     */
36
-    const HOSTNAME = 'hostname';
37
-
38
-    /**
39
-     * The loc string.
40
-     *
41
-     * @var string
42
-     */
43
-    const LOC = 'loc';
44
-
45
-    /**
46
-     * The org string.
47
-     *
48
-     * @var string
49
-     */
50
-    const ORG = 'org';
51
-
52
-    /**
53
-     * The city string.
54
-     *
55
-     * @var string
56
-     */
57
-    const CITY = 'city';
58
-
59
-    /**
60
-     * The region string.
61
-     *
62
-     * @var string
63
-     */
64
-    const REGION = 'region';
65
-
66
-    /**
67
-     * The country string.
68
-     *
69
-     * @var string
70
-     */
71
-    const COUNTRY = 'country';
72
-
73
-    /**
74
-     * The phone string.
75
-     *
76
-     * @var string
77
-     */
78
-    const PHONE = 'phone';
79
-
80
-    /**
81
-     * The geo string.
82
-     *
83
-     * @var string
84
-     */
85
-    const GEO = 'geo';
86
-
87
-    /**
88
-     * The postal string.
89
-     *
90
-     * @var string
91
-     */
92
-    const POSTAL = 'postal';
93
-
94
-    /**
95
-     * All the settings.
96
-     *
97
-     * @var array
98
-     */
99
-    protected $settings;
100
-
101
-    /**
102
-     * Create an Ipinfo instance.
103
-     *
104
-     * @param array $settings An array with all the settings.
105
-     *                        Supported keys are:
106
-     *                        - token: string the developer token;
107
-     *                        - debug: boolean active or not the debug.
108
-     */
109
-    public function __construct($settings = array())
110
-    {
111
-        //Merge user settings
112
-        $this->settings = array_merge(array(
113
-                'token' => '',
114
-                'debug' => false,
115
-                'curlOptions' => array()
116
-        ), $settings);
117
-    }
118
-
119
-    /**
120
-     * Get all the info about your own ip address.
121
-     *
122
-     * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
123
-     * @throws InvalidTokenException
124
-     * @throws RateLimitExceedException
125
-     * @throws WrongIpException
126
-     */
127
-    public function getYourOwnIpDetails()
128
-    {
129
-        $response = $this->makeCurlRequest($this::BASE_URL.'json');
130
-        $response = $this->jsonDecodeResponse($response);
131
-
132
-        return new Host($response);
133
-    }
134
-
135
-    /**
136
-     * Get all the info about an ip address.
137
-     *
138
-     * @param string $ipAddress The ip address.
139
-     *
140
-     * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
141
-     * @throws InvalidTokenException
142
-     * @throws RateLimitExceedException
143
-     * @throws WrongIpException
144
-     */
145
-    public function getFullIpDetails($ipAddress)
146
-    {
147
-        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress);
148
-        $response = $this->jsonDecodeResponse($response);
149
-
150
-        return new Host($response);
151
-    }
152
-
153
-    /**
154
-     * Get a specific field value.
155
-     *
156
-     * @param string $ipAddress The ip address.
157
-     * @param string $field The field.
158
-     *
159
-     * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip.
160
-     *                                           This could returns an Host object if you call it with for the field
161
-     *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
162
-     * @throws InvalidTokenException
163
-     * @throws RateLimitExceedException
164
-     * @throws WrongIpException
165
-     */
166
-    public function getSpecificField($ipAddress, $field)
167
-    {
168
-        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field);
169
-        $response = $this->checkGeo($field, $response);
170
-
171
-        return $response;
172
-    }
173
-
174
-    /**
175
-     * Get a specific field value of your own ip address.
176
-     *
177
-     * @param string $field The field.
178
-     *
179
-     * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip.
180
-     *                                           This could returns an Host object if you call it with for the field
181
-     *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
182
-     * @throws InvalidTokenException
183
-     * @throws RateLimitExceedException
184
-     * @throws WrongIpException
185
-     */
186
-    public function getYourOwnIpSpecificField($field)
187
-    {
188
-        $response = $this->makeCurlRequest($this::BASE_URL.$field);
189
-        $response = $this->checkGeo($field, $response);
190
-
191
-        return $response;
192
-    }
193
-
194
-    /**
195
-     * Use the /geo call to get just the geolocation information, which will often be
196
-     * faster than getting the full response.
197
-     *
198
-     * @param string $ipAddress The ip address.
199
-     *
200
-     * @return \DavidePastore\Ipinfo\Host
201
-     * @throws InvalidTokenException
202
-     * @throws RateLimitExceedException
203
-     * @throws WrongIpException
204
-     */
205
-    public function getIpGeoDetails($ipAddress)
206
-    {
207
-        return $this->getSpecificField($ipAddress, $this::GEO);
208
-    }
209
-
210
-    /**
211
-     * Check if the response is GEO and set the parameters accordingly.
212
-     *
213
-     * @param string $field The field value.
214
-     * @param string $response The response from the server.
215
-     *
216
-     * @return \DavidePastore\Ipinfo\Host|string Returns an Host object if the request is
217
-     *                  of the GEO type, a string otherwise. If the field value is different from the GEO type, it will
218
-     *                  delete the last character ('\n').
219
-     * @throws InvalidTokenException
220
-     * @throws RateLimitExceedException
221
-     * @throws WrongIpException
222
-     */
223
-    private function checkGeo($field, $response)
224
-    {
225
-        if ($field == $this::GEO) {
226
-            $response = $this->jsonDecodeResponse($response);
227
-            $response = new Host($response);
228
-        } else {
229
-            $this->checkErrors($response);
230
-            $response = substr($response, 0, -1);
231
-        }
232
-
233
-        return $response;
234
-    }
235
-
236
-    /**
237
-     * Make a curl request.
238
-     *
239
-     * @param string $address The address of the request.
240
-     *
241
-     * @return string Returns the response from the request.
242
-     */
243
-    private function makeCurlRequest($address)
244
-    {
245
-        $curl = curl_init();
246
-
247
-        if (!empty($this->settings['token'])) {
248
-            $address .= '?token='.$this->settings['token'];
249
-        }
250
-
251
-        if ($this->settings['debug']) {
252
-            echo 'Request address: '.$address."\n";
253
-        }
254
-
255
-        curl_setopt_array(
256
-            $curl,
257
-            array_replace(
258
-                $this->settings['curlOptions'],
259
-                array(
260
-                    CURLOPT_RETURNTRANSFER => 1,
261
-                    CURLOPT_URL => $address
262
-                )
263
-            )
264
-        );
265
-
266
-        $response = curl_exec($curl);
267
-
268
-        if (curl_errno($curl)) {
269
-            $errorMessage = curl_error($curl);
270
-
271
-            if ($this->settings['debug']) {
272
-                echo "The error is".$errorMessage;
273
-            }
274
-        }
275
-
276
-        if (isset($errorMessage)) {
277
-            throw new IpInfoException('cURL error', $errorMessage);
278
-        }
279
-
280
-        curl_close($curl);
281
-
282
-        return $response;
283
-    }
284
-
285
-    /**
286
-     * Returns the json decoded associative array.
287
-     * @param  string $response Response from the http call.
288
-     * @return array           Returns the associative array with the response.
289
-     * @throws RateLimitExceedException    If you exceed the rate limit.
290
-     * @throws InvalidTokenException If the used token is invalid.
291
-     * @throws WrongIpException If the used token is invalid.
292
-     */
293
-    private function jsonDecodeResponse($response)
294
-    {
295
-        $output = array();
296
-        if ($response) {
297
-            // Check if the response contains an error message
298
-            $this->checkErrors($response);
299
-            $output = json_decode($response, true);
300
-            if ($output === null && json_last_error() !== JSON_ERROR_NONE) {
301
-                throw new IpInfoException("Wrong response", $response);
302
-            }
303
-        }
304
-        return $output;
305
-    }
306
-
307
-    /**
308
-     * Check if the given response has some kind of errors.
309
-     * @param string $response The response to check.
310
-     * @throws RateLimitExceedException    If you exceed the rate limit.
311
-     * @throws InvalidTokenException If the used token is invalid.
312
-     * @throws WrongIpException If the used token is invalid.
313
-     */
314
-    private function checkErrors($response)
315
-    {
316
-        if (strpos($response, 'Rate limit exceeded.') !== false) {
317
-            throw new RateLimitExceedException("You exceed the rate limit.", $response);
318
-        } elseif (strpos($response, 'Unknown token') !== false) {
319
-            throw new InvalidTokenException("The used token is invalid.", $response);
320
-        } elseif (strpos($response, 'Wrong ip') !== false) {
321
-            throw new WrongIpException("The used IP address is not valid.", $response);
322
-        }
323
-    }
17
+	/**
18
+	 * The base url of the ipinfo service.
19
+	 *
20
+	 * @var string
21
+	 */
22
+	const BASE_URL = 'https://ipinfo.io/';
23
+
24
+	/**
25
+	 * The ip string.
26
+	 *
27
+	 * @var string
28
+	 */
29
+	const IP = 'ip';
30
+
31
+	/**
32
+	 * The hostname string.
33
+	 *
34
+	 * @var string
35
+	 */
36
+	const HOSTNAME = 'hostname';
37
+
38
+	/**
39
+	 * The loc string.
40
+	 *
41
+	 * @var string
42
+	 */
43
+	const LOC = 'loc';
44
+
45
+	/**
46
+	 * The org string.
47
+	 *
48
+	 * @var string
49
+	 */
50
+	const ORG = 'org';
51
+
52
+	/**
53
+	 * The city string.
54
+	 *
55
+	 * @var string
56
+	 */
57
+	const CITY = 'city';
58
+
59
+	/**
60
+	 * The region string.
61
+	 *
62
+	 * @var string
63
+	 */
64
+	const REGION = 'region';
65
+
66
+	/**
67
+	 * The country string.
68
+	 *
69
+	 * @var string
70
+	 */
71
+	const COUNTRY = 'country';
72
+
73
+	/**
74
+	 * The phone string.
75
+	 *
76
+	 * @var string
77
+	 */
78
+	const PHONE = 'phone';
79
+
80
+	/**
81
+	 * The geo string.
82
+	 *
83
+	 * @var string
84
+	 */
85
+	const GEO = 'geo';
86
+
87
+	/**
88
+	 * The postal string.
89
+	 *
90
+	 * @var string
91
+	 */
92
+	const POSTAL = 'postal';
93
+
94
+	/**
95
+	 * All the settings.
96
+	 *
97
+	 * @var array
98
+	 */
99
+	protected $settings;
100
+
101
+	/**
102
+	 * Create an Ipinfo instance.
103
+	 *
104
+	 * @param array $settings An array with all the settings.
105
+	 *                        Supported keys are:
106
+	 *                        - token: string the developer token;
107
+	 *                        - debug: boolean active or not the debug.
108
+	 */
109
+	public function __construct($settings = array())
110
+	{
111
+		//Merge user settings
112
+		$this->settings = array_merge(array(
113
+				'token' => '',
114
+				'debug' => false,
115
+				'curlOptions' => array()
116
+		), $settings);
117
+	}
118
+
119
+	/**
120
+	 * Get all the info about your own ip address.
121
+	 *
122
+	 * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
123
+	 * @throws InvalidTokenException
124
+	 * @throws RateLimitExceedException
125
+	 * @throws WrongIpException
126
+	 */
127
+	public function getYourOwnIpDetails()
128
+	{
129
+		$response = $this->makeCurlRequest($this::BASE_URL.'json');
130
+		$response = $this->jsonDecodeResponse($response);
131
+
132
+		return new Host($response);
133
+	}
134
+
135
+	/**
136
+	 * Get all the info about an ip address.
137
+	 *
138
+	 * @param string $ipAddress The ip address.
139
+	 *
140
+	 * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
141
+	 * @throws InvalidTokenException
142
+	 * @throws RateLimitExceedException
143
+	 * @throws WrongIpException
144
+	 */
145
+	public function getFullIpDetails($ipAddress)
146
+	{
147
+		$response = $this->makeCurlRequest($this::BASE_URL.$ipAddress);
148
+		$response = $this->jsonDecodeResponse($response);
149
+
150
+		return new Host($response);
151
+	}
152
+
153
+	/**
154
+	 * Get a specific field value.
155
+	 *
156
+	 * @param string $ipAddress The ip address.
157
+	 * @param string $field The field.
158
+	 *
159
+	 * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip.
160
+	 *                                           This could returns an Host object if you call it with for the field
161
+	 *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
162
+	 * @throws InvalidTokenException
163
+	 * @throws RateLimitExceedException
164
+	 * @throws WrongIpException
165
+	 */
166
+	public function getSpecificField($ipAddress, $field)
167
+	{
168
+		$response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field);
169
+		$response = $this->checkGeo($field, $response);
170
+
171
+		return $response;
172
+	}
173
+
174
+	/**
175
+	 * Get a specific field value of your own ip address.
176
+	 *
177
+	 * @param string $field The field.
178
+	 *
179
+	 * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip.
180
+	 *                                           This could returns an Host object if you call it with for the field
181
+	 *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
182
+	 * @throws InvalidTokenException
183
+	 * @throws RateLimitExceedException
184
+	 * @throws WrongIpException
185
+	 */
186
+	public function getYourOwnIpSpecificField($field)
187
+	{
188
+		$response = $this->makeCurlRequest($this::BASE_URL.$field);
189
+		$response = $this->checkGeo($field, $response);
190
+
191
+		return $response;
192
+	}
193
+
194
+	/**
195
+	 * Use the /geo call to get just the geolocation information, which will often be
196
+	 * faster than getting the full response.
197
+	 *
198
+	 * @param string $ipAddress The ip address.
199
+	 *
200
+	 * @return \DavidePastore\Ipinfo\Host
201
+	 * @throws InvalidTokenException
202
+	 * @throws RateLimitExceedException
203
+	 * @throws WrongIpException
204
+	 */
205
+	public function getIpGeoDetails($ipAddress)
206
+	{
207
+		return $this->getSpecificField($ipAddress, $this::GEO);
208
+	}
209
+
210
+	/**
211
+	 * Check if the response is GEO and set the parameters accordingly.
212
+	 *
213
+	 * @param string $field The field value.
214
+	 * @param string $response The response from the server.
215
+	 *
216
+	 * @return \DavidePastore\Ipinfo\Host|string Returns an Host object if the request is
217
+	 *                  of the GEO type, a string otherwise. If the field value is different from the GEO type, it will
218
+	 *                  delete the last character ('\n').
219
+	 * @throws InvalidTokenException
220
+	 * @throws RateLimitExceedException
221
+	 * @throws WrongIpException
222
+	 */
223
+	private function checkGeo($field, $response)
224
+	{
225
+		if ($field == $this::GEO) {
226
+			$response = $this->jsonDecodeResponse($response);
227
+			$response = new Host($response);
228
+		} else {
229
+			$this->checkErrors($response);
230
+			$response = substr($response, 0, -1);
231
+		}
232
+
233
+		return $response;
234
+	}
235
+
236
+	/**
237
+	 * Make a curl request.
238
+	 *
239
+	 * @param string $address The address of the request.
240
+	 *
241
+	 * @return string Returns the response from the request.
242
+	 */
243
+	private function makeCurlRequest($address)
244
+	{
245
+		$curl = curl_init();
246
+
247
+		if (!empty($this->settings['token'])) {
248
+			$address .= '?token='.$this->settings['token'];
249
+		}
250
+
251
+		if ($this->settings['debug']) {
252
+			echo 'Request address: '.$address."\n";
253
+		}
254
+
255
+		curl_setopt_array(
256
+			$curl,
257
+			array_replace(
258
+				$this->settings['curlOptions'],
259
+				array(
260
+					CURLOPT_RETURNTRANSFER => 1,
261
+					CURLOPT_URL => $address
262
+				)
263
+			)
264
+		);
265
+
266
+		$response = curl_exec($curl);
267
+
268
+		if (curl_errno($curl)) {
269
+			$errorMessage = curl_error($curl);
270
+
271
+			if ($this->settings['debug']) {
272
+				echo "The error is".$errorMessage;
273
+			}
274
+		}
275
+
276
+		if (isset($errorMessage)) {
277
+			throw new IpInfoException('cURL error', $errorMessage);
278
+		}
279
+
280
+		curl_close($curl);
281
+
282
+		return $response;
283
+	}
284
+
285
+	/**
286
+	 * Returns the json decoded associative array.
287
+	 * @param  string $response Response from the http call.
288
+	 * @return array           Returns the associative array with the response.
289
+	 * @throws RateLimitExceedException    If you exceed the rate limit.
290
+	 * @throws InvalidTokenException If the used token is invalid.
291
+	 * @throws WrongIpException If the used token is invalid.
292
+	 */
293
+	private function jsonDecodeResponse($response)
294
+	{
295
+		$output = array();
296
+		if ($response) {
297
+			// Check if the response contains an error message
298
+			$this->checkErrors($response);
299
+			$output = json_decode($response, true);
300
+			if ($output === null && json_last_error() !== JSON_ERROR_NONE) {
301
+				throw new IpInfoException("Wrong response", $response);
302
+			}
303
+		}
304
+		return $output;
305
+	}
306
+
307
+	/**
308
+	 * Check if the given response has some kind of errors.
309
+	 * @param string $response The response to check.
310
+	 * @throws RateLimitExceedException    If you exceed the rate limit.
311
+	 * @throws InvalidTokenException If the used token is invalid.
312
+	 * @throws WrongIpException If the used token is invalid.
313
+	 */
314
+	private function checkErrors($response)
315
+	{
316
+		if (strpos($response, 'Rate limit exceeded.') !== false) {
317
+			throw new RateLimitExceedException("You exceed the rate limit.", $response);
318
+		} elseif (strpos($response, 'Unknown token') !== false) {
319
+			throw new InvalidTokenException("The used token is invalid.", $response);
320
+		} elseif (strpos($response, 'Wrong ip') !== false) {
321
+			throw new WrongIpException("The used IP address is not valid.", $response);
322
+		}
323
+	}
324 324
 }
Please login to merge, or discard this patch.
src/DavidePastore/Ipinfo/Exception/IpInfoException.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -6,16 +6,16 @@
 block discarded – undo
6 6
  */
7 7
 class IpInfoException extends \Exception
8 8
 {
9
-    private $fullMessage;
9
+	private $fullMessage;
10 10
 
11
-    public function __construct($error, $fullMessage)
12
-    {
13
-        parent::__construct($error);
14
-        $this->fullMessage = $fullMessage;
15
-    }
11
+	public function __construct($error, $fullMessage)
12
+	{
13
+		parent::__construct($error);
14
+		$this->fullMessage = $fullMessage;
15
+	}
16 16
 
17
-    public function getFullMessage()
18
-    {
19
-        return $this->fullMessage;
20
-    }
17
+	public function getFullMessage()
18
+	{
19
+		return $this->fullMessage;
20
+	}
21 21
 }
Please login to merge, or discard this patch.