Skip to content

Commit cbcaec1

Browse files
committed
Merge pull request #27 from enygma/remove-curl
Remove curl
2 parents 2a67d5d + 875885f commit cbcaec1

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

src/CodeClimate/Bundle/TestReporterBundle/ApiClient.php

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class ApiClient
55
{
66
protected $apiHost;
77

8+
/**
9+
* Init the API client and set the hostname
10+
*/
811
public function __construct()
912
{
1013
$this->apiHost = "https://codeclimate.com";
@@ -15,41 +18,67 @@ public function __construct()
1518

1619
}
1720

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+
*/
1827
public function send($json)
1928
{
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-
3129
$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);
3451

35-
while ($response->code == 100) { // Continue
36-
$this->buildResponse($response, $response->body);
52+
if (!empty($raw_response)) {
53+
$response = $this->buildResponse($response, $raw_response);
3754
}
3855
} 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'];
4162
$response->headers = array();
4263
$response->body = NULL;
4364
}
44-
curl_close($ch);
4565

4666
return $response;
4767
}
4868

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+
*/
4976
private function buildResponse($response, $body)
5077
{
5178
list($response->headers, $response->body) = explode("\r\n\r\n", $body, 2);
5279
$response->headers = explode("\r\n", $response->headers);
5380
list(, $response->code, $response->message) = explode(' ', $response->headers[0], 3);
81+
82+
return $response;
5483
}
5584
}

0 commit comments

Comments
 (0)