Skip to content

Commit a88c5ce

Browse files
committed
added getRaw method
1 parent bb4bc28 commit a88c5ce

File tree

8 files changed

+279
-95
lines changed

8 files changed

+279
-95
lines changed

.php_cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
use Symfony\CS\Config\Config;
4+
use Symfony\CS\FixerInterface;
5+
use Symfony\CS\Finder\DefaultFinder;
6+
7+
$fixers = [
8+
'blankline_after_open_tag',
9+
'braces',
10+
'concat_without_spaces',
11+
'double_arrow_multiline_whitespaces',
12+
'duplicate_semicolon',
13+
'elseif',
14+
'empty_return',
15+
'encoding',
16+
'eof_ending',
17+
'extra_empty_lines',
18+
'function_call_space',
19+
'function_declaration',
20+
'include',
21+
'indentation',
22+
'join_function',
23+
'line_after_namespace',
24+
'linefeed',
25+
'list_commas',
26+
'logical_not_operators_with_successor_space',
27+
'lowercase_constants',
28+
'lowercase_keywords',
29+
'method_argument_space',
30+
'multiline_array_trailing_comma',
31+
'multiline_spaces_before_semicolon',
32+
'multiple_use',
33+
'namespace_no_leading_whitespace',
34+
'no_blank_lines_after_class_opening',
35+
'no_empty_lines_after_phpdocs',
36+
'object_operator',
37+
'operators_spaces',
38+
'parenthesis',
39+
'phpdoc_indent',
40+
'phpdoc_inline_tag',
41+
'phpdoc_no_access',
42+
'phpdoc_no_package',
43+
'phpdoc_scalar',
44+
'phpdoc_short_description',
45+
'phpdoc_to_comment',
46+
'phpdoc_trim',
47+
'phpdoc_type_to_var',
48+
'phpdoc_var_without_name',
49+
'remove_leading_slash_use',
50+
'remove_lines_between_uses',
51+
'return',
52+
'self_accessor',
53+
'short_array_syntax',
54+
'short_echo_tag',
55+
'short_tag',
56+
'single_array_no_trailing_comma',
57+
'single_blank_line_before_namespace',
58+
'single_line_after_imports',
59+
'single_quote',
60+
'spaces_before_semicolon',
61+
'spaces_cast',
62+
'standardize_not_equal',
63+
'ternary_spaces',
64+
'trailing_spaces',
65+
'trim_array_spaces',
66+
'unalign_equals',
67+
'unary_operators_spaces',
68+
'unused_use',
69+
'visibility',
70+
'whitespacy_lines',
71+
];
72+
73+
return Config::create()
74+
->fixers($fixers)
75+
->level(FixerInterface::NONE_LEVEL)
76+
->setUsingCache(false);

src/Drivers/AbstractGeoIPDriver.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,33 @@ public function __construct(array $config)
3434
* @return array
3535
*/
3636
abstract public function get($ip);
37+
38+
/**
39+
* Get the raw GeoIP info from the driver.
40+
*
41+
* @param string $ip
42+
*
43+
* @return mixed
44+
*/
45+
abstract public function getRaw($ip);
46+
47+
/**
48+
* Get the default values (all null)
49+
*
50+
* @return array
51+
*/
52+
protected function getDefault()
53+
{
54+
return [
55+
'city' => null,
56+
'country' => null,
57+
'countryCode' => null,
58+
'latitude' => null,
59+
'longitude' => null,
60+
'region' => null,
61+
'regionCode' => null,
62+
'timezone' => null,
63+
'postalCode' => null,
64+
];
65+
}
3766
}

