@@ -5,6 +5,9 @@ class ApiClient
5
5
{
6
6
protected $ apiHost ;
7
7
8
+ /**
9
+ * Init the API client and set the hostname
10
+ */
8
11
public function __construct ()
9
12
{
10
13
$ this ->apiHost = "https://codeclimate.com " ;
@@ -15,41 +18,67 @@ public function __construct()
15
18
16
19
}
17
20
21
+ /**
22
+ * Send the given JSON as a request to the CodeClimate Server
23
+ *
24
+ * @param object $json JSON data
25
+ * @return \stdClass Response object with (code, message, headers & body properties)
26
+ */
18
27
public function send ($ json )
19
28
{
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
29
$ response = new \stdClass ;
32
- if ($ raw_response = curl_exec ($ ch )) {
33
- $ this ->buildResponse ($ response , $ raw_response );
30
+ $ payload = (string )$ json ;
31
+ $ options = array (
32
+ 'http ' => array (
33
+ 'method ' => 'POST ' ,
34
+ 'header ' => array (
35
+ 'Host: codeclimate.com ' ,
36
+ 'Content-Type: application/json ' ,
37
+ 'User-Agent: Code Climate (PHP Test Reporter v ' .Version::VERSION .') ' ,
38
+ 'Content-Length: ' .strlen ($ payload )
39
+ ),
40
+ 'content ' => $ payload ,
41
+ "timeout " => 10
42
+ )
43
+ );
44
+ $ context = stream_context_create ($ options );
45
+ $ url = $ this ->apiHost .'/test_reports ' ;
46
+
47
+ if ($ stream = @fopen ($ url , 'r ' , false , $ context )) {
48
+ $ meta = stream_get_meta_data ($ stream );
49
+ $ raw_response = implode ("\r\n" , $ meta ['wrapper_data ' ])."\r\n\r\n" .stream_get_contents ($ stream );
50
+ fclose ($ stream );
34
51
35
- while ( $ response -> code == 100 ) { // Continue
36
- $ this ->buildResponse ($ response , $ response -> body );
52
+ if (! empty ( $ raw_response )) {
53
+ $ response = $ this ->buildResponse ($ response , $ raw_response );
37
54
}
38
55
} else {
39
- $ response ->code = -curl_errno ($ ch );
40
- $ response ->message = curl_error ($ ch );
56
+ $ error = error_get_last ();
57
+ preg_match ('/([0-9]{3})/ ' , $ error ['message ' ], $ match );
58
+ $ errorCode = (isset ($ match [1 ])) ? $ match [1 ] : 500 ;
59
+
60
+ $ response ->code = $ errorCode ;
61
+ $ response ->message = $ error ['message ' ];
41
62
$ response ->headers = array ();
42
63
$ response ->body = NULL ;
43
64
}
44
- curl_close ($ ch );
45
65
46
66
return $ response ;
47
67
}
48
68
69
+ /**
70
+ * Build the response object from the HTTP results
71
+ *
72
+ * @param \stdClass $response Standard object
73
+ * @param string $body HTTP response contents
74
+ * @return \stcClass Populated class object
75
+ */
49
76
private function buildResponse ($ response , $ body )
50
77
{
51
78
list ($ response ->headers , $ response ->body ) = explode ("\r\n\r\n" , $ body , 2 );
52
79
$ response ->headers = explode ("\r\n" , $ response ->headers );
53
80
list (, $ response ->code , $ response ->message ) = explode (' ' , $ response ->headers [0 ], 3 );
81
+
82
+ return $ response ;
54
83
}
55
84
}
0 commit comments