Overview

Packages

  • None
  • Service
    • Facebook

Classes

  • Horde_Service_Facebook_Auth
  • Horde_Service_Facebook_Base
  • Horde_Service_Facebook_BatchRequest
  • Horde_Service_Facebook_ErrorCodes
  • Horde_Service_Facebook_Events
  • Horde_Service_Facebook_Exception
  • Horde_Service_Facebook_Fql
  • Horde_Service_Facebook_Friends
  • Horde_Service_Facebook_Groups
  • Horde_Service_Facebook_Links
  • Horde_Service_Facebook_Notes
  • Horde_Service_Facebook_Notifications
  • Horde_Service_Facebook_Photos
  • Horde_Service_Facebook_Request
  • Horde_Service_Facebook_Streams
  • Horde_Service_Facebook_Translation
  • Horde_Service_Facebook_UploadRequest
  • Horde_Service_Facebook_Users
  • Horde_Service_Facebook_Videos
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Horde_Service_Facebook_BatchRequest::
  4:  *
  5:  * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * @author Michael J. Rubinsky <mrubinsk@horde.org>
  8:  * @category Horde
  9:  * @package Service_Facebook
 10:  */
 11: class Horde_Service_Facebook_BatchRequest extends Horde_Service_Facebook_Request
 12: {
 13:     /**
 14:      *  Holds pending operations
 15:      *
 16:      * @var array
 17:      */
 18:     private $_queue = array();
 19: 
 20:     /**
 21:      * Current mode.
 22:      *
 23:      * @var BATCH_MODE_* Constant
 24:      */
 25:     private $_batchMode;
 26: 
 27:     /** BATCH_MODE constants **/
 28:     const BATCH_MODE_DEFAULT = 0;
 29:     const BATCH_MODE_SERVER_PARALLEL = 0;
 30:     const BATCH_MODE_SERIAL_ONLY = 2;
 31: 
 32:     /**
 33:      * Constructor
 34:      *
 35:      * @param Horde_Service_Facebook $facebook
 36:      */
 37:     public function __construct(Horde_Service_Facebook $facebook)
 38:     {
 39:         $this->_facebook = $facebook;
 40:         $this->_http = $facebook->http;
 41:         if (!empty($params['batch_mode'])) {
 42:             $this->_batchMode = $params['batch_mode'];
 43:         } else {
 44:             $this->_batchMode = self::BATCH_MODE_DEFAULT;
 45:         }
 46:     }
 47: 
 48:     /**
 49:      * Add a method call to the queue
 50:      *
 51:      * @param string $method  The API method to call.
 52:      * @param  array $params  The API method parameters.
 53:      *
 54:      * @return mixed  Returns a reference to the results that will be
 55:      *                       produced when the batch is run. This reference
 56:      *                       should be saved until after the batch is run and
 57:      *                       the results can be examined.
 58:      */
 59:     public function &add($method, array $params)
 60:     {
 61:         $result = null;
 62:         $batch_item = array('m' => $method, 'p' => $params, 'r' => &$result);
 63:         $this->_queue[] = $batch_item;
 64:         return $result;
 65:     }
 66: 
 67:     /**
 68:      * Execute a set of batch operations.
 69:      *
 70:      * @return void
 71:      */
 72:     public function run()
 73:     {
 74:         $item_count = count($this->_queue);
 75:         $method_feed = array();
 76:         foreach ($this->_queue as $batch_item) {
 77:             $params = $batch_item['p'];
 78:             $params['method'] = $batch_item['m'];
 79:             $this->_finalizeParams($params);
 80:             $method_feed[] = $this->_createPostString($params);
 81:         }
 82:         $method_feed_json = json_encode($method_feed);
 83: 
 84:         $serial_only = ($this->_batchMode == self::BATCH_MODE_SERIAL_ONLY);
 85:         $params = array('method_feed' => $method_feed_json,
 86:                         'serial_only' => $serial_only,
 87:                         'session_key' => $this->_facebook->auth->getSessionKey());
 88:         $json = $this->_postRequest('batch.run', $params);
 89:         $result = json_decode($json, true);
 90: 
 91:         if (is_array($result) && isset($result['error_code'])) {
 92:           throw new Horde_Service_Facebook_Exception($result['error_msg'],
 93:                                                      $result['error_code']);
 94:         }
 95: 
 96:         for ($i = 0; $i < $item_count; $i++) {
 97:             $batch_item = $this->_queue[$i];
 98:             $batch_item_json = $result[$i];
 99:             $batch_item_result = json_decode($batch_item_json, true);
100:             if (is_array($batch_item_result) &&
101:                 isset($batch_item_result['error_code'])) {
102: 
103:                 throw new Horde_Service_Facebook_Exception($batch_item_result['error_msg'],
104:                                                            $batch_item_result['error_code']);
105:             }
106:             $batch_item['r'] = $batch_item_result;
107:         }
108:     }
109: 
110: }
111: 
API documentation generated by ApiGen