Skip to content

Commit f5ef47a

Browse files
author
Petr Shevtsov
authored
Merge pull request #9 from open-source-contributions/test_enhancement
Test enhancement
2 parents 87e5b3e + 617f914 commit f5ef47a

File tree

10 files changed

+161
-160
lines changed

10 files changed

+161
-160
lines changed

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ dist: trusty
22
language: php
33

44
php:
5-
- 5.6
65
- 7.0
76
- 7.1
87
- 7.2
8+
- 7.3
99
- hhvm
1010

1111
# This triggers builds to run on the new TravisCI infrastructure.
@@ -19,8 +19,10 @@ cache:
1919

2020
matrix:
2121
include:
22-
- php: 5.6
22+
- php: 7.0
2323
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
24+
allow_failures:
25+
- php: hhvm
2426

2527
before_script:
2628
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
@@ -31,7 +33,7 @@ script:
3133

3234
after_script:
3335
- |
34-
if [[ "$TRAVIS_PHP_VERSION" != 'hhvm' && "$TRAVIS_PHP_VERSION" != '7.0' ]]; then
36+
if [[ "$TRAVIS_PHP_VERSION" != 'hhvm' && "$TRAVIS_PHP_VERSION" = '7.0' ]]; then
3537
wget https://scrutinizer-ci.com/ocular.phar
3638
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
3739
fi

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"ext-json": "*"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit" : ">=5.4.3",
25+
"phpunit/phpunit" : ">=6.5",
2626
"squizlabs/php_codesniffer": "^2.3"
2727
},
2828
"autoload": {

src/Details.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
*/
88
class Details
99
{
10-
public function __construct($raw_details)
11-
{
12-
foreach ($raw_details as $property => $value) {
13-
$this->$property = $value;
10+
public function __construct($raw_details)
11+
{
12+
foreach ($raw_details as $property => $value) {
13+
$this->$property = $value;
14+
}
15+
$this->all = $raw_details;
1416
}
15-
$this->all = $raw_details;
16-
}
1717
}

src/IPinfo.php

Lines changed: 58 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace ipinfo\ipinfo;
44

5-
require_once(__DIR__.'/cache/Default.php');
6-
75
use Exception;
6+
use ipinfo\ipinfo\cache\DefaultCache;
7+
use GuzzleHttp\Client;
88
use GuzzleHttp\Exception\GuzzleException;
9-
use ipinfo\ipinfo\Details;
10-
use ipinfo\ipinfo\IPinfoException;
119

1210
/**
1311
* Exposes the IPinfo library to client code.
@@ -29,17 +27,17 @@ class IPinfo
2927
public function __construct($access_token = null, $settings = [])
3028
{
3129
$this->access_token = $access_token;
32-
$this->http_client = new \GuzzleHttp\Client(['http_errors' => false]);
30+
$this->http_client = new Client(['http_errors' => false]);
3331

3432
$countries_file = $settings['countries_file'] ?? self::COUNTRIES_FILE_DEFAULT;
3533
$this->countries = $this->readCountryNames($countries_file);
3634

3735
if (array_key_exists('cache', $settings)) {
38-
$this->cache = $settings['cache'];
36+
$this->cache = $settings['cache'];
3937
} else {
40-
$maxsize = $settings['cache_maxsize'] ?? self::CACHE_MAXSIZE;
41-
$ttl = $settings['cache_ttl'] ?? self::CACHE_TTL;
42-
$this->cache = new cache\DefaultCache($maxsize, $ttl);
38+
$maxsize = $settings['cache_maxsize'] ?? self::CACHE_MAXSIZE;
39+
$ttl = $settings['cache_ttl'] ?? self::CACHE_TTL;
40+
$this->cache = new DefaultCache($maxsize, $ttl);
4341
}
4442
}
4543

@@ -63,19 +61,19 @@ public function getDetails($ip_address = null)
6361
*/
6462
public function formatDetailsObject($details = [])
6563
{
66-
$country = $details['country'] ?? null;
67-
$details['country_name'] = $this->countries[$country] ?? null;
68-
69-
if (array_key_exists('loc', $details)) {
70-
$coords = explode(',', $details['loc']);
71-
$details['latitude'] = $coords[0];
72-
$details['longitude'] = $coords[1];
73-
} else {
74-
$details['latitude'] = null;
75-
$details['longitude'] = null;
76-
}
77-
78-
return new Details($details);
64+
$country = $details['country'] ?? null;
65+
$details['country_name'] = $this->countries[$country] ?? null;
66+
67+
if (array_key_exists('loc', $details)) {
68+
$coords = explode(',', $details['loc']);
69+
$details['latitude'] = $coords[0];
70+
$details['longitude'] = $coords[1];
71+
} else {
72+
$details['latitude'] = null;
73+
$details['longitude'] = null;
74+
}
75+
76+
return new Details($details);
7977
}
8078

8179
/**
@@ -86,38 +84,38 @@ public function formatDetailsObject($details = [])
8684
*/
8785
public function getRequestDetails(string $ip_address)
8886
{
89-
if (!$this->cache->has($ip_address)) {
90-
$url = self::API_URL;
91-
if ($ip_address) {
92-
$url .= "/$ip_address";
87+
if (!$this->cache->has($ip_address)) {
88+
$url = self::API_URL;
89+
if ($ip_address) {
90+
$url .= "/$ip_address";
91+
}
92+
93+
try {
94+
$response = $this->http_client->request(
95+
self::REQUEST_TYPE_GET,
96+
$url,
97+
$this->buildHeaders()
98+
);
99+
} catch (GuzzleException $e) {
100+
throw new IPinfoException($e->getMessage());
101+
} catch (Exception $e) {
102+
throw new IPinfoException($e->getMessage());
103+
}
104+
105+
if ($response->getStatusCode() == self::STATUS_CODE_QUOTA_EXCEEDED) {
106+
throw new IPinfoException('IPinfo request quota exceeded.');
107+
} elseif ($response->getStatusCode() >= 400) {
108+
throw new IPinfoException('Exception: ' . json_encode([
109+
'status' => $response->getStatusCode(),
110+
'reason' => $response->getReasonPhrase(),
111+
]));
112+
}
113+
114+
$raw_details = json_decode($response->getBody(), true);
115+
$this->cache->set($ip_address, $raw_details);
93116
}
94117

95-
try {
96-
$response = $this->http_client->request(
97-
self::REQUEST_TYPE_GET,
98-
$url,
99-
$this->buildHeaders()
100-
);
101-
} catch (GuzzleException $e) {
102-
throw new IPinfoException($e->getMessage());
103-
} catch (Exception $e) {
104-
throw new IPinfoException($e->getMessage());
105-
}
106-
107-
if ($response->getStatusCode() == self::STATUS_CODE_QUOTA_EXCEEDED) {
108-
throw new IPinfoException('IPinfo request quota exceeded.');
109-
} elseif ($response->getStatusCode() >= 400) {
110-
throw new IPinfoException('Exception: ' . json_encode([
111-
'status' => $response->getStatusCode(),
112-
'reason' => $response->getReasonPhrase(),
113-
]));
114-
}
115-
116-
$raw_details = json_decode($response->getBody(), true);
117-
$this->cache->set($ip_address, $raw_details);
118-
}
119-
120-
return $this->cache->get($ip_address);
118+
return $this->cache->get($ip_address);
121119
}
122120

123121
/**
@@ -126,16 +124,16 @@ public function getRequestDetails(string $ip_address)
126124
*/
127125
public function buildHeaders()
128126
{
129-
$headers = [
127+
$headers = [
130128
'user-agent' => 'IPinfoClient/PHP/1.0',
131129
'accept' => 'application/json',
132-
];
130+
];
133131

134-
if ($this->access_token) {
135-
$headers['authorization'] = "Bearer {$this->access_token}";
136-
}
132+
if ($this->access_token) {
133+
$headers['authorization'] = "Bearer {$this->access_token}";
134+
}
137135

138-
return ['headers' => $headers];
136+
return ['headers' => $headers];
139137
}
140138

141139
/**
@@ -145,7 +143,7 @@ public function buildHeaders()
145143
*/
146144
private function readCountryNames($countries_file)
147145
{
148-
$file_contents = file_get_contents($countries_file);
149-
return json_decode($file_contents, true);
146+
$file_contents = file_get_contents($countries_file);
147+
return json_decode($file_contents, true);
150148
}
151149
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@
55
/**
66
* Interface for caches used to store IP data between requests.
77
*/
8-
interface CacheInterface {
8+
interface CacheInterface
9+
{
910

1011
/**
1112
* Tests if the specified IP address is cached.
1213
* @param string $ip_address IP address to lookup.
1314
* @return boolean Is the IP address data in the cache.
1415
*/
15-
public function has(string $ip_address);
16+
public function has(string $ip_address);
1617

1718
/**
1819
* Set the IP address key to the specified value.
1920
* @param string $ip_address IP address to cache data for.
2021
* @param mixed $value Data for specified IP address.
2122
*/
22-
public function set(string $ip_address, $value);
23+
public function set(string $ip_address, $value);
2324

2425
/**
2526
* Get data for the specied IP address.
2627
* @param string $ip_address IP address to lookup in cache.
2728
* @return mixed IP address data.
2829
*/
29-
public function get(string $ip_address);
30+
public function get(string $ip_address);
3031
}

src/cache/Default.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)