44
55use Facturapi \Exceptions \Facturapi_Exception ;
66
7- class BaseClient {
7+ class BaseClient
8+ {
89 // BaseClient class to be extended by specific clients
910 protected $ FACTURAPI_KEY ;
1011 protected $ ENDPOINT ;
@@ -38,8 +39,9 @@ class BaseClient {
3839 *
3940 * @param $FACTURAPI_KEY : String value of Facturapi API Key for requests
4041 */
41- public function __construct ( $ FACTURAPI_KEY , $ API_VERSION = 'v2 ' ) {
42- $ this ->FACTURAPI_KEY = base64_encode ( $ FACTURAPI_KEY . ": " );
42+ public function __construct ($ FACTURAPI_KEY , $ API_VERSION = 'v2 ' )
43+ {
44+ $ this ->FACTURAPI_KEY = base64_encode ($ FACTURAPI_KEY . ": " );
4345 $ this ->API_VERSION = $ API_VERSION ;
4446 }
4547
@@ -48,7 +50,8 @@ public function __construct( $FACTURAPI_KEY, $API_VERSION = 'v2' ) {
4850 *
4951 * @return integer
5052 */
51- public function getLastStatus () {
53+ public function getLastStatus ()
54+ {
5255 return (int ) $ this ->lastStatus ;
5356 }
5457
@@ -60,9 +63,10 @@ public function getLastStatus() {
6063 *
6164 * @throws Facturapi_Exception
6265 */
63- protected function get_endpoint () {
64- if ( empty ( $ this ->ENDPOINT ) ) {
65- throw new Facturapi_Exception ( 'ENDPOINT must be defined ' );
66+ protected function get_endpoint ()
67+ {
68+ if (empty ($ this ->ENDPOINT )) {
69+ throw new Facturapi_Exception ('ENDPOINT must be defined ' );
6670 } else {
6771 return $ this ->ENDPOINT ;
6872 }
@@ -76,9 +80,10 @@ protected function get_endpoint() {
7680 *
7781 * @throws Facturapi_Exception
7882 */
79- protected function get_api_version () {
80- if ( empty ( $ this ->API_VERSION ) ) {
81- throw new Facturapi_Exception ( 'API_VERSION must be defined ' );
83+ protected function get_api_version ()
84+ {
85+ if (empty ($ this ->API_VERSION )) {
86+ throw new Facturapi_Exception ('API_VERSION must be defined ' );
8287 } else {
8388 return $ this ->API_VERSION ;
8489 }
@@ -91,13 +96,14 @@ protected function get_api_version() {
9196 *
9297 * @returns String
9398 */
94- protected function get_request_url ( $ params = null , $ query = null ) {
99+ protected function get_request_url ($ params = null , $ query = null )
100+ {
95101 $ param_string = $ params == null ? "" : (
96- is_string ( $ params )
97- ? ($ query == null
102+ is_string ($ params )
103+ ? ($ query == null
98104 ? "/ " . $ params
99- : "/ " . $ params . "/ " . $ this ->array_to_params ($ query )
100- ) : $ this ->array_to_params ( $ params )
105+ : "/ " . $ params . "/ " . $ this ->array_to_params ($ query )
106+ ) : $ this ->array_to_params ($ params )
101107 );
102108
103109 return $ this ->BASE_URL . $ this ->get_api_version () . "/ " . $ this ->get_endpoint () . $ param_string ;
@@ -112,24 +118,28 @@ protected function get_request_url( $params = null, $query = null ) {
112118 *
113119 * @throws Facturapi_Exception
114120 */
115- protected function execute_get_request ( $ url ) {
121+ protected function execute_get_request ($ url )
122+ {
116123 $ headers [] = 'Authorization: Basic ' . $ this ->FACTURAPI_KEY ;
117124
118125 $ ch = curl_init ();
119- curl_setopt ( $ ch , CURLOPT_URL , $ url );
120- curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , 1 );
121- curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , false );
122- curl_setopt ( $ ch , CURLOPT_HTTPHEADER , $ headers );
123- curl_setopt ( $ ch , CURLOPT_ENCODING , "gzip " );
124- curl_setopt ( $ ch , CURLOPT_FOLLOWLOCATION , 1 );
126+ curl_setopt ($ ch , CURLOPT_URL , $ url );
127+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
128+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
129+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
130+ curl_setopt ($ ch , CURLOPT_ENCODING , "gzip " );
131+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , 1 );
125132
126- $ output = curl_exec ( $ ch );
127- $ errno = curl_errno ( $ ch );
128- $ error = curl_error ( $ ch );
129- $ this ->setLastStatusFromCurl ( $ ch );
130- curl_close ( $ ch );
131- if ( $ errno > 0 ) {
132- throw new Facturapi_Exception ( 'cURL error: ' . $ error );
133+ $ output = curl_exec ($ ch );
134+ $ errno = curl_errno ($ ch );
135+ $ error = curl_error ($ ch );
136+ $ responseCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
137+ $ this ->setLastStatusFromCurl ($ ch );
138+ curl_close ($ ch );
139+ if ($ errno > 0 ) {
140+ throw new Facturapi_Exception ('cURL error: ' . $ error );
141+ } elseif ($ responseCode < 200 || $ responseCode > 299 ) {
142+ throw new Facturapi_Exception ($ output );
133143 } else {
134144 return $ output ;
135145 }
@@ -145,27 +155,31 @@ protected function execute_get_request( $url ) {
145155 *
146156 * @throws Facturapi_Exception
147157 */
148- protected function execute_post_request ( $ url , $ body , $ formenc = false ) {
158+ protected function execute_post_request ($ url , $ body , $ formenc = false )
159+ {
149160 $ headers [] = 'Authorization: Basic ' . $ this ->FACTURAPI_KEY ;
150- if ( $ formenc ) {
161+ if ($ formenc ) {
151162 $ headers [] = 'Content-Type: application/x-www-form-urlencoded ' ;
152163 }
153164 // initialize cURL and send POST data
154165 $ ch = curl_init ();
155- curl_setopt ( $ ch , CURLOPT_POST , true );
156- curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ body );
157- curl_setopt ( $ ch , CURLOPT_URL , $ url );
158- curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
159- curl_setopt ( $ ch , CURLOPT_HTTPHEADER , $ headers );
166+ curl_setopt ($ ch , CURLOPT_POST , true );
167+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ body );
168+ curl_setopt ($ ch , CURLOPT_URL , $ url );
169+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
170+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
160171
161172
162- $ output = curl_exec ( $ ch );
163- $ errno = curl_errno ( $ ch );
164- $ error = curl_error ( $ ch );
165- $ this ->setLastStatusFromCurl ( $ ch );
166- curl_close ( $ ch );
167- if ( $ errno > 0 ) {
168- throw new Facturapi_Exception ( 'cURL error: ' . $ error );
173+ $ output = curl_exec ($ ch );
174+ $ errno = curl_errno ($ ch );
175+ $ error = curl_error ($ ch );
176+ $ responseCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
177+ $ this ->setLastStatusFromCurl ($ ch );
178+ curl_close ($ ch );
179+ if ($ errno > 0 ) {
180+ throw new Facturapi_Exception ('cURL error: ' . $ error );
181+ } elseif ($ responseCode < 200 || $ responseCode > 299 ) {
182+ throw new Facturapi_Exception ($ output );
169183 } else {
170184 return $ output ;
171185 }
@@ -181,32 +195,31 @@ protected function execute_post_request( $url, $body, $formenc = false ) {
181195 *
182196 * @throws Facturapi_Exception
183197 */
184- protected function execute_JSON_post_request ( $ url , $ body ) {
198+ protected function execute_JSON_post_request ($ url , $ body )
199+ {
185200 $ headers [] = 'Authorization: Basic ' . $ this ->FACTURAPI_KEY ;
186201 $ headers [] = 'Content-Type: application/json ' ;
187202
188203 // initialize cURL and send POST data
189204 $ ch = curl_init ();
190- curl_setopt ( $ ch , CURLOPT_POST , true );
191- curl_setopt ( $ ch , CURLOPT_POSTFIELDS , json_encode ( $ body ) );
192- curl_setopt ( $ ch , CURLOPT_URL , $ url );
193- curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
194- curl_setopt ( $ ch , CURLOPT_HTTPHEADER , $ headers );
205+ curl_setopt ($ ch , CURLOPT_POST , true );
206+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ body) );
207+ curl_setopt ($ ch , CURLOPT_URL , $ url );
208+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
209+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
195210
196- $ output = curl_exec ( $ ch );
197- $ errno = curl_errno ( $ ch );
198- $ error = curl_error ( $ ch );
211+ $ output = curl_exec ($ ch );
212+ $ errno = curl_errno ($ ch );
213+ $ error = curl_error ($ ch );
199214 //Get response code, only numbers in range from 200 to 299 are valid
200215 $ responseCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
201- $ this ->setLastStatusFromCurl ( $ ch );
202- curl_close ( $ ch );
203- if ( $ errno > 0 ) {
204- throw new Facturapi_Exception ( 'cURL error: ' . $ error );
216+ $ this ->setLastStatusFromCurl ($ ch );
217+ curl_close ($ ch );
218+ if ($ errno > 0 ) {
219+ throw new Facturapi_Exception ('cURL error: ' . $ error );
205220 } elseif ($ responseCode < 200 || $ responseCode > 299 ) {
206- //Decode response body to get error message from API
207- $ outputDecoded = json_decode ($ output , true );
208- throw new Facturapi_Exception ($ outputDecoded ['message ' ]);
209- }else {
221+ throw new Facturapi_Exception ($ output );
222+ } else {
210223 return $ output ;
211224 }
212225 }
@@ -221,25 +234,29 @@ protected function execute_JSON_post_request( $url, $body ) {
221234 *
222235 * @throws Facturapi_Exception
223236 */
224- protected function execute_JSON_put_request ( $ url , $ body ) {
237+ protected function execute_JSON_put_request ($ url , $ body )
238+ {
225239 $ headers [] = 'Authorization: Basic ' . $ this ->FACTURAPI_KEY ;
226240 $ headers [] = 'Content-Type: application/json ' ;
227241
228242 $ ch = curl_init ();
229- curl_setopt ( $ ch , CURLOPT_URL , $ url );
230- curl_setopt ( $ ch , CURLOPT_HTTPHEADER , $ headers );
231- curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
232- curl_setopt ( $ ch , CURLOPT_CUSTOMREQUEST , "PUT " );
233- curl_setopt ( $ ch , CURLOPT_POSTFIELDS , json_encode ( $ body ) );
234- curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , 0 );
243+ curl_setopt ($ ch , CURLOPT_URL , $ url );
244+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
245+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
246+ curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , "PUT " );
247+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ body) );
248+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
235249
236- $ result = curl_exec ( $ ch );
237- $ errno = curl_errno ( $ ch );
238- $ error = curl_error ( $ ch );
239- $ this ->setLastStatusFromCurl ( $ ch );
240- curl_close ( $ ch );
241- if ( $ errno > 0 ) {
242- throw new Facturapi_Exception ( 'cURL error: ' . $ error );
250+ $ result = curl_exec ($ ch );
251+ $ errno = curl_errno ($ ch );
252+ $ error = curl_error ($ ch );
253+ $ responseCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
254+ $ this ->setLastStatusFromCurl ($ ch );
255+ curl_close ($ ch );
256+ if ($ errno > 0 ) {
257+ throw new Facturapi_Exception ('cURL error: ' . $ error );
258+ } elseif ($ responseCode < 200 || $ responseCode > 299 ) {
259+ throw new Facturapi_Exception ($ result );
243260 } else {
244261 return $ result ;
245262 }
@@ -255,11 +272,12 @@ protected function execute_JSON_put_request( $url, $body ) {
255272 *
256273 * @throws Facturapi_Exception
257274 */
258- protected function execute_data_put_request ( $ url , $ body ) {
275+ protected function execute_data_put_request ($ url , $ body )
276+ {
259277 $ headers [] = 'Authorization: Basic ' . $ this ->FACTURAPI_KEY ;
260278 $ headers [] = 'Content-Type: multipart/form-data ' ;
261279
262- $ data = is_array ( $ body ) ? array (
280+ $ data = is_array ($ body ) ? array (
263281 'cer ' => new \CURLFile ($ body ['cerFile ' ]),
264282 'key ' => new \CURLFile ($ body ['keyFile ' ]),
265283 'password ' => $ body ['password ' ]
@@ -268,25 +286,25 @@ protected function execute_data_put_request( $url, $body ) {
268286 );
269287
270288 $ ch = curl_init ();
271- curl_setopt ( $ ch , CURLOPT_URL , $ url );
272- curl_setopt ( $ ch , CURLOPT_HTTPHEADER , $ headers );
273- curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
274- curl_setopt ( $ ch , CURLOPT_CUSTOMREQUEST , "PUT " );
275- curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ data );
276- curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , 0 );
289+ curl_setopt ($ ch , CURLOPT_URL , $ url );
290+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
291+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
292+ curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , "PUT " );
293+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data );
294+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
277295
278- $ result = curl_exec ( $ ch );
279- $ errno = curl_errno ( $ ch );
280- $ error = curl_error ( $ ch );
281- $ this ->setLastStatusFromCurl ( $ ch );
282- curl_close ( $ ch );
283- if ( $ errno > 0 ) {
284- throw new Facturapi_Exception ( 'cURL error: ' . $ error );
296+ $ result = curl_exec ($ ch );
297+ $ errno = curl_errno ($ ch );
298+ $ error = curl_error ($ ch );
299+ $ this ->setLastStatusFromCurl ($ ch );
300+ curl_close ($ ch );
301+ if ($ errno > 0 ) {
302+ throw new Facturapi_Exception ('cURL error: ' . $ error );
285303 } else {
286304 return $ result ;
287305 }
288306 }
289-
307+
290308 /**
291309 * Executes HTTP DELETE request
292310 *
@@ -297,26 +315,27 @@ protected function execute_data_put_request( $url, $body ) {
297315 *
298316 * @throws Facturapi_Exception
299317 */
300- protected function execute_delete_request ( $ url , $ body ) {
318+ protected function execute_delete_request ($ url , $ body )
319+ {
301320 $ headers [] = 'Authorization: Basic ' . $ this ->FACTURAPI_KEY ;
302321 $ headers [] = 'Content-Type: application/json ' ;
303- $ headers [] = 'Content-Length: ' . strlen ( $ body );
322+ $ headers [] = 'Content-Length: ' . strlen ($ body );
304323
305324 $ ch = curl_init ();
306- curl_setopt ( $ ch , CURLOPT_URL , $ url );
307- curl_setopt ( $ ch , CURLOPT_HTTPHEADER , $ headers );
308- curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
309- curl_setopt ( $ ch , CURLOPT_CUSTOMREQUEST , "DELETE " );
310- curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ body );
311- curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , 0 );
325+ curl_setopt ($ ch , CURLOPT_URL , $ url );
326+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
327+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
328+ curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , "DELETE " );
329+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ body );
330+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
312331
313- $ result = curl_exec ( $ ch );
314- $ errno = curl_errno ( $ ch );
315- $ error = curl_error ( $ ch );
316- $ this ->setLastStatusFromCurl ( $ ch );
317- curl_close ( $ ch );
318- if ( $ errno > 0 ) {
319- throw new Facturapi_Exception ( 'cURL error: ' . $ error );
332+ $ result = curl_exec ($ ch );
333+ $ errno = curl_errno ($ ch );
334+ $ error = curl_error ($ ch );
335+ $ this ->setLastStatusFromCurl ($ ch );
336+ curl_close ($ ch );
337+ if ($ errno > 0 ) {
338+ throw new Facturapi_Exception ('cURL error: ' . $ error );
320339 } else {
321340 return $ result ;
322341 }
@@ -329,16 +348,17 @@ protected function execute_delete_request( $url, $body ) {
329348 *
330349 * @return String of url friendly parameters (&name=value&foo=bar)
331350 */
332- protected function array_to_params ( $ params ) {
351+ protected function array_to_params ($ params )
352+ {
333353 $ param_string = '? ' ;
334- if ( $ params != null ) {
335- foreach ( $ params as $ parameter => $ value ) {
336- if ( is_array ( $ value ) ) {
337- foreach ( $ value as $ key => $ sub_param ) {
338- $ param_string = $ param_string . '& ' . $ parameter. '[ ' . $ key. '] ' . '= ' . urlencode ( $ sub_param );
354+ if ($ params != null ) {
355+ foreach ($ params as $ parameter => $ value ) {
356+ if (is_array ($ value) ) {
357+ foreach ($ value as $ key => $ sub_param ) {
358+ $ param_string = $ param_string . '& ' . $ parameter . '[ ' . $ key . '] ' . '= ' . urlencode ($ sub_param );
339359 }
340360 } else {
341- $ param = gettype ($ value ) == 'boolean ' ? json_encode ($ value ) : urlencode ( $ value );
361+ $ param = gettype ($ value ) == 'boolean ' ? json_encode ($ value ) : urlencode ($ value );
342362 $ param_string = $ param_string . '& ' . $ parameter . '= ' . $ param ;
343363 }
344364 }
@@ -350,8 +370,9 @@ protected function array_to_params( $params ) {
350370 /**
351371 * Sets the status code from a curl request
352372 */
353- protected function setLastStatusFromCurl ( $ ch ) {
354- $ info = curl_getinfo ( $ ch );
355- $ this ->lastStatus = ( isset ( $ info ['http_code ' ] ) ) ? $ info ['http_code ' ] : null ;
373+ protected function setLastStatusFromCurl ($ ch )
374+ {
375+ $ info = curl_getinfo ($ ch );
376+ $ this ->lastStatus = (isset ($ info ['http_code ' ])) ? $ info ['http_code ' ] : null ;
356377 }
357378}
0 commit comments