99class CurlClient implements IClient
1010{
1111
12-
13- public function POST (IService $ service , $ requestUrl , $ params = null , $ token = null ,$ authType = \Seerbit \AuthType::BEARER )
12+ protected bool $ shouldLog = false ;
13+ /**
14+ * @throws SeerbitException
15+ */
16+ public function POST (IService $ service , $ requestUrl , $ params = null , $ token = null , $ authType = \Seerbit \AuthType::BEARER )
1417 {
1518
1619 $ client = $ service ->getClient ();
@@ -19,7 +22,7 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
1922 $ jsonRequest = json_encode ($ params );
2023
2124 // log the request
22- $ this ->logRequest ($ logger , $ requestUrl , $ params );
25+ $ this ->shouldLog && $ this -> logRequest ($ logger , $ requestUrl , $ params );
2326
2427 //Initiate cURL.
2528 $ ch = curl_init ($ requestUrl );
@@ -49,9 +52,9 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
4952 }else {
5053 if ($ authType === \Seerbit \AuthType::BASIC ){
5154 $ key = base64_encode ($ client ->getPublicKey ().": " .$ client ->getSecretKey ());
52- array_push ( $ headers, 'Authorization: Basic ' . $ key) ;
55+ $ headers[] = 'Authorization: Basic ' . $ key ;
5356 }else {
54- array_push ( $ headers, 'Authorization: Bearer ' . $ token) ;
57+ $ headers[] = 'Authorization: Bearer ' . $ token ;
5558 }
5659
5760 curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
@@ -62,15 +65,15 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
6265 //Set the headers
6366 if ($ authType === \Seerbit \AuthType::BASIC ){
6467 $ key = base64_encode ($ client ->getPublicKey ().": " .$ client ->getSecretKey ());
65- array_push ( $ headers, 'Authorization: Basic ' . $ key) ;
68+ $ headers[] = 'Authorization: Basic ' . $ key ;
6669 }else {
67- array_push ( $ headers, 'Authorization: Bearer ' . $ token) ;
70+ $ headers[] = 'Authorization: Bearer ' . $ token ;
6871 }
6972
7073 curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
7174 }
7275
73- $ logger ->info ("Request headers to SeerBit " . print_r ($ headers , 1 ));
76+ $ this -> shouldLog && $ logger ->info ("Request headers to SeerBit " . print_r ($ headers , 1 ));
7477
7578 // return the result
7679 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
@@ -80,7 +83,7 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
8083
8184
8285 // log the raw response
83- $ logger ->info ("JSON Response is: " . $ result );
86+ $ this -> shouldLog && $ logger ->info ("JSON Response is: " . $ result );
8487
8588 // Get errors
8689 list ($ errno , $ message ) = $ this ->curlError ($ ch );
@@ -95,15 +98,15 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
9598 }
9699
97100 // log the array result
98- $ logger ->info ('Params in response from SeerBit: ' . print_r (json_decode ($ result , true ), 1 ));
101+ $ this -> shouldLog && $ logger ->info ('Params in response from SeerBit: ' . print_r (json_decode ($ result , true ), 1 ));
99102
100103 $ result = json_decode ($ result , true );
101104
102105 if (is_array ($ result ) || is_object ($ result )){
103106 $ msg = "" ;
104- $ data = isset ( $ result ["data " ]) ? $ result [ " data " ] : null ;
107+ $ data = $ result ["data " ] ?? null ;
105108 if (is_null ($ data )){
106- $ data = isset ( $ result ["networks " ]) ? $ result [ " networks " ] : null ;
109+ $ data = $ result ["networks " ] ?? null ;
107110 }
108111 if (isset ($ result ["message " ]) ){
109112 $ msg = $ result ["message " ];
@@ -125,15 +128,18 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
125128
126129 }
127130
128- public function GET (IService $ service , $ requestUrl , $ token = null ,$ authType = \Seerbit \AuthType::BEARER )
131+ /**
132+ * @throws SeerbitException
133+ */
134+ public function GET (IService $ service , $ requestUrl , $ token = null , $ authType = \Seerbit \AuthType::BEARER )
129135 {
130136
131137 $ client = $ service ->getClient ();
132138 $ config = $ client ->getConfig ();
133139 $ logger = $ client ->getLogger ();
134140
135141 // log the request
136- $ this ->logRequest ($ logger , $ requestUrl , null );
142+ $ this ->shouldLog && $ this -> logRequest ($ logger , $ requestUrl , null );
137143
138144 //Initiate cURL.
139145 $ ch = curl_init ($ requestUrl );
@@ -157,9 +163,9 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S
157163 }else {
158164 if ($ authType === \Seerbit \AuthType::BASIC ){
159165 $ key = base64_encode ($ client ->getPublicKey ().": " .$ client ->getSecretKey ());
160- array_push ( $ headers, 'Authorization: Basic ' . $ key) ;
166+ $ headers[] = 'Authorization: Basic ' . $ key ;
161167 }else {
162- array_push ( $ headers, 'Authorization: Bearer ' . $ token) ;
168+ $ headers[] = 'Authorization: Bearer ' . $ token ;
163169 }
164170
165171 curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
@@ -170,16 +176,16 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S
170176 //Set the headers
171177 if ($ authType === \Seerbit \AuthType::BASIC ){
172178 $ key = base64_encode ($ client ->getPublicKey ().": " .$ client ->getSecretKey ());
173- array_push ( $ headers, 'Authorization: Basic ' . $ key) ;
179+ $ headers[] = 'Authorization: Basic ' . $ key ;
174180 }else {
175- array_push ( $ headers, 'Authorization: Bearer ' . $ token) ;
181+ $ headers[] = 'Authorization: Bearer ' . $ token ;
176182 }
177183
178184 curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
179185 }
180186
181187
182- $ logger ->info ("Request headers to Seerbit " . print_r ($ headers , 1 ));
188+ $ this -> shouldLog && $ logger ->info ("Request headers to Seerbit " . print_r ($ headers , 1 ));
183189
184190 // return the result
185191 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
@@ -189,7 +195,7 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S
189195
190196
191197 // log the raw response
192- $ logger ->info ("JSON Response is: " . $ result );
198+ $ this -> shouldLog && $ logger ->info ("JSON Response is: " . $ result );
193199
194200 // Get errors
195201 list ($ errno , $ message ) = $ this ->curlError ($ ch );
@@ -203,7 +209,7 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S
203209 }
204210
205211 // log the array result
206- $ logger ->info ('Params in response from Seerbit: ' . print_r ($ result , 1 ));
212+ $ this -> shouldLog && $ logger ->info ('Params in response from Seerbit: ' . print_r ($ result , 1 ));
207213
208214 $ result = json_decode ($ result , true );
209215
@@ -233,7 +239,10 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S
233239
234240 }
235241
236- public function PUT (IService $ service , $ requestUrl , $ params = null , $ token = null ,$ authType = \Seerbit \AuthType::BEARER )
242+ /**
243+ * @throws SeerbitException
244+ */
245+ public function PUT (IService $ service , $ requestUrl , $ params = null , $ token = null , $ authType = \Seerbit \AuthType::BEARER )
237246 {
238247
239248 $ client = $ service ->getClient ();
@@ -242,7 +251,7 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul
242251 $ jsonRequest = json_encode ($ params );
243252
244253 // log the request
245- $ this ->logRequest ($ logger , $ requestUrl , $ params );
254+ $ this ->shouldLog && $ this -> logRequest ($ logger , $ requestUrl , $ params );
246255
247256 //Initiate cURL.
248257 $ ch = curl_init ($ requestUrl );
@@ -273,9 +282,9 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul
273282 }else {
274283 if ($ authType === \Seerbit \AuthType::BASIC ){
275284 $ key = base64_encode ($ client ->getPublicKey ().": " .$ client ->getSecretKey ());
276- array_push ( $ headers, 'Authorization: Basic ' . $ key) ;
285+ $ headers[] = 'Authorization: Basic ' . $ key ;
277286 }else {
278- array_push ( $ headers, 'Authorization: Bearer ' . $ token) ;
287+ $ headers[] = 'Authorization: Bearer ' . $ token ;
279288 }
280289
281290 curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
@@ -286,15 +295,15 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul
286295 //Set the headers
287296 if ($ authType === \Seerbit \AuthType::BASIC ){
288297 $ key = base64_encode ($ client ->getPublicKey ().": " .$ client ->getSecretKey ());
289- array_push ( $ headers, 'Authorization: Basic ' . $ key) ;
298+ $ headers[] = 'Authorization: Basic ' . $ key ;
290299 }else {
291- array_push ( $ headers, 'Authorization: Bearer ' . $ token) ;
300+ $ headers[] = 'Authorization: Bearer ' . $ token ;
292301 }
293302
294303 curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
295304 }
296305
297- $ logger ->info ("Request headers to Seerbit " . print_r ($ headers , 1 ));
306+ $ this -> shouldLog && $ logger ->info ("Request headers to Seerbit " . print_r ($ headers , 1 ));
298307
299308
300309
@@ -306,7 +315,7 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul
306315
307316
308317 // log the raw response
309- $ logger ->info ("JSON Response is: " . $ result );
318+ $ this -> shouldLog && $ logger ->info ("JSON Response is: " . $ result );
310319
311320 // Get errors
312321 list ($ errno , $ message ) = $ this ->curlError ($ ch );
@@ -346,41 +355,38 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul
346355
347356 }
348357
349- protected function handleCurlError ($ url ,$ result , $ errno , $ message , $ logger )
358+ /**
359+ * @throws SeerbitException
360+ */
361+ protected function handleCurlError ($ url , $ result , $ errno , $ message , $ logger )
350362 {
351- switch ($ errno ) {
352- case CURLE_OK :
353- $ msg = "Probably your Web Service username and/or password is incorrect " ;
354- break ;
355- case CURLE_COULDNT_RESOLVE_HOST :
356- case CURLE_OPERATION_TIMEOUTED :
357- $ msg = "Could not connect to Seerbit ( $ url). Please check your "
358- . "internet connection and try again. " ;
359- break ;
360- case CURLE_SSL_CACERT :
361- case CURLE_SSL_PEER_CERTIFICATE :
362- $ msg = "Could not verify Seerbit's SSL certificate. Please make sure "
363- . "that your network is not intercepting certificates. "
364- . "(Try going to $ url in your browser.) "
365- . "If this problem persists, " ;
366- break ;
367- default :
368- $ msg = "Unexpected error communicating with Seerbit Server. " ;
369- }
363+ $ msg = match ($ errno ) {
364+ CURLE_OK => "Probably your Web Service username and/or password is incorrect " ,
365+ CURLE_COULDNT_RESOLVE_HOST , CURLE_OPERATION_TIMEOUTED => "Could not connect to Seerbit ( $ url). Please check your "
366+ . "internet connection and try again. " ,
367+ CURLE_SSL_CACERT , CURLE_SSL_PEER_CERTIFICATE => "Could not verify Seerbit's SSL certificate. Please make sure "
368+ . "that your network is not intercepting certificates. "
369+ . "(Try going to $ url in your browser.) "
370+ . "If this problem persists, " ,
371+ default => "Unexpected error communicating with Seerbit Server. " ,
372+ };
370373 $ msg .= "\n(Network error [errno $ errno]: $ message) " ;
371374 $ msg .= "\n(Network error [result $ errno]: $ result) " ;
372- $ logger ->error ($ msg );
373- throw new \Seerbit \ConnectionException ($ msg , $ errno );
375+ $ this -> shouldLog && $ logger ->error ($ msg );
376+ throw new \Seerbit \SeerbitException ($ msg , $ errno );
374377 }
375378
379+ /**
380+ * @throws SeerbitException
381+ */
376382 protected function handleResultError ($ result , $ logger )
377383 {
378384
379385 $ decodeResult = json_decode ($ result , true );
380386
381387 if ($ result ) {
382388 if (isset ($ decodeResult ['message ' ])) {
383- $ logger ->error ($ decodeResult ['message ' ]);
389+ $ this -> shouldLog && $ logger ->error ($ decodeResult ['message ' ]);
384390 throw new SeerbitException (
385391 $ decodeResult ['message ' ],
386392 "-00 " ,
@@ -389,19 +395,19 @@ protected function handleResultError($result, $logger)
389395 time ()
390396 );
391397 }
392- $ logger ->error ($ result );
398+ $ this -> shouldLog && $ logger ->error ($ result );
393399 throw new SeerbitException ("Error making HTTP request to SeerBit server " , 500 , null , "Server Error " , time ());
394400 }else {
395- $ logger ->error ($ result );
401+ $ this -> shouldLog && $ logger ->error ($ result );
396402 throw new SeerbitException ("Error making HTTP request to SeerBit server " , 500 , null , "Server Error " , time ());
397403 }
398404 }
399405
400406 private function logRequest (\Psr \Log \LoggerInterface $ logger , $ requestUrl , $ params )
401407 {
402408 // log the requestUr, params and json request
403- $ logger ->info ("Request url to SeerBit: " . $ requestUrl );
404- $ logger ->info ('JSON Request payload to SeerBit: ' . json_encode ($ params ));
409+ $ this -> shouldLog && $ logger ->info ("Request url to SeerBit: " . $ requestUrl );
410+ $ this -> shouldLog && $ logger ->info ('JSON Request payload to SeerBit: ' . json_encode ($ params ));
405411 }
406412
407413 protected function curlRequest ($ ch )
0 commit comments