Skip to content

Commit 3ae5912

Browse files
sunpbrisbin
authored andcommitted
Use cURL extension instead of Guzzle.
1 parent e0b4ab9 commit 3ae5912

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
],
1616
"require": {
1717
"php": ">=5.3",
18-
"guzzle/guzzle": ">=3.0",
18+
"ext-curl": "*",
1919
"satooshi/php-coveralls": ">=0.6.1",
2020
"symfony/console": ">=2.0"
2121
},

src/CodeClimate/Bundle/TestReporterBundle/ApiClient.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
<?php
22
namespace CodeClimate\Bundle\TestReporterBundle;
33

4-
use Guzzle\Http\Client;
5-
use Guzzle\Http\Exception\ClientErrorResponseException;
6-
74
class 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
}

src/CodeClimate/Bundle/TestReporterBundle/Command/TestReporterCommand.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
6161
} else {
6262
$client = new ApiClient();
6363
$response = $client->send($json);
64+
switch ($response->code) {
65+
case 200:
66+
$output->writeln("Test coverage data sent.");
67+
break;
6468

65-
if ($response) {
66-
$code = $response->getStatusCode();
69+
case 401:
70+
$output->writeln("Invalid CODECLIMATE_REPO_TOKEN.");
71+
$ret = 1;
72+
break;
6773

68-
switch ($code) {
69-
case 200:
70-
$output->writeln("Test coverage data sent");
71-
break;
72-
case 401:
73-
$output->writeln("An invalid CODECLIMATE_REPO_TOKEN repo token was specified.");
74-
$ret = 1;
75-
break;
76-
default:
77-
$output->writeln("Status code: ".$code);
78-
$ret = 1;
79-
break;
80-
}
81-
} else {
82-
$output->writeln("Unknown error posting Test coverage data.");
83-
$ret = 1;
74+
default:
75+
$output->writeln("Unexpected response: ".$response->code." ".$response->message);
76+
$output->writeln($response->body);
77+
$ret = 1;
78+
break;
8479
}
8580
}
8681

0 commit comments

Comments
 (0)