33namespace PulkitJalan \GeoIP \Drivers ;
44
55use GuzzleHttp \Exception \RequestException ;
6+ use PulkitJalan \GeoIP \Exceptions \InvalidCredentialsException ;
67
78class 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}
0 commit comments