Permalink
Browse files

Minor cleanup and better documentation

  • Loading branch information...
1 parent ddad987 commit eb1b5cfa5a4a6345f1abb346fc70da1d8b092f1a @epochblue committed Feb 26, 2012
Showing with 70 additions and 18 deletions.
  1. +64 −12 services/{UrlService.php → ShorteningService.php}
  2. +6 −6 web/index.php
@@ -1,21 +1,27 @@
<?php
-use Symfony\Component\HttpFoundation\Response;
-
/**
* Wraps up the shortening of URLs into a simpler service interface.
*
* @author Bill Israel <bill.israel@gmail.com>
*/
-class UrlService {
+
+use Symfony\Component\HttpFoundation\Response;
+
+class ShorteningService {
/** Constructor; gets the app injected */
public function __construct($app) {
$this->_app = $app;
}
- //
- // Public API
- //
+ /**
+ * Shortens the given URL, or returns the short code
+ * if this URL has already been shortened.
+ *
+ * @param string $url The URL to shorten
+ *
+ * @return Response The HTTP response representing success or failure
+ */
public function shorten($url) {
$url = $this->clean_url($url);
if ($this->is_url($url)) {
@@ -30,6 +36,14 @@ public function shorten($url) {
}
+ /**
+ * Looks up the URL based on the given code.
+ *
+ * @param string $code The short code to look up
+ * @param int $base The base to use for short code conversion
+ *
+ * @return Response The HTTP Response representing success or failure.
+ */
public function lookup($code, $base) {
$id = intval($code, $base);
if ($url = $this->find_by_id($id)) {
@@ -40,36 +54,67 @@ public function lookup($code, $base) {
}
- //
- // Private Helpers
- //
+ /**
+ * Looks for the URL in the database.
+ *
+ * @param string $url The URL to find
+ *
+ * @return mixed The found row(s) as an associative array, or false on failure
+ */
private function find_by_url($url) {
$sel = $this->_app['db']->prepare("SELECT id FROM urls WHERE url = :url");
$sel->execute(array(':url' => $url));
return $sel->fetch(PDO::FETCH_ASSOC);
}
+ /**
+ * Returns the row for a given ID.
+ *
+ * @param int $id The id to look up
+ *
+ * @return mixed The row that matches the ID, or false on failure
+ */
private function find_by_id($id) {
$sel = $this->_app['db']->prepare("SELECT url FROM urls WHERE id = :id");
$sel->execute(array(':id' => $id));
return $sel->fetch(PDO::FETCH_ASSOC);
}
+ /**
+ * Creates a new record in the DB for the URL.
+ *
+ * @param string $url The URL to create the record for
+ *
+ * @return integer The id of the newly created row
+ */
private function create($url) {
$ins = $this->_app['db']->prepare("INSERT INTO urls (url) VALUES (:url)");
$ins->execute(array(':url' => $url));
return intval($this->_app['db']->lastInsertId());
}
+ /**
+ * Returns the short code for the given id.
+ *
+ * @param integer $id The interger to shorten
+ *
+ * @return string The converted id
+ */
private function get_short_code($id) {
return base_convert($id, 10, $this->_app['config']['convert_base']);
}
- // Try to help, and assume the user might've forgotten to add the protocol.
- // And remove any trailing slashes so we don't store example.com and example.com/
+ /**
+ * Try to help, and assume the user might've forgotten to add the protocol,
+ * and remove any trailing slashes so we don't store example.com and example.com/
+ *
+ * @param string $url The URL to clean up
+ *
+ * @return string The cleaned-up URL
+ */
private function clean_url($url) {
$url = rtrim($url, '/');
if (!preg_match('/^https?:\/\//', $url)) {
@@ -78,10 +123,17 @@ private function clean_url($url) {
return $url;
}
+ /**
+ * Determines whether the string given is a URL.
+ *
+ * @param string $url The URL to test.
+ *
+ * @return bool True if the string is a URL, false otherwise.
+ */
private function is_url($url) {
// This slightly modified URL regex originally came from:
// http://www.blog.highub.com/regular-expression/php-regex-regular-expression/php-regex-validating-a-url/
$url_re = '/^((http|https):?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})?=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
- return preg_match($url_re, $url);
+ return (bool) preg_match($url_re, $url);
}
}
View
@@ -8,17 +8,17 @@
use Symfony\Component\HttpFoundation\Response;
require_once(__DIR__ . '/../vendor/Silex/silex.phar');
-require_once(__DIR__ . '/../services/UrlService.php');
+require_once(__DIR__ . '/../services/ShorteningService.php');
// Create the application
$app = new Silex\Application();
// Configure the appliation
require_once(__DIR__ . '/../config/config.php');
-// Setup the UrlService
-$app['url_service'] = function($app) {
- return new UrlService($app);
+// Setup the ShorteningService
+$app['service'] = function($app) {
+ return new ShorteningService($app);
};
@@ -37,7 +37,7 @@
* Stores the URL and returns the short code, which is never explicitly stored.
*/
$app->post('/', function(Request $request) use ($app) {
- return $app['url_service']->shorten($request->get('url'));
+ return $app['service']->shorten($request->get('url'));
});
/**
@@ -46,7 +46,7 @@
* Retrieves and redirects the user to the URL identified by {code}.
*/
$app->get('/{code}', function($code) use ($app) {
- return $app['url_service']->lookup($code, $app['config']['convert_base']);
+ return $app['service']->lookup($code, $app['config']['convert_base']);
});
// All done. Now go do stuff.

0 comments on commit eb1b5cf

Please sign in to comment.