Skip to content

Commit b362fcd

Browse files
committed
removed requester
1 parent 7be5372 commit b362fcd

File tree

11 files changed

+61
-56
lines changed

11 files changed

+61
-56
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=5.4.0",
13+
"php": ">=5.5",
1414
"illuminate/support": "~5",
1515
"illuminate/console": "~5",
16-
"pulkitjalan/requester": "1.*",
16+
"guzzlehttp/guzzle": "~6",
1717
"geoip2/geoip2": "2.*"
1818
},
1919
"require-dev": {

src/Console/UpdateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function fire()
4747
{
4848
$result = $this->geoIPUpdater->update();
4949

50-
if (!$result) {
50+
if (! $result) {
5151
$this->error('Update failed!');
5252

5353
return;

src/Drivers/AbstractGeoIPDriver.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PulkitJalan\GeoIP\Drivers;
44

55
use GuzzleHttp\Client as GuzzleClient;
6-
use PulkitJalan\Requester\Requester;
76

87
abstract class AbstractGeoIPDriver
98
{
@@ -13,9 +12,9 @@ abstract class AbstractGeoIPDriver
1312
protected $config;
1413

1514
/**
16-
* @var \PulkitJalan\Requester\Requester
15+
* @var \GuzzleHttp\Client
1716
*/
18-
protected $requester;
17+
protected $guzzle;
1918

2019
/**
2120
* @param array $config
@@ -24,7 +23,7 @@ public function __construct(array $config)
2423
{
2524
$this->config = $config;
2625

27-
$this->requester = with(new Requester(new GuzzleClient()))->retry(2)->every(50);
26+
$this->guzzle = new GuzzleClient();
2827
}
2928

3029
/**

src/Drivers/IPApiDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class IPApiDriver extends AbstractGeoIPDriver
1313
*/
1414
public function get($ip)
1515
{
16-
$data = $this->requester->url($this->getUrl($ip))->get()->json();
16+
$data = json_decode($this->guzzle->get($this->getUrl($ip))->getBody(), true);
1717

1818
if (array_get($data, 'status') === 'fail') {
1919
return [];

src/Drivers/MaxmindDriver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ protected function create()
8484
*/
8585
protected function createWebClient()
8686
{
87-
$userId = array_get($this->config, 'user_id', false);
88-
$licenseKey = array_get($this->config, 'license_key', false);
87+
$userId = array_get($this->config, 'user_id', false);
88+
$licenseKey = array_get($this->config, 'license_key', false);
8989

9090
// check and make sure they are set
91-
if (!$userId || !$licenseKey) {
91+
if (! $userId || ! $licenseKey) {
9292
throw new InvalidCredentialsException();
9393
}
9494

@@ -107,7 +107,7 @@ protected function createDatabase()
107107
$database = array_get($this->config, 'database', false);
108108

109109
// check if file exists first
110-
if (!$database || !file_exists($database)) {
110+
if (! $database || ! file_exists($database)) {
111111
throw new InvalidCredentialsException();
112112
}
113113

src/Drivers/TelizeDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TelizeDriver extends AbstractGeoIPDriver
1616
public function get($ip)
1717
{
1818
try {
19-
$data = $this->requester->url($this->getUrl($ip))->get()->json();
19+
$data = json_decode($this->guzzle->get($this->getUrl($ip))->getBody(), true);
2020
} catch (RequestException $e) {
2121
return [];
2222
}

src/GeoIP.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class GeoIP
1717
protected $driver;
1818

1919
/**
20-
* @var boolean
20+
* @var bool
2121
*/
2222
protected $random;
2323

@@ -66,7 +66,7 @@ public function setIp($ip)
6666
*/
6767
public function getIp()
6868
{
69-
if (!$this->ip) {
69+
if (! $this->ip) {
7070
if ($this->random) {
7171
$this->ip = long2ip(rand(0, '4294967295'));
7272
} else {
@@ -94,7 +94,7 @@ public function get($property = '')
9494
{
9595
$data = $this->getData();
9696

97-
if (!$property) {
97+
if (! $property) {
9898
return $data;
9999
}
100100

@@ -116,7 +116,7 @@ protected function getData()
116116
// check ip in memory
117117
$data = array_get($this->store, $ip);
118118

119-
if (!$data) {
119+
if (! $data) {
120120
try {
121121
$data = $this->getDriver()->get($ip);
122122
} catch (\Exception $e) {

src/GeoIPManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getDriver($driver = null)
3333

3434
$method = 'create'.ucfirst(camel_case($driver)).'Driver';
3535

36-
if (!method_exists($this, $method)) {
36+
if (! method_exists($this, $method)) {
3737
throw new InvalidDriverException(sprintf('Driver [%s] not supported.', $driver));
3838
}
3939

src/GeoIPUpdater.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PulkitJalan\GeoIP;
44

55
use GuzzleHttp\Client as GuzzleClient;
6-
use PulkitJalan\Requester\Requester;
76
use Exception;
87

98
class GeoIPUpdater
@@ -14,18 +13,18 @@ class GeoIPUpdater
1413
protected $config;
1514

1615
/**
17-
* @var \PulkitJalan\Requester\Requester
16+
* @var \GuzzleHttp\Client
1817
*/
19-
protected $requester;
18+
protected $guzzle;
2019

2120
/**
2221
* @param array $config
2322
*/
24-
public function __construct(array $config, Requester $requester = null)
23+
public function __construct(array $config, GuzzleClient $guzzle = null)
2524
{
2625
$this->config = $config;
2726

28-
$this->requester = $requester ?: with(new Requester(new GuzzleClient()))->retry(2)->every(50);
27+
$this->guzzle = $guzzle ?: new GuzzleClient();
2928
}
3029

3130
/**
@@ -53,11 +52,11 @@ protected function updateMaxmindDatabase()
5352

5453
$database = array_get($this->config, 'maxmind.database', false);
5554

56-
if (!file_exists($dir = pathinfo($database, PATHINFO_DIRNAME))) {
55+
if (! file_exists($dir = pathinfo($database, PATHINFO_DIRNAME))) {
5756
mkdir($dir, 0777, true);
5857
}
5958

60-
$file = $this->requester->url($maxmindDatabaseUrl)->get()->getBody();
59+
$file = $this->guzzle->get($maxmindDatabaseUrl)->getBody();
6160

6261
try {
6362
file_put_contents($database, $this->gzdecode($file));

tests/GeoIPTest.php

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
<?php
22

3-
namespace PulkitJalan\GeoIP\tests;
3+
namespace PulkitJalan\GeoIP\Tests;
44

55
use Mockery;
66
use PHPUnit_Framework_TestCase;
7+
use PulkitJalan\GeoIP\Exceptions\GeoIPException;
8+
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;
9+
use PulkitJalan\GeoIP\Exceptions\InvalidDatabaseException;
10+
use PulkitJalan\GeoIP\Exceptions\InvalidDriverException;
11+
use PulkitJalan\GeoIP\GeoIP;
12+
use BadMethodCallException;
713

814
class GeoIPTest extends PHPUnit_Framework_TestCase
915
{
@@ -18,25 +24,25 @@ public function tearDown()
1824

1925
public function test_invalid_driver_exception()
2026
{
21-
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\InvalidDriverException');
27+
$this->setExpectedException(InvalidDriverException::class);
2228

23-
$geoip = new \PulkitJalan\GeoIP\GeoIP([]);
29+
$geoip = new GeoIP([]);
2430
}
2531

2632
public function test_bad_method_call_exception()
2733
{
28-
$this->setExpectedException('BadMethodCallException');
34+
$this->setExpectedException(BadMethodCallException::class);
2935

30-
$geoip = new \PulkitJalan\GeoIP\GeoIP();
36+
$geoip = new GeoIP();
3137

3238
$geoip->setNothing();
3339
}
3440

3541
public function test_maxmind_exception()
3642
{
37-
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException');
43+
$this->setExpectedException(InvalidCredentialsException::class);
3844

39-
$geoip = new \PulkitJalan\GeoIP\GeoIP(['driver' => 'maxmind']);
45+
$geoip = new GeoIP(['driver' => 'maxmind']);
4046
}
4147

4248
public function test_maxmind_database_exception()
@@ -48,9 +54,9 @@ public function test_maxmind_database_exception()
4854
],
4955
];
5056

51-
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException');
57+
$this->setExpectedException(InvalidCredentialsException::class);
5258

53-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
59+
$geoip = new GeoIP($config);
5460
}
5561

5662
public function test_maxmind_invalid_database_exception()
@@ -62,9 +68,9 @@ public function test_maxmind_invalid_database_exception()
6268
],
6369
];
6470

65-
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\InvalidDatabaseException');
71+
$this->setExpectedException(InvalidDatabaseException::class);
6672

67-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
73+
$geoip = new GeoIP($config);
6874
}
6975

7076
public function test_maxmind_web_api_exception()
@@ -76,9 +82,9 @@ public function test_maxmind_web_api_exception()
7682
],
7783
];
7884

79-
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException');
85+
$this->setExpectedException(InvalidCredentialsException::class);
8086

81-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
87+
$geoip = new GeoIP($config);
8288
}
8389

8490
public function test_maxmind_web_api_authentication_exception()
@@ -91,9 +97,9 @@ public function test_maxmind_web_api_authentication_exception()
9197
],
9298
];
9399

94-
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\GeoIPException');
100+
$this->setExpectedException(GeoIPException::class);
95101

96-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
102+
$geoip = new GeoIP($config);
97103
$geoip = $geoip->setIp($this->validIp);
98104

99105
$geoip->get();
@@ -106,11 +112,11 @@ public function test_get_random_ipaddress()
106112
'random' => true,
107113
];
108114

109-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
115+
$geoip = new GeoIP($config);
110116
$ip = $geoip->getIp();
111117

112118
$this->assertNotEquals($ip, $this->invalidIp);
113-
$this->assertTrue(!(filter_var($ip, FILTER_VALIDATE_IP)) === False);
119+
$this->assertTrue(! (filter_var($ip, FILTER_VALIDATE_IP)) === false);
114120
}
115121

116122
public function test_get_non_random_ipaddress()
@@ -120,11 +126,11 @@ public function test_get_non_random_ipaddress()
120126
'random' => false,
121127
];
122128

123-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
129+
$geoip = new GeoIP($config);
124130
$ip = $geoip->getIp();
125131

126132
$this->assertEquals($ip, $this->invalidIp);
127-
$this->assertTrue(!(filter_var($ip, FILTER_VALIDATE_IP)) === False);
133+
$this->assertTrue(! (filter_var($ip, FILTER_VALIDATE_IP)) === false);
128134
}
129135

130136
public function test_get_multiple_ipaddress()
@@ -133,12 +139,12 @@ public function test_get_multiple_ipaddress()
133139
'driver' => 'ip-api',
134140
];
135141

136-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
142+
$geoip = new GeoIP($config);
137143
$geoip->setIp($this->multipleIps);
138144
$ip = $geoip->getIp();
139145

140146
$this->assertEquals($ip, $this->validIp);
141-
$this->assertTrue(!(filter_var($ip, FILTER_VALIDATE_IP)) === False);
147+
$this->assertTrue(! (filter_var($ip, FILTER_VALIDATE_IP)) === false);
142148
}
143149

144150
public function test_maxmind_database()
@@ -150,7 +156,7 @@ public function test_maxmind_database()
150156
],
151157
];
152158

153-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
159+
$geoip = new GeoIP($config);
154160
$geoip = $geoip->setIp($this->validIp);
155161

156162
$this->assertEquals($geoip->getCountry(), 'United Kingdom');
@@ -171,9 +177,9 @@ public function test_ip_api_pro_exception()
171177
],
172178
];
173179

174-
$this->setExpectedException('PulkitJalan\GeoIP\Exceptions\GeoIPException');
180+
$this->setExpectedException(GeoIPException::class);
175181

176-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
182+
$geoip = new GeoIP($config);
177183
$geoip = $geoip->setIp($this->validIp);
178184

179185
$geoip->get();
@@ -185,7 +191,7 @@ public function test_ip_api()
185191
'driver' => 'ip-api',
186192
];
187193

188-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
194+
$geoip = new GeoIP($config);
189195
$geoip = $geoip->setIp($this->validIp);
190196

191197
$this->assertEquals($geoip->getCountry(), 'United Kingdom');
@@ -202,7 +208,7 @@ public function test_telize()
202208
'driver' => 'telize',
203209
];
204210

205-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
211+
$geoip = new GeoIP($config);
206212
$geoip = $geoip->setIp($this->validIp);
207213

208214
$this->assertEquals($geoip->getCountry(), 'United Kingdom');
@@ -222,7 +228,7 @@ public function test_telize_secure()
222228
],
223229
];
224230

225-
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
231+
$geoip = new GeoIP($config);
226232
$geoip = $geoip->setIp($this->validIp);
227233

228234
$this->assertEquals($geoip->getCountry(), 'United Kingdom');

0 commit comments

Comments
 (0)