Horde_Token no longer uses PEAR_Errors
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 27 Jul 2009 18:27:51 +0000 (12:27 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 28 Jul 2009 04:09:50 +0000 (22:09 -0600)
framework/Token/lib/Horde/Token.php
framework/Token/lib/Horde/Token/File.php
framework/Token/lib/Horde/Token/Sql.php
framework/Token/package.xml

index cb87a10..ebc486f 100644 (file)
@@ -39,8 +39,7 @@ class Horde_Token
      * @param array $params  A hash containing any additional configuration or
      *                       connection parameters a subclass might need.
      *
-     * @return Horde_Token  The newly created concrete Horde_Token instance, or
-     *                      false an error.
+     * @return Horde_Token  The newly created concrete Horde_Token instance.
      */
     static public function factory($driver, $params = array())
     {
@@ -83,8 +82,7 @@ class Horde_Token
      * @param array $params  A hash containing any additional configuration or
      *                       connection parameters a subclass might need.
      *
-     * @return Horde_Token  The concrete Horde_Token reference, or false on
-     *                      error.
+     * @return Horde_Token  The concrete Horde_Token reference.
      */
     static public function singleton($driver, $params = array())
     {
@@ -137,24 +135,23 @@ class Horde_Token
      * @param string $token  The value of the token to check.
      *
      * @return boolean  True if the token has not been used, false otherwise.
+     * @throws Horde_Exception
      */
     public function verify($token)
     {
         $this->purge();
-        $exists = $this->exists($token);
-        if (is_a($exists, 'PEAR_Error')) {
-            return $exists;
-        } elseif ($exists) {
+        if ($this->exists($token)) {
             return false;
-        } else {
-            return $this->add($token);
         }
+        return $this->add($token);
     }
 
     /**
      * This is an abstract method that should be overridden by a
      * subclass implementation. The base implementation allows all
      * token values.
+     *
+     * @throws Horde_Exception
      */
     public function exists()
     {
@@ -165,20 +162,22 @@ class Horde_Token
      * This is an abstract method that should be overridden by a
      * subclass implementation. The base implementation allows all
      * token values.
+     *
+     * @throws Horde_Exception
      */
     public function add()
     {
-        return true;
     }
 
     /**
      * This is an abstract method that should be overridden by a
      * subclass implementation. The base implementation allows all
      * token values.
+     *
+     * @throws Horde_Exception
      */
     public function purge()
     {
-        return true;
     }
 
 }
index 6e75967..7d55e97 100644 (file)
@@ -62,42 +62,41 @@ class Horde_Token_File extends Horde_Token
     /**
      * Deletes all expired connection id's from the SQL server.
      *
-     * @return boolean  True on success, a PEAR_Error object on failure.
+     * @throws Horde_Exception
      */
     public function purge()
     {
         // Make sure we have no open file descriptors before unlinking
         // files.
         if (!$this->_disconnect()) {
-            return PEAR::raiseError('Unable to close file descriptors');
+            throw new Horde_Exception('Unable to close file descriptors');
         }
 
         /* Build stub file list. */
         if (!($dir = opendir($this->_params['token_dir']))) {
-            return PEAR::raiseError('Unable to open token directory');
+            throw new Horde_Exception('Unable to open token directory');
         }
 
         /* Find expired stub files */
         while (($dirEntry = readdir($dir)) != '') {
-            if (preg_match('|^conn_\w{8}$|', $dirEntry) && (time() - filemtime($this->_params['token_dir'] . '/' . $dirEntry) >= $this->_params['timeout'])) {
-                if (!@unlink($this->_params['token_dir'] . '/' . $dirEntry)) {
-                    return PEAR::raiseError('Unable to purge token file.');
-                }
+            if (preg_match('|^conn_\w{8}$|', $dirEntry) && (time() - filemtime($this->_params['token_dir'] . '/' . $dirEntry) >= $this->_params['timeout']) &&
+                !@unlink($this->_params['token_dir'] . '/' . $dirEntry)) {
+                throw new Horde_Exception('Unable to purge token file.');
             }
         }
 
         closedir($dir);
-        return true;
     }
 
     /**
      * TODO
+     *
+     * @return boolean  TODO
+     * @throws Horde_Exception
      */
     public function exists($tokenID)
     {
-        if (is_a(($result = $this->_connect($tokenID)), 'PEAR_Error')) {
-            return $result;
-        }
+        $this->_connect($tokenID);
 
         /* Find already used IDs. */
         $fileContents = file($this->_params['token_dir'] . '/conn_' . $this->encodeRemoteAddress());
@@ -115,43 +114,40 @@ class Horde_Token_File extends Horde_Token
 
     /**
      * TODO
+     *
+     * @throws Horde_Exception
      */
     public function add($tokenID)
     {
-        if (is_a(($result = $this->_connect($tokenID)), 'PEAR_Error')) {
-            return $result;
-        }
+        $this->_connect($tokenID);
 
         /* Write the entry. */
         fwrite($this->_fd, "$tokenID\n");
 
         /* Return an error if the update fails, too. */
         if (!$this->_disconnect()) {
-            return PEAR::raiseError('Failed to close token file cleanly.');
+            throw new Horde_Exception('Failed to close token file cleanly.');
         }
-
-        return true;
     }
 
     /**
      * Opens a file descriptor to a new or existing file.
      *
-     * @return boolean  True on success, a PEAR_Error object on failure.
+     * @throws Horde_Exception
      */
     protected function _connect($tokenID)
     {
-        if (!$this->_connected) {
-
-            // Open a file descriptor to the token stub file.
-            $this->_fd = @fopen($this->_params['token_dir'] . '/conn_' . $this->encodeRemoteAddress(), 'a');
-            if (!$this->_fd) {
-                return PEAR::raiseError('Failed to open token file.');
-            }
+        if ($this->_connected) {
+            return;
+        }
 
-            $this->_connected = true;
+        // Open a file descriptor to the token stub file.
+        $this->_fd = @fopen($this->_params['token_dir'] . '/conn_' . $this->encodeRemoteAddress(), 'a');
+        if (!$this->_fd) {
+            throw new Horde_Exception('Failed to open token file.');
         }
 
-        return true;
+        $this->_connected = true;
     }
 
     /**
index 6d94ad4..b6d039c 100644 (file)
@@ -90,13 +90,11 @@ class Horde_Token_Sql extends Horde_Token
     /**
      * Deletes all expired connection id's from the SQL server.
      *
-     * @return boolean  True on success, a PEAR_Error object on failure.
+     * @throws Horde_Exception
      */
     public function purge()
     {
-        if (is_a(($result = $this->_connect()), 'PEAR_Error')) {
-            return $result;
-        }
+        $this->_connect();
 
         /* Build SQL query. */
         $query = 'DELETE FROM ' . $this->_params['table']
@@ -106,20 +104,22 @@ class Horde_Token_Sql extends Horde_Token
 
         /* Return an error if the update fails. */
         $result = $this->_write_db->query($query, $values);
-        if (is_a($result, 'PEAR_Error')) {
+        if ($result instanceof PEAR_Error) {
             Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return $result;
+            throw new Horde_Exception($result);
         }
-
-        return true;
     }
 
     /**
      * TODO
+     *
+     * @return boolean  TODO
      */
     public function exists($tokenID)
     {
-        if (is_a(($result = $this->_connect()), 'PEAR_Error')) {
+        try {
+            $this->_connect();
+        } catch (Horde_Exception $e) {
             return false;
         }
 
@@ -130,7 +130,7 @@ class Horde_Token_Sql extends Horde_Token
         $values = array($this->encodeRemoteAddress(), $tokenID);
 
         $result = $this->_db->getOne($query, $values);
-        if (is_a($result, 'PEAR_Error')) {
+        if ($result instanceof PEAR_Error) {
             Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
             return false;
         } else {
@@ -140,12 +140,12 @@ class Horde_Token_Sql extends Horde_Token
 
     /**
      * TODO
+     *
+     * @throws Horde_Exception
      */
     public function add($tokenID)
     {
-        if (is_a(($result = $this->_connect()), 'PEAR_Error')) {
-            return $result;
-        }
+        $this->_connect();
 
         /* Build SQL query. */
         $query = 'INSERT INTO ' . $this->_params['table']
@@ -155,29 +155,24 @@ class Horde_Token_Sql extends Horde_Token
         $values = array($this->encodeRemoteAddress(), $tokenID, time());
 
         $result = $this->_write_db->query($query, $values);
-        if (is_a($result, 'PEAR_Error')) {
+        if ($result instanceof PEAR_Error) {
             Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return $result;
+            throw new Horde_Exception($result);
         }
-
-        return true;
     }
 
     /**
      * Opens a connection to the SQL server.
      *
-     * @return boolean  True on success, a PEAR_Error object on failure.
+     * @throws Horde_Exception
      */
     protected function _connect()
     {
         if ($this->_connected) {
-            return true;
+            return;
         }
 
-        $result = Horde_Util::assertDriverConfig($this->_params, array('phptype'), 'token SQL', array('driver' => 'token'));
-        if (is_a($result, 'PEAR_Error')) {
-            return $result;
-        }
+        Horde_Util::assertDriverConfig($this->_params, array('phptype'), 'token SQL', array('driver' => 'token'));
 
         if (!isset($this->_params['database'])) {
             $this->_params['database'] = '';
@@ -199,8 +194,8 @@ class Horde_Token_Sql extends Horde_Token
         $this->_write_db = DB::connect($this->_params,
                                  array('persistent' => !empty($this->_params['persistent']),
                                        'ssl' => !empty($this->_params['ssl'])));
-        if (is_a($this->_write_db, 'PEAR_Error')) {
-            return $this->_write_db;
+        if ($this->_write_db instanceof PEAR_Error) {
+            throw new Horde_Exception($this->_write_db);
         }
 
         // Set DB portability options.
@@ -208,8 +203,10 @@ class Horde_Token_Sql extends Horde_Token
         case 'mssql':
             $this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
             break;
+
         default:
             $this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
+            break;
         }
 
         /* Check if we need to set up the read DB connection
@@ -219,8 +216,8 @@ class Horde_Token_Sql extends Horde_Token
             $this->_db = DB::connect($params,
                                      array('persistent' => !empty($params['persistent']),
                                            'ssl' => !empty($params['ssl'])));
-            if (is_a($this->_db, 'PEAR_Error')) {
-                return $this->_db;
+            if ($this->_db instanceof PEAR_Error) {
+                throw new Horde_Exception($this->_db);
             }
 
             // Set DB portability options.
@@ -228,8 +225,10 @@ class Horde_Token_Sql extends Horde_Token
             case 'mssql':
                 $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
                 break;
+
             default:
                 $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
+                break;
             }
 
         } else {
@@ -238,7 +237,6 @@ class Horde_Token_Sql extends Horde_Token
         }
 
         $this->_connected = true;
-        return true;
     }
 
 }
index 38b82fe..2fbc66c 100644 (file)
@@ -26,7 +26,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Initial Horde 4 package.
+ <notes>* Use exceptions, not PEAR_Errors.
+ * Initial Horde 4 package.
  </notes>
  <contents>
   <dir name="/">
@@ -50,6 +51,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <min>1.5.0</min>
    </pearinstaller>
    <package>
+    <name>Exception</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Util</name>
     <channel>pear.horde.org</channel>
    </package>