<?php
-
-/** HTTP_Request **/
-require_once 'HTTP/Request.php';
-
/**
* Horde_Serivce_Vimeo:: wrapper around Vimeo's (http://www.vimeo.com)
* API.
*/
class Horde_Service_Vimeo {
- protected static $_format = 'php';
+ /**
+ * The format of the data returned from Vimeo.
+ * Obviously does not apply to the getEmbedJson() method.
+ *
+ * php - serialized php array
+ * json - json encoded
+ *
+ * @var string
+ */
+ protected $_format = 'php';
/**
* HTTP client object to use for accessing the Vimeo API.
* @var Horde_Http_Client
*/
- protected static $_httpClient = null;
-
- protected $_cache;
- protected $_cache_lifetime;
+ protected $_http_client;
/**
- * Set the HTTP client instance
- *
- * Sets the HTTP client object to use for Vimeo requests. If none is set,
- * the default Horde_Http_Client will be used.
+ * An optional cache object
*
- * @param Horde_Http_Client $httpClient
+ * @var Horde_Cache
*/
- public static function setHttpClient($httpClient)
- {
- self::$_httpClient = $httpClient;
- }
+ protected $_cache;
/**
- * Gets the HTTP client object.
+ * The lifetime of any cached data in seconds.
*
- * @return Horde_Http_Client
+ * @var int
*/
- public static function getHttpClient()
- {
- if (!self::$_httpClient) {
- self::$_httpClient = new Horde_Http_Client;
- }
-
- return self::$_httpClient;
- }
-
- public static function getFormat()
- {
- return self::$_format;
- }
-
- public static function setFormat($format)
- {
- self::$_format = $format;
- }
+ protected $_cache_lifetime = 60;
/**
- * Inject a cache obect
+ * Setter for changing the format parameter
*
- * @param Horde_Cache $cache The cache object
- * @param int $lifetime The cache lifetime in seconds
+ * @param string $format The data format requested.
*/
- protected function _setCache($cache, $lifetime = 60)
+ public function setFormat($format)
{
- $this->_cache = $cache;
- $this->_cache_lifetime = $lifetime;
+ $this->_format = $format;
}
/**
- * Get the raw JSON response containing the data to embed a single video.
+ * Facory method. Attempt to return a concrete Horde_Service_Vimeo instance
+ * based on the parameters. A 'http_client' parameter is required. An
+ * optional 'cache' and 'cache_lifetime' parameters are also taken.
*
- * @param mixed $optons Either an array containing api parameters or the
- * video id. If an array, if the url is not passed,
- * we find it from the video_id.
- * Parameters:
- * url OR video_id
- * width
- * maxwidth
- * byline
- * title
- * portrait
- * color
- * callback
+ * @param string $driver The concrete class to instantiate.
+ * @param array $params An array containing any parameters the class needs.
*
- * @return JSON encoded data
+ * @return Horde_Service_Vimeo object
*/
- public function getEmbedJson($options)
+ public static function factory($driver = 'Simple', $params = null)
{
- $params = array('cache' => array('object' => $this->_cache,
- 'lifetime' => $this->_cache_lifetime));
- $request = new Horde_Service_Vimeo_Request($params);
- return $request->getEmbedJson($options);
- }
+ // Check for required dependencies
+ if (empty($params['http_client'])) {
+ // Throw exception
+ }
- /**
- */
- public function factory($driver = 'Simple', $params = null)
- {
$driver = basename($driver);
include_once dirname(__FILE__) . '/Vimeo/' . $driver . '.php';
$class = 'Horde_Service_Vimeo_' . $driver;
if (class_exists($class)) {
- return new $class($params);
+ return new $class($params['http_client'], $params);
} else {
// @TODO: Exceptions!!!
Horde::fatal(PEAR::raiseError(sprintf(_("Unable to load the definition of %s."), $class)), __FILE__, __LINE__);
}
}
- private function __construct($params)
+ /**
+ * Constructor
+ *
+ * @param Horde_Http_Client $http_client Http client object.
+ * @param array $params An array of any other parameters
+ * or optional object dependencies.
+ *
+ * @return Horde_Service_Vimeo object
+ */
+ private function __construct($http_client, $params)
{
+ $this->_http_client = $http_client;
+
if (isset($params['cache'])) {
- $this->_setCache($params['cache'], $params['cache_lifetime']);
+ $this->_cache = $params['cache'];
+ if (isset($params['cache_lifetime'])) {
+ $this->_cache_lifetime = $params['cache_lifetime'];
+ }
}
-
}
}
\ No newline at end of file
*/
class Horde_Service_Vimeo_Simple extends Horde_Service_Vimeo {
- /**
- * 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)
- {
- $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';
'channel' => array('clips', 'info'),
'album' => array('clips', 'info'));
+
/**
- * Contructor
+ * TODO: Validate the requested method fits with the type of query
*
- * @param Horde_Service_Vimeo $parent The requesting object
- * @param array $args Argument array
+ * @param unknown_type $name
+ * @param unknown_type $args
+ * @return unknown
*/
- public function __construct($args = array())
+ public function __call($name, $args)
{
- $this->_cache = !empty($args['cache']['object']) ? $args['cache'] : null;
+ // Is this a Vimeo type?
+ if (in_array($name, array_keys($this->_methodTypes))) {
- if (count($args) && !empty($args['type'])) {
- // The type of method we are calling (user, group, etc...)
- $this->_type = $args['type'];
+ // Make sure we have an identifier arguament.
+ if (empty($args[0])) {
+ //@TODO Exception
+ }
+
+ // Remember the type we're requesting
+ $this->_type = $name;
- switch ($args['type']) {
+ // Build a valid identifier
+ switch ($name) {
case 'user':
- $this->_identifier = $args['identifier'];
- break;
- case 'group':
- $this->_identifier = '/group/' . $args['identifier'];
+ // user is the default type for a Vimeo simple query
+ $this->_identifier = $args[0];
break;
- case 'channel':
- $this->_identifier = '/channel/' . $args['identifier'];
- break;
- case 'album':
- $this->_identifier = '/album/' . $args['identifier'];
+ default:
+ $this->_identifier = '/' . $name . '/' . $args[0];
break;
}
+
+ return $this;
}
- }
- /**
- * TODO: Validate the requested method fits with the type of query
- *
- * @param unknown_type $name
- * @param unknown_type $args
- * @return unknown
- */
- public function __call($name, $args)
- {
- if (!in_array($name, $this->_methodTypes[$this->_type])) {
- return;
+ // What about a method call - we must have already called a type
+ if (in_array($name, $this->_methodTypes[$this->_type]) && !empty($this->_type)) {
+ $this->_method = $name;
+ return $this;
}
- $this->_method = $name;
- return $this;
+
+ // Don't know what the heck is going on...
+ // @TODO Throw exception
}
/**
* parameter. Passing a url is the most effecient as we won't have to query
* the vimeo service for the url.
*
- * @TODO: Validate that we don't put any extraneous options onto the end
- * of the url (in other words, make sure the options passed make
- * sense for the method we are calling
* @param mixed $options Either an array containing the vimeo url or
* vimeo clip id, OR a scaler containing the clip id.
// 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'])) {
+ if (!empty($this->_cache)) {
$cache_key = 'VimeoJson' . md5(serialize($options));
- $data = $this->_cache['object']->get($cache_key, $this->_cache['lifetime']);
+ $data = $this->_cache->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);
+ $response = $this->_http_client->request('GET', $url);
$results = $response->getBody();
if (!empty($this->_cache)) {
- $this->_cache['object']->set($cache_key, serialize($results));
+ $this->_cache->set($cache_key, serialize($results));
}
return $results;
public function run()
{
- $call = '/' . $this->_identifier . '/' . $this->_method . '.' . Horde_Service_Vimeo::getFormat();
- if (!empty($this->_cache['object'])) {
+ $call = '/' . $this->_identifier . '/' . $this->_method . '.' . $this->_format;
+ if (!empty($this->_cache)) {
$cache_key = 'VimeoRequest' . md5($call);
- $data = $this->_cache['object']->get($cache_key, $this->_cache['lifetime']);
+ $data = $this->_cache->get($cache_key, $this->_cache_lifetime);
if ($data !== false) {
// php format is already returned serialized
- if (Horde_Service_Vimeo::getFormat() != 'php') {
+ if ($this->_format != 'php') {
$data = unserialize($data);
}
}
}
- $req = Horde_Service_Vimeo::getHttpClient();
- $response = $req->request('GET', $this->_api_endpoint . $call);
+ $response = $this->_http_client->request('GET', $this->_api_endpoint . $call);
$data = $response->getBody();
- if (!empty($this->_cache['object'])) {
- if (Horde_Service_Vimeo::getFormat() != 'php') {
+ if (!empty($this->_cache)) {
+ if ($this->_format != 'php') {
$sdata = serialize($data);
} else {
$sdata = $data;
}
- $this->_cache['object']->set($cache_key, $sdata);
+ $this->_cache->set($cache_key, $sdata);
}
return $data;