From ddbc37266f5270df7f95f91710eacadb774e6d0a Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Tue, 24 Feb 2009 12:18:37 -0500 Subject: [PATCH] Notification methods to it's own class --- .../lib/Horde/Service/Facebook.php | 120 ++++++++------------- .../lib/Horde/Service/Facebook/Notifications.php | 83 ++++++++++++++ framework/Service_Facebook/package.xml | 2 + 3 files changed, 127 insertions(+), 78 deletions(-) create mode 100644 framework/Service_Facebook/lib/Horde/Service/Facebook/Notifications.php diff --git a/framework/Service_Facebook/lib/Horde/Service/Facebook.php b/framework/Service_Facebook/lib/Horde/Service/Facebook.php index bfcf7799d..a0b967884 100644 --- a/framework/Service_Facebook/lib/Horde/Service/Facebook.php +++ b/framework/Service_Facebook/lib/Horde/Service/Facebook.php @@ -62,26 +62,25 @@ class Horde_Service_Facebook */ public $secret; - // The token returned by auth.createToken - protected $_token; - - // Store the current session_key - public $session_key; - - // Session expiry - protected $_session_expires; - - // All parameters passed back to us from FB - public $fb_params; - - // The current session user - public $user; + /** + * Use only ssl resource flag + * + * @var boolean + */ + public $useSslResources = false; - // Use only ssl resource flag - public $use_ssl_resources = false; + /** + * Holds the batch object when building a batch request. + * + * @var Horde_Service_Facebook_Batch + */ private $_batchRequest; - + /** + * Holds an optional logger object + * + * @var Horde_Log_Logger + */ protected $_logger; /** @@ -102,6 +101,7 @@ class Horde_Service_Facebook */ protected $_context; + static protected $_objCache = array(); // TODO: Implement some kind of instance array for these types of classes... protected $_auth = null; // H_S_Facebook_Auth @@ -124,16 +124,19 @@ class Horde_Service_Facebook * no_resolve - set to true to prevent attempting to obtain a session * from an auth_token. Useful if client code wants to * handle this. - * + * * @param session_key */ public function __construct($api_key, $secret, $context) { - // We require a http client object - if (empty($context['http_client'])) { + // We require a http client object, but we can get it from a + // controller if we have one. + if (empty($context['http_client']) && empty($context['controller'])) { throw new InvalidArgumentException('A http client object is required'); - } else { + } elseif (!empty($context['http_client'])) { $this->_http = $context['http_client']; + } else { + $this->_http = $context['controller']->getRequest(); } // Required Horde_Controller_Request object @@ -170,17 +173,27 @@ class Horde_Service_Facebook return $this->auth->validateSession(empty($this->_context['no_resolve'])); } - // Lazy loader + /** + * Lazy load the facebook classes. + * + * @param string $value The lowercase representation of the subclass. + * + * @throws Horde_Service_Facebook_Exception + * @return Horde_Service_Facebook_* object. + */ public function __get($value) { - // TODO: Some kind of array/hash to hold valid types - maybe a - // factory method to instantiate these? - if ($value == 'auth') { - if (empty($this->_auth)) { - $this->_auth = new Horde_Service_Facebook_Auth($this, $this->_request); - } - return $this->_auth; + $class = 'Horde_Service_Facebook_' . ucfirst($value); + if (!empty(self::$_objCache[$class])) { + return self::$_objCache[$class]; } + + if (!class_exists($class)) { + throw new Horde_Service_Facebook_Exception(sprintf("%s class not found", $class)); + } + + self::$_objCache[$class] = new $class($this, $this->_request); + return self::$_objCache[$class]; } /** @@ -409,55 +422,6 @@ class Horde_Service_Facebook } /** - * Returns the outstanding notifications for the session user. - * - * @return array An assoc array of notification count objects for - * 'messages', 'pokes' and 'shares', a uid list of - * 'friend_requests', a gid list of 'group_invites', - * and an eid list of 'event_invites' - */ - public function ¬ifications_get() - { - return $this->call_method('facebook.notifications.get'); - } - - /** - * Sends a notification to the specified users. - * - * @return A comma separated list of successful recipients - * @error - * API_EC_PARAM_USER_ID_LIST - */ - public function ¬ifications_send($to_ids, $notification, $type) - { - return $this->call_method('facebook.notifications.send', - array('to_ids' => $to_ids, - 'notification' => $notification, - 'type' => $type)); - } - - /** - * Sends an email to the specified user of the application. - * - * @param string $recipients comma-separated ids of the recipients - * @param string $subject subject of the email - * @param string $text (plain text) body of the email - * @param string $fbml fbml markup for an html version of the email - * - * @return string A comma separated list of successful recipients - * @error - * API_EC_PARAM_USER_ID_LIST - */ - public function ¬ifications_sendEmail($recipients, $subject, $text, $fbml) - { - return $this->call_method('facebook.notifications.sendEmail', - array('recipients' => $recipients, - 'subject' => $subject, - 'text' => $text, - 'fbml' => $fbml)); - } - - /** * Adds a tag with the given information to a photo. See the wiki for details: * * http://wiki.developers.facebook.com/index.php/Photos.addTag diff --git a/framework/Service_Facebook/lib/Horde/Service/Facebook/Notifications.php b/framework/Service_Facebook/lib/Horde/Service/Facebook/Notifications.php new file mode 100644 index 000000000..3ea891dea --- /dev/null +++ b/framework/Service_Facebook/lib/Horde/Service/Facebook/Notifications.php @@ -0,0 +1,83 @@ + + * @category Horde + * @package Horde_Service_Facebook + */ +class Horde_Service_Facebook_Notifications extends Horde_Service_Facebook_Base +{ + /** + * Returns the outstanding notifications for the session user. + * + * @throws Horde_Service_Facebook_Exception + * @return array An assoc array of notification count objects for + * 'messages', 'pokes' and 'shares', a uid list of + * 'friend_requests', a gid list of 'group_invites', + * and an eid list of 'event_invites' + */ + public function &get() + { + // Session key is *required* + if (empty($this->_sessionKey)) { + throw new Horde_FaceBook_Exception('session_key is required', + Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED); + } + return $this->_facebook->call_method('facebook.notifications.get', + array('session_key' => $this->_sessionKey)); + } + + /** + * Sends a notification to the specified users. + * + * @throws Horde_Service_Facebook_Exception + * @return A comma separated list of successful recipients + * @error API_EC_PARAM_USER_ID_LIST + */ + public function &send($to_ids, $notification, $type) + { + // Session key is *required* + if (empty($this->_sessionKey)) { + throw new Horde_FaceBook_Exception('session_key is required', + Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED); + } + + return $this->call_method('facebook.notifications.send', + array('to_ids' => $to_ids, + 'notification' => $notification, + 'type' => $type, + 'session_key' => $this->_sessionKey)); + } + + /** + * Sends an email to the specified user of the application. + * + * @param string $recipients comma-separated ids of the recipients + * @param string $subject subject of the email + * @param string $text (plain text) body of the email + * @param string $fbml fbml markup for an html version of the email + * + * @throws Horde_Service_Facebook_Exception + * @return string A comma separated list of successful recipients + * @error + * API_EC_PARAM_USER_ID_LIST + */ + public function &sendEmail($recipients, $subject, $text, $fbml) + { + // Session key is *required* + if (empty($this->_sessionKey)) { + throw new Horde_FaceBook_Exception('session_key is required', + Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED); + } + return $this->call_method('facebook.notifications.sendEmail', + array('recipients' => $recipients, + 'subject' => $subject, + 'text' => $text, + 'fbml' => $fbml, + 'session_key' => $this->_sessionKey)); + } + +} \ No newline at end of file diff --git a/framework/Service_Facebook/package.xml b/framework/Service_Facebook/package.xml index d69bc3467..da26c4c0d 100644 --- a/framework/Service_Facebook/package.xml +++ b/framework/Service_Facebook/package.xml @@ -36,6 +36,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -67,6 +68,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + -- 2.11.0