src/Drivers/IPApiDriver.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,37 @@ class IPApiDriver extends AbstractGeoIPDriver
1313
*/
1414
public function get($ip)
1515
{
16-
$data = json_decode($this->guzzle->get($this->getUrl($ip))->getBody(), true);
16+
$data = $this->getRaw($ip);
1717

18-
if (array_get($data, 'status') === 'fail') {
19-
return [];
18+
if (empty($data)) {
19+
return $this->getDefault();
2020
}
2121

2222
return [
23-
'city' => array_get($data, 'city'),
24-
'country' => array_get($data, 'country'),
25-
'countryCode' => array_get($data, 'countryCode'),
26-
'latitude' => array_get($data, 'lat'),
27-
'longitude' => array_get($data, 'lon'),
28-
'region' => array_get($data, 'region'),
29-
'regionCode' => array_get($data, 'regionName'),
30-
'timezone' => array_get($data, 'timezone'),
31-
'postalCode' => array_get($data, 'zip'),
32-
'organization' => array_get($data, 'org'),
33-
'isp' => array_get($data, 'isp'),
23+
'city' => array_get($data, 'city'),
24+
'country' => array_get($data, 'country'),
25+
'countryCode' => array_get($data, 'countryCode'),
26+
'latitude' => array_get($data, 'lat'),
27+
'longitude' => array_get($data, 'lon'),
28+
'region' => array_get($data, 'region'),
29+
'regionCode' => array_get($data, 'regionName'),
30+
'timezone' => array_get($data, 'timezone'),
31+
'postalCode' => array_get($data, 'zip'),
3432
];
3533
}
3634

35+
/**
36+
* Get the raw GeoIP info using ip-api.
37+
*
38+
* @param string $ip
39+
*
40+
* @return array
41+
*/
42+
public function getRaw($ip)
43+
{
44+
return json_decode($this->guzzle->get($this->getUrl($ip))->getBody(), true);
45+
}
46+
3747
/**
3848
* Get the ip-api url add key and
3949
* change base url if pro user.

src/Drivers/MaxmindDriver.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,43 @@ public function __construct(array $config)
3434
*/
3535
public function get($ip)
3636
{
37-
try {
38-
$data = $this->maxmind->city($ip);
39-
} catch (AddressNotFoundException $e) {
40-
return [];
37+
$data = $this->getRaw($ip);
38+
39+
if (empty($data)) {
40+
return $this->getDefault();
4141
}
4242

4343
return [
44-
'city' => $data->city->name,
45-
'country' => $data->country->name,
44+
'city' => $data->city->name,
45+
'country' => $data->country->name,
4646
'countryCode' => $data->country->isoCode,
47-
'latitude' => $data->location->latitude,
48-
'longitude' => $data->location->longitude,
49-
'region' => $data->mostSpecificSubdivision->name,
50-
'regionCode' => $data->mostSpecificSubdivision->isoCode,
51-
'timezone' => $data->location->timeZone,
52-
'postalCode' => $data->postal->code,
47+
'latitude' => $data->location->latitude,
48+
'longitude' => $data->location->longitude,
49+
'region' => $data->mostSpecificSubdivision->name,
50+
'regionCode' => $data->mostSpecificSubdivision->isoCode,
51+
'timezone' => $data->location->timeZone,
52+
'postalCode' => $data->postal->code,
5353
];
5454
}
5555

56+
/**
57+
* Get the raw GeoIP info using Maxmind.
58+
*
59+
* @param string $ip
60+
*
61+
* @return mixed
62+
*/
63+
public function getRaw($ip)
64+
{
65+
try {
66+
return $this->maxmind->city($ip);
67+
} catch (AddressNotFoundException $e) {
68+
// ignore
69+
}
70+
71+
return [];
72+
}
73+
5674
/**
5775
* Create the maxmind driver based on config.
5876
*

src/Drivers/TelizeDriver.php

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@
33
namespace PulkitJalan\GeoIP\Drivers;
44

55
use GuzzleHttp\Exception\RequestException;
6+
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;
67

78
class TelizeDriver extends AbstractGeoIPDriver
89
{
10+
/**
11+
* @param array $config
12+
*/
13+
public function __construct(array $config)
14+
{
15+
parent::__construct($config);
16+
17+
if (! array_get($this->config, 'key')) {
18+
throw new InvalidCredentialsException();
19+
}
20+
}
21+
922
/**
1023
* Get array of data using telize.
1124
*
@@ -15,29 +28,46 @@ class TelizeDriver extends AbstractGeoIPDriver
1528
*/
1629
public function get($ip)
1730
{
31+
$data = $this->getRaw($ip);
32+
33+
if (empty($data)) {
34+
return $this->getDefault();
35+
}
36+
37+
return [
38+
'city' => array_get($data, 'city'),
39+
'country' => array_get($data, 'country'),
40+
'countryCode' => array_get($data, 'country_code'),
41+
'latitude' => array_get($data, 'latitude'),
42+
'longitude' => array_get($data, 'longitude'),
43+
'region' => array_get($data, 'region'),
44+
'regionCode' => array_get($data, 'region_code'),
45+
'timezone' => array_get($data, 'timezone'),
46+
'postalCode' => array_get($data, 'postal_code'),
47+
];
48+
}
1849

50+
/**
51+
* Get the raw GeoIP info using telize.
52+
*
53+
* @param string $ip
54+
*
55+
* @return array
56+
*/
57+
public function getRaw($ip)
58+
{
1959
try {
20-
$data = json_decode($this->guzzle->get($this->getUrl($ip), [
60+
return json_decode($this->guzzle->get($this->getUrl($ip), [
2161
'headers' => [
22-
'X-Mashape-Key' => array_get($this->config, 'key'),
23-
'Accept' => 'application/json'
24-
]
62+
'X-Mashape-Key' => array_get($this->config, 'key'),
63+
'Accept' => 'application/json',
64+
],
2565
])->getBody(), true);
2666
} catch (RequestException $e) {
27-
return [];
67+
// ignore
2868
}
2969

30-
return [
31-
'city' => array_get($data, 'city'),
32-
'country' => array_get($data, 'country'),
33-
'countryCode' => array_get($data, 'country_code'),
34-
'latitude' => array_get($data, 'latitude'),
35-
'longitude' => array_get($data, 'longitude'),
36-
'region' => array_get($data, 'region'),
37-
'regionCode' => array_get($data, 'region_code'),
38-
'timezone' => array_get($data, 'timezone'),
39-
'postalCode' => array_get($data, 'postal_code')
40-
];
70+
return [];
4171
}
4272

4373
/**
@@ -49,13 +79,6 @@ public function get($ip)
4979
*/
5080
protected function getUrl($ip)
5181
{
52-
$protocol = 'http:';
53-
if (array_get($this->config, 'secure', false)) {
54-
$protocol = 'https:';
55-
}
56-
57-
$baseUrl = $protocol.'//telize-v1.p.mashape.com/geoip/';
58-
59-
return $baseUrl.$ip;
82+
return 'https://telize-v1.p.mashape.com/geoip/'.$ip;
6083
}
6184
}

src/GeoIP.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class GeoIP
2626
*/
2727
protected $store = [];
2828

29+
/**
30+
* @var array
31+
*/
32+
protected $storeRaw = [];
33+
2934
/**
3035
* @var array
3136
*/
@@ -101,6 +106,35 @@ public function get($property = '')
101106
return array_get($data, $property, '');
102107
}
103108

109+
/**
110+
* Get the raw geoip data from the driver.
111+
*
112+
* @param string
113+
*
114+
* @return mixed
115+
*/
116+
public function getRaw()
117+
{
118+
$ip = $this->getIp();
119+
$this->setIp($ip);
120+
121+
// check ip in memory
122+
$data = array_get($this->storeRaw, $ip);
123+
124+
if (! $data) {
125+
try {
126+
$data = $this->getDriver()->getRaw($ip);
127+
} catch (\Exception $e) {
128+
throw new GeoIPException('Failed to get raw geoip data', 0, $e);
129+
}
130+
131+
// cache ip data in memory
132+
$this->storeRaw[$ip] = $data;
133+
}
134+
135+
return $data;
136+
}
137+
104138
/**
105139
* Get an array or single item of geoip data.
106140
*

0 commit comments

Comments
 (0)