1- <?php
2-
3- namespace PulkitJalan \GeoIP \Drivers ;
4-
5- use GuzzleHttp \ Exception \ GuzzleException ;
6- use Illuminate \ Support \ Arr ;
7- use GuzzleHttp \Client as GuzzleClient ;
8- use PulkitJalan \GeoIP \Exceptions \InvalidCredentialsException ;
9-
10- class IpStackDriver extends AbstractGeoIPDriver
11- {
12- /**
13- * @param array $config
14- * @param GuzzleClient|null $guzzle
15- * @throws InvalidCredentialsException
16- */
17- public function __construct (array $ config , GuzzleClient $ guzzle = null )
18- {
19- parent ::__construct ($ config , $ guzzle );
20-
21- if (! Arr::get ($ this ->config , 'key ' )) {
22- throw new InvalidCredentialsException ();
23- }
24- }
25-
26- /**
27- * Get array of data using ipstack.
28- *
29- * @param string $ip
30- * @return array
31- * @throws InvalidCredentialsException
32- * @throws GuzzleException
33- */
34- public function get ($ ip )
35- {
36- $ data = $ this ->getRaw ($ ip );
37-
38- if (empty ($ data ) || (Arr::get ($ data , 'latitude ' ) === 0 && Arr::get ($ data , 'longitude ' ) === 0 )) {
39- return $ this ->getDefault ();
40- }
41-
42- return [
43- 'city ' => Arr::get ($ data , 'city ' ),
44- 'country ' => Arr::get ($ data , 'country_name ' ),
45- 'countryCode ' => Arr::get ($ data , 'country_code ' ),
46- 'latitude ' => (float ) number_format (Arr::get ($ data , 'latitude ' , 0 ), 5 ),
47- 'longitude ' => (float ) number_format (Arr::get ($ data , 'longitude ' , 0 ), 5 ),
48- 'region ' => Arr::get ($ data , 'region_name ' ),
49- 'regionCode ' => Arr::get ($ data , 'region_code ' ),
50- 'timezone ' => Arr::get ($ data , 'time_zone.id ' ),
51- 'postalCode ' => Arr::get ($ data , 'zip ' ),
52- ];
53- }
54-
55- /**
56- * Get the raw GeoIP info using ipstack.
57- *
58- * @param string $ip
59- * @return array
60- * @throws InvalidCredentialsException
61- * @throws GuzzleException
62- */
63- public function getRaw ($ ip )
64- {
65- $ data = json_decode ($ this ->guzzle ->get ($ this ->getUrl ($ ip ))->getBody (), true );
66-
67- if (Arr::get ($ data , 'success ' ) === false && Arr::get ($ data , 'error.type ' ) === 'invalid_access_key ' ) {
68- throw new InvalidCredentialsException ();
69- }
70-
71- return $ data ;
72- }
73-
74- /**
75- * Get the ipstack url.
76- *
77- * @param string $ip
78- * @return string
79- */
80- protected function getUrl ($ ip )
81- {
82- $ protocol = 'http ' .(Arr::get ($ this ->config , 'secure ' , true ) ? 's ' : '' );
83-
84- return $ protocol .'://api.ipstack.com/ ' .$ ip .'?access_key= ' .Arr::get ($ this ->config , 'key ' );
85- }
86- }
1+ <?php
2+
3+ namespace PulkitJalan \GeoIP \Drivers ;
4+
5+ use Illuminate \ Support \ Arr ;
6+ use GuzzleHttp \ Client as GuzzleClient ;
7+ use GuzzleHttp \Exception \ GuzzleException ;
8+ use PulkitJalan \GeoIP \Exceptions \InvalidCredentialsException ;
9+
10+ class IpStackDriver extends AbstractGeoIPDriver
11+ {
12+ /**
13+ * @param array $config
14+ * @param GuzzleClient|null $guzzle
15+ * @throws InvalidCredentialsException
16+ */
17+ public function __construct (array $ config , GuzzleClient $ guzzle = null )
18+ {
19+ parent ::__construct ($ config , $ guzzle );
20+
21+ if (! Arr::get ($ this ->config , 'key ' )) {
22+ throw new InvalidCredentialsException ();
23+ }
24+ }
25+
26+ /**
27+ * Get array of data using ipstack.
28+ *
29+ * @param string $ip
30+ * @return array
31+ * @throws InvalidCredentialsException
32+ * @throws GuzzleException
33+ */
34+ public function get ($ ip )
35+ {
36+ $ data = $ this ->getRaw ($ ip );
37+
38+ if (empty ($ data ) || (Arr::get ($ data , 'latitude ' ) === 0 && Arr::get ($ data , 'longitude ' ) === 0 )) {
39+ return $ this ->getDefault ();
40+ }
41+
42+ return [
43+ 'city ' => Arr::get ($ data , 'city ' ),
44+ 'country ' => Arr::get ($ data , 'country_name ' ),
45+ 'countryCode ' => Arr::get ($ data , 'country_code ' ),
46+ 'latitude ' => (float ) number_format (Arr::get ($ data , 'latitude ' , 0 ), 5 ),
47+ 'longitude ' => (float ) number_format (Arr::get ($ data , 'longitude ' , 0 ), 5 ),
48+ 'region ' => Arr::get ($ data , 'region_name ' ),
49+ 'regionCode ' => Arr::get ($ data , 'region_code ' ),
50+ 'timezone ' => Arr::get ($ data , 'time_zone.id ' ),
51+ 'postalCode ' => Arr::get ($ data , 'zip ' ),
52+ ];
53+ }
54+
55+ /**
56+ * Get the raw GeoIP info using ipstack.
57+ *
58+ * @param string $ip
59+ * @return array
60+ * @throws InvalidCredentialsException
61+ * @throws GuzzleException
62+ */
63+ public function getRaw ($ ip )
64+ {
65+ $ data = json_decode ($ this ->guzzle ->get ($ this ->getUrl ($ ip ))->getBody (), true );
66+
67+ if (Arr::get ($ data , 'success ' ) === false && Arr::get ($ data , 'error.type ' ) === 'invalid_access_key ' ) {
68+ throw new InvalidCredentialsException ();
69+ }
70+
71+ return $ data ;
72+ }
73+
74+ /**
75+ * Get the ipstack url.
76+ *
77+ * @param string $ip
78+ * @return string
79+ */
80+ protected function getUrl ($ ip )
81+ {
82+ $ protocol = 'http ' .(Arr::get ($ this ->config , 'secure ' , true ) ? 's ' : '' );
83+
84+ return $ protocol .'://api.ipstack.com/ ' .$ ip .'?access_key= ' .Arr::get ($ this ->config , 'key ' );
85+ }
86+ }
0 commit comments