code refactored

This commit is contained in:
Steffen Schröder 2014-06-27 00:17:02 +02:00
parent 47a8c5bcfe
commit 15edb02e9e

View File

@ -5,7 +5,6 @@ namespace ManiaControl\Files;
use cURL\Event; use cURL\Event;
use cURL\Exception; use cURL\Exception;
use cURL\Request; use cURL\Request;
use cURL\Response;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
/** /**
@ -25,10 +24,11 @@ class AsynchronousFileReader {
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
/** @var Request[] $requests */
private $requests = array(); private $requests = array();
/** /**
* Construct FileReader * Construct a new Asynchronous File Reader Instance
* *
* @param ManiaControl $maniaControl * @param ManiaControl $maniaControl
*/ */
@ -37,11 +37,10 @@ class AsynchronousFileReader {
} }
/** /**
* Appends the Data * Append available Data of active Requests
*/ */
public function appendData() { public function appendData() {
foreach ($this->requests as $key => $request) { foreach ($this->requests as $key => $request) {
/** @var Request $request */
try { try {
if ($request->socketPerform()) { if ($request->socketPerform()) {
$request->socketSelect(); $request->socketSelect();
@ -71,31 +70,26 @@ class AsynchronousFileReader {
return false; return false;
} }
$headers = array('Content-Type: ' . $contentType); $headers = array();
array_push($headers, 'Content-Type: ' . $contentType);
if ($keepAlive) { if ($keepAlive) {
array_push($headers, 'Keep-Alive: ' . $keepAlive); array_push($headers, 'Keep-Alive: ' . $keepAlive);
array_push($headers, 'Connection: Keep-Alive'); array_push($headers, 'Connection: Keep-Alive');
} }
$request = new Request($url); $request = new Request($url);
$request->getOptions() // request options $this->prepareOptions($request->getOptions())
->set(CURLOPT_TIMEOUT, 10) // timeout
->set(CURLOPT_HEADER, false) // don't display response header
->set(CURLOPT_CRLF, true) // linux linefeed
->set(CURLOPT_ENCODING, '') // accept encoding
->set(CURLOPT_AUTOREFERER, true) // accept link reference ->set(CURLOPT_AUTOREFERER, true) // accept link reference
->set(CURLOPT_HTTPHEADER, $headers) // headers ->set(CURLOPT_HTTPHEADER, $headers); // headers
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) // user-agent
->set(CURLOPT_RETURNTRANSFER, true);
$request->addListener('complete', function (Event $event) use (&$function) { $request->addListener('complete', function (Event $event) use (&$function) {
$response = $event->response;
$error = null; $error = null;
$content = null; $content = null;
if ($response->hasError()) { if ($event->response->hasError()) {
$error = $response->getError()->getMessage(); $error = $event->response->getError()
->getMessage();
} else { } else {
$content = $response->getContent(); $content = $event->response->getContent();
} }
call_user_func($function, $content, $error); call_user_func($function, $content, $error);
}); });
@ -105,7 +99,23 @@ class AsynchronousFileReader {
} }
/** /**
* Adds a Request to the queue * Prepare the cURL Options
*
* @param Options $options
* @return Options
*/
private function prepareOptions(Options $options) {
$options->set(CURLOPT_TIMEOUT, 10)
->set(CURLOPT_HEADER, false) // don't display response header
->set(CURLOPT_CRLF, true) // linux line feed
->set(CURLOPT_ENCODING, '') // accept encoding
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)
->set(CURLOPT_RETURNTRANSFER, true);
return $options;
}
/**
* Add a Request to the queue
* *
* @param Request $request * @param Request $request
*/ */
@ -123,54 +133,44 @@ class AsynchronousFileReader {
* @param string $contentType * @param string $contentType
* @return bool * @return bool
*/ */
public function postData($url, $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8') { public function postData($url, callable $function, $content, $compression = false,
if (!is_callable($function)) { $contentType = 'text/xml; charset=UTF-8;') {
$this->maniaControl->log("Function is not callable");
return false;
}
if (!$url) { if (!$url) {
$this->maniaControl->log("Url is empty"); $this->maniaControl->log("Url is empty");
return false; return false;
} }
$content = str_replace(array("\r", "\n"), '', $content); $content = str_replace(array("\r", "\n"), '', $content);
$headers = array();
array_push($headers, 'Content-Type: ' . $contentType);
array_push($headers, 'Keep-Alive: 300');
array_push($headers, 'Connection: Keep-Alive');
if ($compression) { if ($compression) {
$content = zlib_encode($content, 31); $content = zlib_encode($content, 31);
$header = array("Content-Type: " . $contentType, "Keep-Alive: 300", "Connection: Keep-Alive", "Content-Encoding: gzip"); array_push($headers, 'Content-Encoding: gzip');
} else {
$header = array("Content-Type: " . $contentType, "Keep-Alive: 300", "Connection: Keep-Alive");
} }
$request = new Request($url); $request = new Request($url);
$request->getOptions()->set(CURLOPT_HEADER, false) //don't display response header $this->prepareOptions($request->getOptions())
->set(CURLOPT_CRLF, true) //linux linefeed ->set(CURLOPT_POST, true) // post method
->set(CURLOPT_ENCODING, '')//accept encoding ->set(CURLOPT_POSTFIELDS, $content) // post content field
//->set(CURLOPT_AUTOREFERER, true)//accept link reference ->set(CURLOPT_HTTPHEADER, $headers); // headers
->set(CURLOPT_POST, true) //post field
->set(CURLOPT_POSTFIELDS, $content) //post content field
->set(CURLOPT_HTTPHEADER, $header) //
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) //
->set(CURLOPT_RETURNTRANSFER, true) //
->set(CURLOPT_TIMEOUT, 10);
$request->addListener('complete', function (Event $event) use (&$function) { $request->addListener('complete', function (Event $event) use (&$function) {
/** @var Response $response */
$response = $event->response;
$error = null; $error = null;
$content = null; $content = null;
if ($response->hasError()) { if ($event->response->hasError()) {
$error = $response->getError()->getMessage(); $error = $event->response->getError()
->getMessage();
} else { } else {
$content = $response->getContent(); $content = $event->response->getContent();
} }
call_user_func($function, $content, $error); call_user_func($function, $content, $error);
}); });
$this->addRequest($request); $this->addRequest($request);
return true; return true;
} }
} }