22
33namespace ipinfo \ipinfo ;
44
5- require_once (__DIR__ .'/cache/Default.php ' );
6-
75use Exception ;
6+ use ipinfo \ipinfo \cache \DefaultCache ;
7+ use GuzzleHttp \Client ;
88use 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}
0 commit comments