Notification methods to it's own class
authorMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 24 Feb 2009 17:18:37 +0000 (12:18 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Wed, 25 Feb 2009 19:13:11 +0000 (14:13 -0500)
framework/Service_Facebook/lib/Horde/Service/Facebook.php
framework/Service_Facebook/lib/Horde/Service/Facebook/Notifications.php [new file with mode: 0644]
framework/Service_Facebook/package.xml

index bfcf779..a0b9678 100644 (file)
@@ -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.
-     *
+     * </pre>
      * @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 &notifications_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 &notifications_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 &notifications_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 (file)
index 0000000..3ea891d
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+/**
+ * Notifications methods for Horde_Service_Faceboook
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org)
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @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
index d69bc34..da26c4c 100644 (file)
@@ -36,6 +36,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <file name="Base.php" role="php" />
        <file name="Fql.php" role="php" />
        <file name="Friends.php" role="php" />
+       <file name="Notifications.php" role="php" />
        <file name="Exception.php" role="php" />
        <file name="ErrorCodes.php" role="php" />
        <file name="Request.php" role="php" />
@@ -67,6 +68,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Service/Facebook/Base.php" as="Horde/Service/Facebook/Base.php" />
    <install name="lib/Horde/Service/Facebook/Fql.php" as="Horde/Service/Facebook/Fql.php" />
    <install name="lib/Horde/Service/Facebook/Friends.php" as="Horde/Service/Facebook/Friends.php" />
+   <install name="lib/Horde/Service/Facebook/Notifications.php" as="Horde/Service/Facebook/Notifications.php" />
    <install name="lib/Horde/Service/Facebook/Exception.php" as="Horde/Service/Facebook/Exception.php" />
    <install name="lib/Horde/Service/Facebook/ErrorCodes.php" as="Horde/Service/Facebook/ErrorCodes.php" />
    <install name="lib/Horde/Service/Facebook/Request.php" as="Horde/Service/Facebook/Request.php" />