Current file: /Users/bill/git/HTTP_OAuth/HTTP/OAuth/Store/Consumer/CacheLite.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 10 / 10 CRAP
100.00%100.00%
100.00% 37 / 37
 
HTTP_OAuth_Store_Consumer_CacheLite
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 10 / 10
100.00%100.00%
100.00% 37 / 37
 __construct(array $options = array()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 3 / 3
 setRequestToken($token, $tokenSecret, $providerName, $sessionID)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 8 / 8
 getRequestToken($providerName, $sessionID)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 4 / 4
 getRequestTokenKey($providerName, $sessionID)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 getAccessToken($consumerUserID, $providerName)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 4 / 4
 setAccessToken(HTTP_OAuth_Store_Data $data)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 3 / 3
 removeAccessToken(HTTP_OAuth_Store_Data $data)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 3 / 3
 getAccessTokenKey($consumerUserID, $providerName)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 setOptions($key, $expire = null)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 6 / 6
 ensureDirectoryExists($dir)
100.00%100.00%
100.00% 1 / 1 2
100.00%100.00%
100.00% 4 / 4


       1                 : <?php                                                                                   
       2                 : /**                                                                                     
       3                 :  * HTTP_OAuth_Store_CacheLite                                                            
       4                 :  *                                                                                       
       5                 :  * PHP Version 5.0.0                                                                    
       6                 :  *                                                                                       
       7                 :  * @uses      HTTP_OAuth_Store_Consumer_Interface                                       
       8                 :  * @category  HTTP                                                                      
       9                 :  * @package   HTTP_OAuth                                                                
      10                 :  * @author    Bill Shupp <hostmaster@shupp.org>                                          
      11                 :  * @copyright 2010 Bill Shupp                                                           
      12                 :  * @license   http://www.opensource.org/licenses/bsd-license.php FreeBSD                
      13                 :  * @link      http://pear.php.net/http_oauth                                            
      14                 :  */                                                                                     
      15                 :                                                                                         
      16                 : require_once 'HTTP/OAuth/Store/Data.php';                                               
      17                 : require_once 'HTTP/OAuth/Store/Consumer/Interface.php';                                 
      18                 : require_once 'Cache/Lite.php';                                                          
      19                 :                                                                                         
      20                 : /**                                                                                     
      21                 :  * Cache_Lite driver for HTTP_OAuth_Store_Consumer_Interface                            
      22                 :  *                                                                                       
      23                 :  * @uses      HTTP_OAuth_Store_Consumer_Interface                                       
      24                 :  * @category  HTTP                                                                      
      25                 :  * @package   HTTP_OAuth                                                                
      26                 :  * @author    Bill Shupp <hostmaster@shupp.org>                                          
      27                 :  * @copyright 2010 Bill Shupp                                                           
      28                 :  * @license   http://www.opensource.org/licenses/bsd-license.php FreeBSD                
      29                 :  * @link      http://pear.php.net/http_oauth                                            
      30                 :  */                                                                                     
      31                 : class HTTP_OAuth_Store_Consumer_CacheLite implements HTTP_OAuth_Store_Consumer_Interface
      32                 : {                                                                                       
      33                 :     const TYPE_REQUEST           = 'requestTokens';                                     
      34                 :     const TYPE_ACCESS            = 'accessTokens';                                      
      35                 :     const REQUEST_TOKEN_LIFETIME = 300;                                                 
      36                 :                                                                                         
      37                 :     /**                                                                                 
      38                 :      * Instance of Cache_Lite                                                           
      39                 :      *                                                                                   
      40                 :      * @var Cache_Lite|null                                                             
      41                 :      */                                                                                 
      42                 :     protected $cache = null;                                                            
      43                 :                                                                                         
      44                 :     /**                                                                                 
      45                 :      * CacheLite options                                                                
      46                 :      *                                                                                   
      47                 :      * @var array                                                                       
      48                 :      * @see $defaultOptions                                                             
      49                 :      */                                                                                 
      50                 :     protected $options = array();                                                       
      51                 :                                                                                         
      52                 :     /**                                                                                 
      53                 :      * Default options for Cache_Lite                                                   
      54                 :      *                                                                                   
      55                 :      * @var array                                                                       
      56                 :      */                                                                                 
      57                 :     protected $defaultOptions = array(                                                  
      58                 :         'cacheDir'             => '/tmp',                                               
      59                 :         'lifeTime'             => 300,                                                  
      60                 :         'hashedDirectoryLevel' => 2                                                     
      61                 :     );                                                                                  
      62                 :                                                                                         
      63                 :                                                                                         
      64                 :     /**                                                                                 
      65                 :      * Instantiate Cache_Lite.  Allows for options to be passed to Cache_Lite.            
      66                 :      *                                                                                   
      67                 :      * @param array $options Options for Cache_Lite constructor                         
      68                 :      *                                                                                   
      69                 :      * @return void                                                                     
      70                 :      */                                                                                 
      71                 :     public function __construct(array $options = array())                               
      72                 :     {                                                                                   
      73               2 :         $this->options = array_merge($this->defaultOptions, $options);                  
      74               2 :         $this->cache   = new Cache_Lite($this->options);                                
      75               2 :     }                                                                                   
      76                 :                                                                                         
      77                 :     /**                                                                                 
      78                 :      * Sets a request token                                                             
      79                 :      *                                                                                   
      80                 :      * @param string $token        The request token                                    
      81                 :      * @param string $tokenSecret  The request token secret                             
      82                 :      * @param string $providerName The name of the provider (i.e. 'twitter')            
      83                 :      * @param string $sessionID    A string representing this user's session            
      84                 :      *                                                                                   
      85                 :      * @return true on success, false or PEAR_Error on failure                          
      86                 :      */                                                                                 
      87                 :     public function setRequestToken($token, $tokenSecret, $providerName, $sessionID)    
      88                 :     {                                                                                   
      89               1 :         $this->setOptions(self::TYPE_REQUEST, self::REQUEST_TOKEN_LIFETIME);            
      90                 :         $data = array(                                                                  
      91               1 :             'token'        => $token,                                                   
      92               1 :             'tokenSecret'  => $tokenSecret,                                             
      93               1 :             'providerName' => $providerName,                                            
      94                 :             'sessionID'    => $sessionID                                                
      95               1 :         );                                                                              
      96                 :                                                                                         
      97               1 :         return $this->cache->save(serialize($data),                                     
      98               1 :                                   $this->getRequestTokenKey($providerName,              
      99               1 :                                                             $sessionID));               
     100                 :     }                                                                                   
     101                 :                                                                                         
     102                 :     /**                                                                                 
     103                 :      * Gets a request token as an array of the token, tokenSecret, providerName,        
     104                 :      * and sessionID (array key names)                                                  
     105                 :      *                                                                                   
     106                 :      * @param string $providerName The provider name (i.e. 'twitter')                   
     107                 :      * @param string $sessionID    A string representing this user's session            
     108                 :      *                                                                                   
     109                 :      * @return array on success, false on failure                                       
     110                 :      */                                                                                 
     111                 :     public function getRequestToken($providerName, $sessionID)                          
     112                 :     {                                                                                   
     113               1 :         $this->setOptions(self::TYPE_REQUEST, self::REQUEST_TOKEN_LIFETIME);            
     114               1 :         $result = $this->cache->get($this->getRequestTokenKey($providerName,            
     115               1 :                                                               $sessionID));             
     116               1 :         return unserialize($result);                                                    
     117                 :     }                                                                                   
     118                 :                                                                                         
     119                 :     /**                                                                                 
     120                 :      * Gets a cache key for request tokens.  It's an md5 hash of the provider name      
     121                 :      * and sessionID                                                                    
     122                 :      *                                                                                   
     123                 :      * @param string $providerName The provider name (i.e. 'twitter')                   
     124                 :      * @param string $sessionID    A string representing this user's session            
     125                 :      *                                                                                   
     126                 :      * @return string                                                                   
     127                 :      */                                                                                 
     128                 :     protected function getRequestTokenKey($providerName, $sessionID)                    
     129                 :     {                                                                                   
     130               1 :         return md5($providerName . ':' . $sessionID);                                   
     131                 :     }                                                                                   
     132                 :                                                                                         
     133                 :     /**                                                                                 
     134                 :      * Gets access token data in the form of an HTTP_OAuth_Store_Data object            
     135                 :      *                                                                                   
     136                 :      * @param string $consumerUserID The end user's ID at the consumer                  
     137                 :      * @param string $providerName   The provider name (i.e. 'twitter')                 
     138                 :      *                                                                                   
     139                 :      * @return HTTP_OAuth_Store_Data                                                    
     140                 :      */                                                                                 
     141                 :     public function getAccessToken($consumerUserID, $providerName)                      
     142                 :     {                                                                                   
     143               1 :         $this->setOptions(self::TYPE_ACCESS);                                           
     144               1 :         $result = $this->cache->get($this->getAccessTokenKey($consumerUserID,           
     145               1 :                                                              $providerName));           
     146               1 :         return unserialize($result);                                                    
     147                 :     }                                                                                   
     148                 :                                                                                         
     149                 :     /**                                                                                 
     150                 :      * Sets access token data from an HTTP_OAuth_Store_Data object                      
     151                 :      *                                                                                   
     152                 :      * @param HTTP_OAuth_Store_Data $data The access token data                         
     153                 :      *                                                                                   
     154                 :      * @return bool true on success, false or PEAR_Error on failure                     
     155                 :      */                                                                                 
     156                 :     public function setAccessToken(HTTP_OAuth_Store_Data $data)                         
     157                 :     {                                                                                   
     158               1 :         $this->setOptions(self::TYPE_ACCESS);                                           
     159                 :                                                                                         
     160               1 :         $key = $this->getAccessTokenKey($data->consumerUserID, $data->providerName);    
     161               1 :         return $this->cache->save(serialize($data), $key);                              
     162                 :     }                                                                                   
     163                 :                                                                                         
     164                 :     /**                                                                                 
     165                 :      * Removes an access token                                                          
     166                 :      *                                                                                   
     167                 :      * @param HTTP_OAuth_Store_Data $data The access token data                         
     168                 :      *                                                                                   
     169                 :      * @return bool true on success, false or PEAR_Error on failure                     
     170                 :      */                                                                                 
     171                 :     public function removeAccessToken(HTTP_OAuth_Store_Data $data)                      
     172                 :     {                                                                                   
     173               1 :         $this->setOptions(self::TYPE_ACCESS);                                           
     174                 :                                                                                         
     175               1 :         $key = $this->getAccessTokenKey($data->consumerUserID, $data->providerName);    
     176               1 :         return $this->cache->remove($key);                                              
     177                 :     }                                                                                   
     178                 :                                                                                         
     179                 :     /**                                                                                 
     180                 :      * Gets an access token key for storage, based on the consumer user ID and the      
     181                 :      * provider name                                                                    
     182                 :      *                                                                                   
     183                 :      * @param string $consumerUserID The end user's ID at the consumer                  
     184                 :      * @param string $providerName   The provider name (i.e. 'twitter')                 
     185                 :      *                                                                                   
     186                 :      * @return void                                                                     
     187                 :      */                                                                                 
     188                 :     protected function getAccessTokenKey($consumerUserID, $providerName)                
     189                 :     {                                                                                   
     190               1 :         return md5($consumerUserID . ':' . $providerName);                              
     191                 :     }                                                                                   
     192                 :                                                                                         
     193                 :     /**                                                                                 
     194                 :      * Sets options for Cache_Lite based on the needs of the current method.            
     195                 :      * Options set include the subdirectory to be used, and the expiration.             
     196                 :      *                                                                                   
     197                 :      * @param string $key    The sub-directory of the cacheDir                          
     198                 :      * @param string $expire The cache lifetime (expire) to be used                     
     199                 :      *                                                                                   
     200                 :      * @return void                                                                     
     201                 :      */                                                                                 
     202                 :     protected function setOptions($key, $expire = null)                                 
     203                 :     {                                                                                   
     204               2 :         $cacheDir  = $this->options['cacheDir'] . '/oauth/';                            
     205               2 :         $cacheDir .= rtrim($key, '/') . '/';                                            
     206                 :                                                                                         
     207               2 :         $this->ensureDirectoryExists($cacheDir);                                        
     208                 :                                                                                         
     209               2 :         $this->cache->setOption('cacheDir', $cacheDir);                                 
     210               2 :         $this->cache->setOption('lifeTime', $expire);                                   
     211               2 :     }                                                                                   
     212                 :                                                                                         
     213                 :     /**                                                                                 
     214                 :      * Make sure the given sub directory exists.  If not, create it.                    
     215                 :      *                                                                                   
     216                 :      * @param string $dir The full path to the sub director we plan to write to         
     217                 :      *                                                                                   
     218                 :      * @return void                                                                     
     219                 :      */                                                                                 
     220                 :     protected function ensureDirectoryExists($dir)                                      
     221                 :     {                                                                                   
     222               2 :         if (!file_exists($dir)) {                                                       
     223               2 :             mkdir($dir, 0777, true);                                                    
     224               2 :         }                                                                               
     225               2 :     }                                                                                   
     226                 : }                                                                                       
     227                 : ?>                                                                                      

Generated by PHP_CodeCoverage 1.0.2 using PHP 5.3.3 and PHPUnit 3.5.6 at Tue Dec 28 21:42:13 PST 2010.