Move processorCall method to RequestProcessor class

This commit is contained in:
Dmitriy Simushev 2013-01-15 14:01:16 +00:00
parent 95bc033817
commit a650a35dd6
2 changed files with 30 additions and 40 deletions

View File

@ -452,6 +452,30 @@ abstract class RequestProcessor {
); );
} }
/**
* Dispatcher of the functions, provided by the RequestProcessor (or inherited) classes as an external API.
*
* All API methods names starts with 'api' prefix.
* It calls before '<eventPrefix>FunctionCall' event triggers.
*
* @param array &$func Function array equals to array, passed to the '<eventPrefix>FunctionCall' event.
* @see RequestProcessor::registerEvents()
* @todo Create some unit tests
*/
protected function processorCall(&$func) {
$method_name = 'api' . ucfirst($func['function']);
if (is_callable(array($this, $method_name))) {
try {
$func['results'] = $this->$method_name($func['arguments']);
} catch(RequestProcessorException $e) {
$func['results'] = array(
'errorCode' => $e->getCode(),
'errorMessage' => $e->getMessage()
);
}
}
}
/** /**
* Sends synchronous request * Sends synchronous request
* *
@ -496,16 +520,6 @@ abstract class RequestProcessor {
* @return MibewAPI * @return MibewAPI
*/ */
protected abstract function getMibewAPIInstance(); protected abstract function getMibewAPIInstance();
/**
* Dispatcher of the functions, provided by the RequestProcessor (or inherited) classes as an external API.
*
* It calls before '<eventPrefix>FunctionCall' event triggers.
*
* @param array &$func Function array equals to array, passed to the '<eventPrefix>FunctionCall' event.
* @see RequestProcessor::registerEvents()
*/
protected abstract function processorCall(&$func);
} }
class RequestProcessorException extends Exception { class RequestProcessorException extends Exception {

View File

@ -122,30 +122,6 @@ class ThreadProcessor extends RequestProcessor {
return MibewAPI::getAPI('MibewAPIChatInteraction'); return MibewAPI::getAPI('MibewAPIChatInteraction');
} }
/**
* Dispatcher of the functions, provided by the RequestProcessor (or inherited) classes as an external API.
*
* All API methods names starts with 'api' prefix.
* It calls before 'threadFunctionCall' event triggers.
*
* @param array &$func Function array equals to array, passed to the '<eventPrefix>FunctionCall' event.
* @see RequestProcessor::registerEvents()
* @todo Add function to auto create errors to the MibewAPI
*/
protected function processorCall(&$func) {
$method_name = 'api' . ucfirst($func['function']);
if (is_callable(array($this, $method_name))) {
try {
$func['results'] = $this->$method_name($func['arguments']);
} catch(ThreadProcessorException $e) {
$func['results'] = array(
'errorCode' => $e->getCode(),
'errorMessage' => $e->getMessage()
);
}
}
}
/** /**
* Sends asynchronous responses * Sends asynchronous responses
* *
@ -153,11 +129,11 @@ class ThreadProcessor extends RequestProcessor {
*/ */
protected function sendAsyncResponses($responses) { protected function sendAsyncResponses($responses) {
header("Content-type: text/plain; charset=UTF-8"); header("Content-type: text/plain; charset=UTF-8");
echo($this->mibewAPI->encodePackage( echo($this->mibewAPI->encodePackage(
$responses, $responses,
$this->config['signature'], $this->config['signature'],
true true
)); ));
} }
/** /**
@ -436,7 +412,7 @@ class ThreadProcessor extends RequestProcessor {
} }
} }
class ThreadProcessorException extends Exception { class ThreadProcessorException extends RequestProcessorException {
/** /**
* Wrong arguments set for an API function * Wrong arguments set for an API function
*/ */