Add ability to cache the returned data if a cache object is present.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 8 Jan 2009 01:02:18 +0000 (20:02 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 8 Jan 2009 01:14:00 +0000 (20:14 -0500)
Also: some phpdoc and remove some unused methods.

framework/Service_Vimeo/lib/Horde/Service/Vimeo.php
framework/Service_Vimeo/lib/Horde/Service/Vimeo/Simple.php

index 7cb6336..aad3c99 100644 (file)
@@ -81,8 +81,12 @@ class Horde_Service_Vimeo {
      */
     public function getEmbedJson($options)
     {
-        $request = new Horde_Service_Vimeo_Request();
-        return $request->embed($options);
+        if (!empty($this->_cache)) {
+            $params = array('cache' => array('object' => $this->_cache, 'lifetime' => $this->_cache_lifetime));
+        }
+        $request = new Horde_Service_Vimeo_Request($params);
+
+        return $request->getEmbedJson($options);
     }
 
     /**
index 2046244..aad10dc 100644 (file)
  */
 class Horde_Service_Vimeo_Simple extends Horde_Service_Vimeo {
 
-    public function getActivity($criteria)
-    {
-    }
-
     /**
-     * Group:
-     * User:
-     * Album:
-     * Channel:
+     * An optional cache object that implments the same interface as
+     * Horde_Cache
      *
+     * @var Horde_Cache
+     */
+    protected $_cache;
+    protected $_cache_lifetime;
+
+
+    /**
+     * Inject a cache obect
      *
-     * @param unknown_type $criteria
+     * @param Horde_Cache $cache  The cache object
+     * @param int $lifetime       The cache lifetime in seconds
      */
-    public function getInfo($criteria)
+    public function setCache($cache, $lifetime = 1)
     {
+        $this->_cache = $cache;
+        $this->_cache_lifetime = $lifetime;
     }
 
+    /**
+     * Set up a request based on the method name.
+     *
+     * @TODO: validate that we have a valid method or throw an exception
+     *
+     * @return Horde_Service_Vimeo_Request
+     */
     public function __call($name, $args)
     {
-        return new Horde_Service_Vimeo_Request(array('type' => $name, 'identifier' => $args[0]));
+        $params = array('type' => $name,
+                        'identifier' => $args[0],
+                        'cache' => array('object' => $this->_cache,
+                                          'lifetime' => $this->_cache_lifetime));
+
+        return new Horde_Service_Vimeo_Request($params);
     }
 
 }
 
 class Horde_Service_Vimeo_Request {
+
+    /**
+     * Cache object
+     *
+     * @var Horde_Cache
+     */
+    private $_cache;
+
+    // The vimeo simple api endpoint
     protected $_api_endpoint = 'http://www.vimeo.com/api';
+
+    // The vimeo oembed api endpoint
     protected $_oembed_endpoint = 'http://www.vimeo.com/api/oembed.json';
 
-    // Qualifier in the URL after /api/
+    // Qualifier in the URL after /api/ (like <user_id> or group/<group_id>)
     protected $_identifier;
+
+    // The api method we are calling (clips, info etc...)
     protected $_method;
+
+    // The type of request (user, group etc...)
     protected $_type;
 
+    // Valid method/type map
     protected $_methodTypes = array('user' => array('clips', 'likes', 'info', 'appears_in', 'all_clips', 'subscriptions', 'albums', 'channels', 'groups', 'contacts_clips', 'contacts_like'),
                                     'group' => array('clips', 'users', 'info'),
                                     'channel' => array('clips', 'info'),
                                     'album' => array('clips', 'info'));
 
+    /**
+     * Contructor
+     *
+     * @param Horde_Service_Vimeo $parent  The requesting object
+     * @param array $args                  Argument array
+     */
     public function __construct($args = array())
     {
-        if (count($args) && !empty($args['type'])) {
-
-            // Might be useful to know the type at some point
-            $this->_type = $args['type'];
-            switch ($args['type']) {
-            case 'user':
-                $this->_identifier = $args['identifier'];
-                break;
-            case 'group':
-                $this->_identifier = '/group/' . $args['identifier'];
-                break;
-            case 'channel':
-                $this->_identifier = '/channel/' . $args['identifier'];
-                break;
-            case 'album':
-                $this->_identifier = '/album/' . $args['identifier'];
-                break;
+        if (count($args)) {
+            $this->_cache = isset($args['cache']) ? $args['cache'] : null;
+            if (!empty($args['type'])) {
+
+                // The type of method we are calling (user, group, etc...)
+                $this->_type = $args['type'];
+
+                switch ($args['type']) {
+                case 'user':
+                    $this->_identifier = $args['identifier'];
+                    break;
+                case 'group':
+                    $this->_identifier = '/group/' . $args['identifier'];
+                    break;
+                case 'channel':
+                    $this->_identifier = '/channel/' . $args['identifier'];
+                    break;
+                case 'album':
+                    $this->_identifier = '/album/' . $args['identifier'];
+                    break;
+                }
             }
         }
     }
@@ -89,7 +132,17 @@ class Horde_Service_Vimeo_Request {
         return $this;
     }
 
-    public function embed($options)
+    /**
+     * Obtain the JSON needed to embed a single Vimeo video specified by the
+     * parameter. Passing a url is the most effecient as we won't have to query
+     * the vimeo service for the url.
+     *
+     * @param mixed $options  Either an array containing the vimeo url or
+     *                        vimeo clip id, OR a scaler containing the clip id.
+
+     * @return unknown
+     */
+    public function getEmbedJson($options)
     {
         if (!is_array($options)) {
             // Assume it's a video id, need to get the video url
@@ -102,12 +155,25 @@ class Horde_Service_Vimeo_Request {
             // @TODO
         }
 
+        // See if we have a cache, and if so, try to get the data from it before
+        // polling the vimeo service.
+        if (!empty($this->_cache['object'])) {
+            $cache_key = 'VimeoJson' . md5(serialize($options));
+            $data = $this->_cache['object']->get($cache_key, $this->_cache['lifetime']);
+            if ($data !== false) {
+                return unserialize($data);
+            }
+        }
+
         // We should have a url now, and possibly other options.
         $url = Util::addParameter($this->_oembed_endpoint, $options, null, false);
 
         $req = Horde_Service_Vimeo::getHttpClient();
         $response = $req->request('GET', $url);
         $results = $response->getBody();
+        if (!empty($this->_cache)) {
+            $this->_cache['object']->set($cache_key, serialize($results));
+        }
 
         return $results;
     }
@@ -115,8 +181,35 @@ class Horde_Service_Vimeo_Request {
 
     public function run()
     {
+        $call =  '/' . $this->_identifier . '/' . $this->_method . '.' . Horde_Service_Vimeo::getFormat();
+        if (!empty($this->_cache['object'])) {
+            $cache_key = 'VimeoRequest' . md5($call);
+            $data = $this->_cache['object']->get($cache_key, $this->_cache['lifetime']);
+            if ($data !== false) {
+                // php format is already returned serialized
+                if (Horde_Service_Vimeo::getFormat() != 'php') {
+                    $data = unserialize($data);
+                }
+
+                return $data;
+            }
+        }
+
         $req = Horde_Service_Vimeo::getHttpClient();
-        $response = $req->request('GET', $this->_api_endpoint . '/' . $this->_identifier . '/' . $this->_method . '.' . Horde_Service_Vimeo::getFormat());
-        return $response->getBody();
+        $response = $req->request('GET', $this->_api_endpoint . $call);
+        $data = $response->getBody();
+
+        if (!empty($this->_cache['object'])) {
+            if (Horde_Service_Vimeo::getFormat() != 'php') {
+                $sdata = serialize($data);
+            } else {
+                $sdata = $data;
+            }
+            $this->_cache['object']->set($cache_key, $sdata);
+        }
+
+        return $data;
+
     }
+
 }
\ No newline at end of file