Skip to content

Commit

Permalink
Updated Common and Bitly to conform to PHPCS.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Stump committed Apr 28, 2009
1 parent 88ef647 commit 1876963
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Services/ShortURL.php
Expand Up @@ -5,7 +5,7 @@
*
* PHP version 5.2.0+
*
* LICENSE: This source file is subject to the New BSD license that is
* LICENSE: This source file is subject to the New BSD license that is
* available through the world-wide-web at the following URI:
* http://www.opensource.org/licenses/bsd-license.php. If you did not receive
* a copy of the New BSD License and are unable to obtain it through the web,
Expand Down
32 changes: 29 additions & 3 deletions Services/ShortURL/Bitly.php
Expand Up @@ -47,8 +47,17 @@ class Services_ShortURL_Bitly
*
* @var string $api The URL for the API
*/
private $api = 'http://api.bit.ly';
protected $api = 'http://api.bit.ly';

/**
* Constructor
*
* @param array $options The service options array
* @param object $req The request object
*
* @throws {@link Services_ShortURL_Exception_InvalidOptions}
* @return void
*/
public function __construct(array $options = array(),
HTTP_Request2 $req = null)
{
Expand All @@ -67,6 +76,15 @@ public function __construct(array $options = array(),
}
}

/**
* Shorten a URL using {@link http://bit.ly}
*
* @param string $url The URL to shorten
*
* @throws {@link Services_ShortURL_Exception_CouldNotShorten}
* @return string The shortened URL
* @see Services_ShortURL_Bitly::sendRequest()
*/
public function shorten($url)
{
$params = array(
Expand All @@ -87,14 +105,22 @@ public function shorten($url)
return (string)$xml->results->nodeKeyVal->shortUrl;
}

private function sendRequest($url)
/**
* Send a request to {@link http://bit.ly}
*
* @param string $url The URL to send the request to
*
* @throws {@link Services_ShortURL_Exception_CouldNotShorten}
* @return object Instance of SimpleXMLElement
*/
protected function sendRequest($url)
{
$this->req->setUrl($url);
$this->req->setMethod('GET');

$result = $this->req->send();
if ($result->getStatus() != 200) {
throw new Services_ShortURL_Exception_CouldNotExpand(
throw new Services_ShortURL_Exception_CouldNotShorten(
'Non-300 code returned', $result->getStatus()
);
}
Expand Down
55 changes: 52 additions & 3 deletions Services/ShortURL/Common.php
Expand Up @@ -35,9 +35,35 @@
*/
abstract class Services_ShortURL_Common
{
/**
* Service options
*
* Some services require an API key, username/password, or other
* non-standard information. Those options are set on a per-service
* basis and passed to the constructor.
*
* @var array $options Service options
* @see Services_ShortURL::$options
* @see Services_ShortURL::setServiceOptions()
*/
protected $options = array();

/**
* Instance of {@link HTTP_Request2}
*
* @var object $req Instance of {@link HTTP_Request2}
* @see Services_ShortURL_Common::__construct()
*/
protected $req = null;

/**
* Constructor
*
* @param array $options Service options
* @param object $req Provide your own {@link HTTP_Request2} instance
*
* @return void
*/
public function __construct(array $options = array(),
HTTP_Request2 $req = null)
{
Expand All @@ -46,14 +72,24 @@ public function __construct(array $options = array(),
} else {
$this->req = new HTTP_Request2();
$this->req->setAdapter('Curl');
$this->req->setHeader(
'User-Agent', get_class($this) . ' @version@'
);
$this->req->setHeader('User-Agent', get_class($this) . ' @version@');
}

$this->options = $options;
}

/**
* Acceptor pattern
*
* By default, {@link Services_ShortURL} will create a cURL version of
* {@link HTTP_Request2}. If you need to override this you can use the
* accept method or pass an instance into the constructor.
*
* @param object $object Instance of {@link HTTP_Request2}
*
* @throws InvalidArgumentException on invalid object
* @return void
*/
public function accept($object)
{
if (!is_object($object)) {
Expand All @@ -69,6 +105,19 @@ public function accept($object)
}
}

/**
* Default expand method
*
* All of the URL shortening services, for the most part, do a 301 redirect
* using the Location header. Rather than implement this over and over we
* provide it here and assume others who need non-normal expansion will
* override this method.
*
* @param string $url The URL to expand
*
* @throws {@link Services_ShortURL_Exception_CouldNotExpand} on non-300's.
* @return string $url The expanded URL
*/
public function expand($url)
{
$this->req->setUrl($url);
Expand Down

0 comments on commit 1876963

Please sign in to comment.