11<?php
22namespace CodeClimate \Bundle \TestReporterBundle ;
33
4- use Guzzle \Http \Client ;
5- use Guzzle \Http \Exception \ClientErrorResponseException ;
6-
74class ApiClient
85{
9- protected $ client ;
106 protected $ apiHost ;
117
128 public function __construct ()
139 {
14- $ this ->client = new Client ();
1510 $ this ->apiHost = "https://codeclimate.com " ;
1611
1712 if (isset ($ _SERVER ["CODECLIMATE_API_HOST " ])) {
@@ -22,18 +17,30 @@ public function __construct()
2217
2318 public function send ($ json )
2419 {
25- $ request = $ this ->client ->createRequest ('POST ' , $ this ->apiHost ."/test_reports " );
26- $ response = false ;
27-
28- $ request ->setHeader ("User-Agent " , "Code Climate (PHP Test Reporter v " .Version::VERSION .") " );
29- $ request ->setHeader ("Content-Type " , "application/json " );
30- $ request ->setBody ($ json );
31-
32- try {
33- $ response = $ this ->client ->send ($ request );
34- } catch (ClientErrorResponseException $ e ) {
35- $ response = $ e ->getResponse ();
20+ $ ch = curl_init ();
21+ curl_setopt_array ($ ch , array (
22+ CURLOPT_CUSTOMREQUEST => 'POST ' ,
23+ CURLOPT_URL => $ this ->apiHost .'/test_reports ' ,
24+ CURLOPT_USERAGENT => 'Code Climate (PHP Test Reporter v ' .Version::VERSION .') ' ,
25+ CURLOPT_HTTPHEADER => array ('Content-Type: application/json ' ),
26+ CURLOPT_HEADER => true ,
27+ CURLOPT_RETURNTRANSFER => true ,
28+ CURLOPT_POSTFIELDS => (string )$ json ,
29+ ));
30+
31+ $ response = new \stdClass ;
32+ if ($ raw_response = curl_exec ($ ch )) {
33+ list ($ response ->headers , $ response ->body ) = explode ("\r\n\r\n" , $ raw_response , 2 );
34+ $ response ->headers = explode ("\r\n" , $ response ->headers );
35+ list (, $ response ->code , $ response ->message ) = explode (' ' , $ response ->headers [0 ], 3 );
36+ }
37+ else {
38+ $ response ->code = -curl_errno ($ ch );
39+ $ response ->message = curl_error ($ ch );
40+ $ response ->headers = array ();
41+ $ response ->body = NULL ;
3642 }
43+ curl_close ($ ch );
3744
3845 return $ response ;
3946 }
0 commit comments