Author: xavier
Date: 2010-05-19 23:52:25 +0200 (Wed, 19 May 2010)
New Revision: 29544

Modified:
   
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrlTable.class.php
Log:
[sfDoctrineShortUrlPlugin]: fixed short urls generation, and documented it.

Modified: 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrlTable.class.php
===================================================================
--- 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrlTable.class.php
   2010-05-19 21:41:27 UTC (rev 29543)
+++ 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrlTable.class.php
   2010-05-19 21:52:25 UTC (rev 29544)
@@ -11,42 +11,100 @@
 class PluginsfShortUrlTable extends Doctrine_Table
 {
   /**
-   * Generates a short url
+   * return true if this shorturl is already used
+   *
+   * @param $shorturl  a shorturl alias
+   * @return boolean   whether or not this shorturl alias is already in use
    */
-  public function acceptOrCreateSfShortUrl($shorturl = null)
+  public function alreadyExists($shorturl)
   {
+    $q = Doctrine_Query::create()
+      ->select('count(u.id) as total')
+      ->from('sfShortUrl u')
+      ->where('u.shorturl = ?', $shorturl);
+    $result = $q->execute(null, Doctrine::HYDRATE_NONE);
+    return ($result[0][0] > 0);
+  }
+
+  /**
+   * Creates a shorturl object, based on its long and short urls.
+   *
+   * @param $longurl   string  The longurl to shorten
+   * @param $shorturl  string  The alias to use
+   *
+   * @return object   An sfShortUrl object
+   */
+  public function createShortUrl($longurl, $shorturl)
+  {
+    $shorturl_object = new sfShortUrl();
+    $shorturl_object->setLongurl($longurl);
+    $shorturl_object->setShorturl($shorturl);
+    $shorturl_object->setIsEnabled(true);
+    $shorturl_object->setViewcount(0);
+    $shorturl_object->save();
+    return $shorturl_object;
+  }
+
+  /**
+   * Generates an unused short url alias
+   *
+   * @return string   an unused short url alias
+   */
+  public function generateAlias()
+  {
     $acceptable = false;
 
     while (!$acceptable)
     {
-      if ($shorturl)
-      {
-        $acceptable = (count($this->findByShorturl($shorturl)) == 0);
-      }
-
-      if (!$acceptable)
-      {
-        $shorturl = substr(md5(microtime()), 0, 5);
-      }
+      $alias = substr(md5(microtime()), 0, 5);
+      $acceptable = !$this->alreadyExists($alias);
     }
 
-    return $shorturl;
+    return $alias;
   }
 
+  /**
+   * Generate a short url, based on a long url and optionnally a desired alias
+   *
+   * If the alias is provided:
+   *  - either it exists and is associated to the same longurl, then this
+   *    alias is kept,
+   *  - either it exists but is not associated to the same longurl, then we
+   *    try to find if a shorturl already exist for this long url, or we
+   *    create a new one,
+   *  - either it doesn't exist and is then attached to the long url.
+   *
+   * If no alias is provided, we try to find if a shorturl already exist for
+   * this long url, or we create a new one.
+   *
+   * @param $longurl   string  The longurl to shorten
+   * @param $shorturl  string  Optionnal, a wished alias
+   *
+   * @return object   An sfShortUrl object
+   */
   public function generate($longurl, $shorturl = '')
   {
     if ($shorturl)
     {
-      // try to retrieve a corresponding couple (shorturl, longurl)
-      $objects = Doctrine_Query::create()
-        ->from('sfShortUrl u')
-        ->where('u.longurl = ?', $longurl)
-        ->andWhere('u.shorturl = ?', $shorturl)
-        ->execute();
-      $shorturl_object = isset($objects[0]) ? $objects[0] : null;
+      // search if this short url is already in use
+      $objects = $this->findByShorturl($shorturl);
+
+      if (isset($objects[0]))
+      {
+        // if it is the case, is it the same long url ?
+        if ($longurl == $objects[0]->getLongurl())
+        {
+          $shorturl_object = $objects[0];
+        }
+      }
+      else
+      {
+        // create new short url
+        $shorturl_object = $this->createShortUrl($longurl, $shorturl);
+      }
     }
 
-    if ('' == $shorturl || !isset($shorturl_object) || !$shorturl_object)
+    if (!isset($shorturl_object) || !$shorturl_object)
     {
       // try to retrieve a corresponding url within the public ones
       $shorturl_object = $this->findOneByLongurl($longurl);
@@ -55,13 +113,7 @@
     if (!isset($shorturl_object) || !$shorturl_object)
     {
       // no acceptable object => create one
-      $shorturl = $this->acceptOrCreateSfShortUrl($shorturl);
-      $shorturl_object = new sfShortUrl();
-      $shorturl_object->setLongurl($longurl);
-      $shorturl_object->setShorturl($shorturl);
-      $shorturl_object->setIsEnabled(true);
-      $shorturl_object->setViewcount(0);
-      $shorturl_object->save();
+      $shorturl_object = $this->createShortUrl($longurl, 
$this->generateAlias());
     }
 
     return $shorturl_object;

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to