From: Michael J. Rubinsky Date: Fri, 9 Jan 2009 00:16:42 +0000 (-0500) Subject: - Refactor a bit to better deal with object dependencies. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=81b8411c256fcc6385eeb5b1f5548addb9d99ae2;p=horde.git - Refactor a bit to better deal with object dependencies. - Get rid of the *_Vimeo_Request class and roll all that functionality into the Horde_Service_Vimeo object. --- diff --git a/framework/Service_Vimeo/lib/Horde/Service/Vimeo.php b/framework/Service_Vimeo/lib/Horde/Service/Vimeo.php index 16786ab2d..d650e0eb5 100644 --- a/framework/Service_Vimeo/lib/Horde/Service/Vimeo.php +++ b/framework/Service_Vimeo/lib/Horde/Service/Vimeo.php @@ -1,8 +1,4 @@ _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 diff --git a/framework/Service_Vimeo/lib/Horde/Service/Vimeo/Simple.php b/framework/Service_Vimeo/lib/Horde/Service/Vimeo/Simple.php index eb12390cb..853e767bb 100644 --- a/framework/Service_Vimeo/lib/Horde/Service/Vimeo/Simple.php +++ b/framework/Service_Vimeo/lib/Horde/Service/Vimeo/Simple.php @@ -12,33 +12,6 @@ */ 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'; @@ -60,51 +33,49 @@ class Horde_Service_Vimeo_Request { '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 } /** @@ -112,9 +83,6 @@ class Horde_Service_Vimeo_Request { * 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. @@ -135,9 +103,9 @@ class Horde_Service_Vimeo_Request { // 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); } @@ -146,12 +114,10 @@ class Horde_Service_Vimeo_Request { // 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; @@ -160,13 +126,13 @@ class Horde_Service_Vimeo_Request { 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); } @@ -174,17 +140,16 @@ class Horde_Service_Vimeo_Request { } } - $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;