From: Michael J. Rubinsky Date: Wed, 18 Feb 2009 01:11:21 +0000 (-0500) Subject: Move batch functionality to it's own class. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=9a2dfad73a982b29792e67118ccae1ee243ce255;p=horde.git Move batch functionality to it's own class. Fix visibility of some methods and members. --- diff --git a/framework/Service_Facebook/lib/Horde/Service/Facebook.php b/framework/Service_Facebook/lib/Horde/Service/Facebook.php index 0d93cd707..8f6e68fd5 100644 --- a/framework/Service_Facebook/lib/Horde/Service/Facebook.php +++ b/framework/Service_Facebook/lib/Horde/Service/Facebook.php @@ -84,8 +84,7 @@ class Horde_Service_Facebook // Flag to indicate we are in batch mode. protected $_batch_mode; - // The array to hold batch operations in batch mode. - protected $_batch_queue; + private $_batchRequest; // // Internal call_id counter // protected $_last_call_id = 0; @@ -152,7 +151,6 @@ class Horde_Service_Facebook $this->secret = $secret; $this->_app_secret = $secret; $this->validate_fb_params(); - $this->batch_mode = self::BATCH_MODE_DEFAULT; $this->_call_as_apikey = ''; // Set the default user id for methods that allow the caller to @@ -564,13 +562,13 @@ class Horde_Service_Facebook */ public function begin_batch() { - if ($this->_batch_queue !== null) { + if ($this->_batchRequest !== null) { $code = Horde_Service_Facebook_ErrorCodes::API_EC_BATCH_ALREADY_STARTED; $description = Horde_Service_Facebook_ErrorCodes::$api_error_descriptions[$code]; throw new Horde_Service_Facebook_Exception($description, $code); } - $this->_batch_queue = array(); + $this->_batchRequest = new Horde_Service_Facebook_BatchRequest($this, $this->_http); } /** @@ -578,63 +576,14 @@ class Horde_Service_Facebook */ public function end_batch() { - if ($this->_batch_queue === null) { + if ($this->_batchRequest === null) { $code = Horde_Service_Facebook_ErrorCodes::API_EC_BATCH_NOT_STARTED; $description = Horde_Service_Facebook_ErrorCodes::$api_error_descriptions[$code]; throw new Horde_Service_Facebook_Exception($description, $code); } - $this->execute_server_side_batch(); - $this->_batch_queue = null; - } - - /** - * Execute a set of batch operations. - * - * @return void - */ - private function execute_server_side_batch() - { - $item_count = count($this->_batch_queue); - $method_feed = array(); - foreach ($this->_batch_queue as $batch_item) { - $method = $batch_item['m']; - $params = $batch_item['p']; - $this->finalize_params($method, $params); - $method_feed[] = $this->create_post_string($params); - } - $method_feed_json = json_encode($method_feed); - - $serial_only = ($this->batch_mode == self::BATCH_MODE_SERIAL_ONLY); - $params = array('method_feed' => $method_feed_json, - 'serial_only' => $serial_only); - if ($this->_call_as_apikey) { - $params['call_as_apikey'] = $this->_call_as_apikey; - } - - // Request JSON format - $params['format'] = 'JSON'; - $json = $this->post_request('batch.run', $params); - // For now, get back a hash instead of a stdObject until more of the - // library gets refactored. - $result = json_decode($json, true); - if (is_array($result) && isset($result['error_code'])) { - throw new Horde_Service_Facebook_Exception($result['error_msg'], - $result['error_code']); - } - - for ($i = 0; $i < $item_count; $i++) { - $batch_item = $this->_batch_queue[$i]; - $batch_item_json = $result[$i]; - $batch_item_result = json_decode($batch_item_json, true); - if (is_array($batch_item_result) && - isset($batch_item_result['error_code'])) { - - throw new Horde_Service_Facebook_Exception($batch_item_result['error_msg'], - $batch_item_result['error_code']); - } - $batch_item['r'] = $batch_item_result; - } + $this->_batchRequest->run(); + $this->_batchRequest = null; } /** @@ -661,7 +610,7 @@ class Horde_Service_Facebook public function auth_getSession($auth_token) { //Check if we are in batch mode - if ($this->_batch_queue === null) { + if ($this->_batchRequest === null) { $result = $this->call_method('facebook.auth.getSession', array('auth_token' => $auth_token)); $this->session_key = $result['session_key']; if (!empty($result['secret'])) { @@ -1378,9 +1327,12 @@ class Horde_Service_Facebook */ public function &call_method($method, $params = array()) { - - $request = new Horde_Service_Facebook_Request($this, $method, $this->_http, $params); - $results = &$request->run(); + if ($this->_batchRequest === null) { + $request = new Horde_Service_Facebook_Request($this, $method, $this->_http, $params); + $results = &$request->run(); + } else { + $results = &$this->_batchRequest->add($method, $params); + } return $results; } diff --git a/framework/Service_Facebook/lib/Horde/Service/Facebook/BatchRequest.php b/framework/Service_Facebook/lib/Horde/Service/Facebook/BatchRequest.php index 1e502dc74..acd417a1c 100644 --- a/framework/Service_Facebook/lib/Horde/Service/Facebook/BatchRequest.php +++ b/framework/Service_Facebook/lib/Horde/Service/Facebook/BatchRequest.php @@ -4,6 +4,75 @@ * * */ -class Horde_Service_Facebook_BatchRequest extents Horde_Service_Facebook_Request +class Horde_Service_Facebook_BatchRequest extends Horde_Service_Facebook_Request { + private $_queue = array(); + private $_batchMode = Horde_Service_Facebook::BATCH_MODE_DEFAULT; + + public function __construct($facebook, $http_client, $params = array()) + { + $this->_http = $http_client; + $this->_facebook = $facebook; + + if (!empty($params['batch_mode'])) { + $this->_batchMode = $params['batch_mode']; + } + } + + /** + * Add a method call to the queue + * + * @param $method + * @param $params + * @return unknown_type + */ + public function &add($method, $params) + { + $result = null; + $batch_item = array('m' => $method, 'p' => $params, 'r' => &$result); + $this->_queue[] = $batch_item; + return $result; + } + + /** + * Execute a set of batch operations. + * + * @return void + */ + public function run() + { + $item_count = count($this->_queue); + $method_feed = array(); + foreach ($this->_queue as $batch_item) { + $method = $batch_item['m']; + $params = $batch_item['p']; + $this->_finalize_params($method, $params); + $method_feed[] = $this->_create_post_string($params); + } + $method_feed_json = json_encode($method_feed); + + $serial_only = ($this->_batchMode == Horde_Service_Facebook::BATCH_MODE_SERIAL_ONLY); + $params = array('method_feed' => $method_feed_json, + 'serial_only' => $serial_only); + $json = $this->_post_request('batch.run', $params); + $result = json_decode($json, true); + if (is_array($result) && isset($result['error_code'])) { + throw new Horde_Service_Facebook_Exception($result['error_msg'], + $result['error_code']); + } + + for ($i = 0; $i < $item_count; $i++) { + $batch_item = $this->_queue[$i]; + $batch_item_json = $result[$i]; + $batch_item_result = json_decode($batch_item_json, true); + if (is_array($batch_item_result) && + isset($batch_item_result['error_code'])) { + + throw new Horde_Service_Facebook_Exception($batch_item_result['error_msg'], + $batch_item_result['error_code']); + } + $batch_item['r'] = $batch_item_result; + } + } + } diff --git a/framework/Service_Facebook/lib/Horde/Service/Facebook/Request.php b/framework/Service_Facebook/lib/Horde/Service/Facebook/Request.php index f9f15f1eb..370594fed 100644 --- a/framework/Service_Facebook/lib/Horde/Service/Facebook/Request.php +++ b/framework/Service_Facebook/lib/Horde/Service/Facebook/Request.php @@ -5,9 +5,9 @@ */ class Horde_Service_Facebook_Request { - private $_facebook; - private $_last_call_id = 0; - private $_http; + protected $_facebook; + protected $_last_call_id = 0; + protected $_http; private $_method; private $_params; @@ -53,7 +53,7 @@ class Horde_Service_Facebook_Request * @param $params * @return unknown_type */ - private function _finalize_params($method, &$params) + protected function _finalize_params($method, &$params) { $this->_add_standard_params($method, $params); // we need to do this before signing the params @@ -61,7 +61,7 @@ class Horde_Service_Facebook_Request $params['sig'] = Horde_Service_Facebook::generate_sig($params, $this->_facebook->secret); } - private function _add_standard_params($method, &$params) + protected function _add_standard_params($method, &$params) { // We only support JSON $params['format'] = 'json'; @@ -81,7 +81,7 @@ class Horde_Service_Facebook_Request } } - private function convert_array_values_to_csv(&$params) + protected function convert_array_values_to_csv(&$params) { foreach ($params as $key => &$val) { if (is_array($val)) { @@ -94,7 +94,7 @@ class Horde_Service_Facebook_Request * TODO: Figure out why using http_build_query doesn't work here. * */ - private function _create_post_string($params) + protected function _create_post_string($params) { $post_params = array(); foreach ($params as $key => &$val) {