Current file: /Users/sxs/Sites/kapit/trunk/lib/Api/Kwick/Request.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
0.00%0.00%
0.00% 0 / 1
82.35%82.35%
82.35% 14 / 17
79.31%79.31%
79.31% 69 / 87
 
Api_Kwick_Request
0.00%0.00%
0.00% 0 / 1
82.35%82.35%
82.35% 14 / 17
79.31%79.31%
79.31% 69 / 87
 public function __construct(Api_Kwick_Config $config)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 9 / 9
 public function setBaseUri($uri)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 2 / 2
 public function getBaseUri()
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
 public function setApiKey($key)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 2 / 2
 public function getApiKey()
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
 public function setFiles(array $files)
0.00%0.00%
0.00% 0 / 1
0.00%0.00%
0.00% 0 / 2
 public function setSecret($secret)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 2 / 2
 public function getSecret()
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
 public function setParameter($name, $value)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 3 / 3
 public function setMethod($method)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 3 / 3
 public function setService($service)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 3 / 3
 public function send(HttpRequest $httpRequest = null)
0.00%0.00%
0.00% 0 / 1
61.29%61.29%
61.29% 19 / 31
 public function generateUrl()
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
 public function generateUri()
0.00%0.00%
0.00% 0 / 1
63.64%63.64%
63.64% 7 / 11
 public function getQualifiedMethod()
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
 public function generateParamString()
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 11 / 11
 public function generateSignature()
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 3 / 3


       1                 : <?php                                                                                            
       2                 : /**                                                                                              
       3                 :  * LICENSE                                                                                       
       4                 :  *                                                                                               
       5                 :  * "THE BEER-WARE LICENSE" (Revision 42):                                                        
       6                 :  * "Sven Strittmatter" <ich@weltraumschaf.de> wrote this file.                                   
       7                 :  * As long as you retain this notice you can do whatever you want with                           
       8                 :  * this stuff. If we meet some day, and you think this stuff is worth it,                        
       9                 :  * you can buy me a beer in return.                                                              
      10                 :  *                                                                                               
      11                 :  * @author    Sven Strittmatter <ich@weltraumschaf.de>                                           
      12                 :  * @copyright Copyright (c) 2010, Sven Strittmatter.                                             
      13                 :  * @version   0.2.2                                                                              
      14                 :  * @license   http://www.weltraumschaf.de/the-beer-ware-license.txt                              
      15                 :  */                                                                                              
      16                 :                                                                                                  
      17                 : /**                                                                                              
      18                 :  * @see Api_Log                                                                                  
      19                 :  */                                                                                              
      20                 : require_once 'Api/Log.php';                                                                      
      21                 :                                                                                                  
      22                 : /**                                                                                              
      23                 :  *                                                                                               
      24                 :  */                                                                                              
      25                 : class Api_Kwick_Request {                                                                        
      26                 :     const VERSION_20 = '2.0';                                                                    
      27                 :                                                                                                  
      28                 :     const DEFAULT_METHOD  = 'getAPIVersion';                                                     
      29                 :     const DEFAULT_SERVICE = 'application';                                                       
      30                 :                                                                                                  
      31                 :     private $baseUri;                                                                            
      32                 :     private $apiKey;                                                                             
      33                 :     private $secret;                                                                             
      34                 :     private $version;                                                                            
      35                 :     private $method;                                                                             
      36                 :     private $service;                                                                            
      37                 :     private $parameters;                                                                         
      38                 :     private $files;                                                                              
      39                 :                                                                                                  
      40                 :     public function  __construct(Api_Kwick_Config $config) {                                     
      41              11 :         $this->setBaseUri($config->get('baseUri'));                                              
      42              11 :         $this->setApiKey($config->get('apiKey'));                                                
      43              11 :         $this->setSecret($config->get('secret'));                                                
      44              11 :         $this->version    = $config->get('version', self::VERSION_20);                           
      45              11 :         $this->service    = self::DEFAULT_SERVICE;                                               
      46              11 :         $this->method     = self::DEFAULT_METHOD;                                                
      47              11 :         $this->parameters = array();                                                             
      48              11 :         $this->files      = array();                                                             
      49              11 :     }                                                                                            
      50                 :                                                                                                  
      51                 :     public function setBaseUri($uri) {                                                           
      52               8 :         $this->baseUri = (string) $uri;                                                          
      53               8 :     }                                                                                            
      54                 :                                                                                                  
      55                 :     public function getBaseUri() {                                                               
      56               2 :         return $this->baseUri;                                                                   
      57                 :     }                                                                                            
      58                 :                                                                                                  
      59                 :     public function setApiKey($key) {                                                            
      60               8 :         $this->apiKey = (string) $key;                                                           
      61               8 :     }                                                                                            
      62                 :                                                                                                  
      63                 :     public function getApiKey() {                                                                
      64               2 :         return $this->apiKey;                                                                    
      65                 :     }                                                                                            
      66                 :                                                                                                  
      67                 :     public function setFiles(array $files) {                                                     
      68               0 :         $this->files = $files;                                                                   
      69               0 :     }                                                                                            
      70                 :                                                                                                  
      71                 :     public function setSecret($secret) {                                                         
      72               8 :         $this->secret = (string) $secret;                                                        
      73               8 :     }                                                                                            
      74                 :                                                                                                  
      75                 :     public function getSecret() {                                                                
      76               2 :         return $this->secret;                                                                    
      77                 :     }                                                                                            
      78                 :                                                                                                  
      79                 :     public function setParameter($name, $value) {                                                
      80               2 :         Api_Log::log('Set parameter: ' . $name . ' with: ' . $value);                            
      81               2 :         $this->parameters[$name] = $value;                                                       
      82               2 :     }                                                                                            
      83                 :                                                                                                  
      84                 :     public function setMethod($method) {                                                         
      85               1 :         Api_Log::log('Set method to: ' . $method);                                               
      86               1 :         $this->method = (string) $method;                                                        
      87               1 :     }                                                                                            
      88                 :                                                                                                  
      89                 :     public function setService($service) {                                                       
      90               1 :         Api_Log::log('Set service to: ' . $service);                                             
      91               1 :         $this->service = (string) $service;                                                      
      92               1 :     }                                                                                            
      93                 :                                                                                                  
      94                 :     public function send(HttpRequest $httpRequest = null) {                                      
      95               2 :         if (null === $httpRequest) {                                                             
      96               0 :             if (!class_exists('HttpRequest', false)) {                                           
      97               0 :                 throw new RuntimeException('Can not find class HttpRequest! Install pecl http.');
      98                 :             }                                                                                    
      99                 :                                                                                                  
     100               0 :             $httpRequest = new HttpRequest();                                                    
     101               0 :         }                                                                                        
     102                 :                                                                                                  
     103               2 :         $url = $this->generateUri();                                                             
     104                 :                                                                                                  
     105               2 :         if (!empty($this->files)) {                                                              
     106               0 :             foreach ($this->files as $fileName => $fileUpload) {                                 
     107               0 :                 $httpRequest->addPostFile($fileName, $fileUpload['tmp_name']);                   
     108               0 :             }                                                                                    
     109               0 :         }                                                                                        
     110                 :                                                                                                  
     111               2 :         Api_Log::log('Send request with url: ' . $url);                                          
     112               2 :         $httpRequest->setUrl($url);                                                              
     113               2 :         $httpRequest->setMethod(HttpRequest::METH_POST);                                         
     114               2 :         $start = microtime(true);                                                                
     115               2 :         $httpRequest->send();                                                                    
     116               2 :         $time  = microtime(true) - $start;                                                       
     117               2 :         Api_Log::log('Duration of api request: ' . round($time, 3) . 's');                       
     118                 :                                                                                                  
     119               2 :         if ($httpRequest->getResponseCode() < 500) {                                             
     120               2 :             switch ($httpRequest->getResponseCode()) {                                           
     121               2 :                 case 401:                                                                        
     122               0 :                     $message = 'Authroization required on URI: ' . $url;                         
     123               0 :                     throw new RuntimeException($message, 401);                                   
     124               2 :                 case 404:                                                                        
     125               1 :                     $message = 'Not found URI: ' . $url;                                         
     126               1 :                     throw new RuntimeException($message, 404);                                   
     127               1 :                 default:                                                                         
     128               1 :                     return $httpRequest->getResponseBody();                                      
     129               1 :             }                                                                                    
     130                 :         }                                                                                        
     131                 :                                                                                                  
     132               0 :         $message = 'Got a server error response on URI: ' . $url;                                
     133               0 :         throw new RuntimeException($message, $httpRequest->getResponseCode());                   
     134                 :     }                                                                                            
     135                 :                                                                                                  
     136                 :     /**                                                                                          
     137                 :      * @deprecated Please use generateUri() instead.                                             
     138                 :      * @return string                                                                            
     139                 :      */                                                                                          
     140                 :     public function generateUrl() {                                                              
     141               2 :         return $this->generateUri();                                                             
     142                 :     }                                                                                            
     143                 :                                                                                                  
     144                 :     /**                                                                                          
     145                 :      *                                                                                           
     146                 :      * @return string                                                                            
     147                 :      */                                                                                          
     148                 :     public function generateUri() {                                                              
     149               3 :         $url  = $this->baseUri . '?';                                                            
     150               3 :         $url .= 'api_key=' . $this->apiKey . '&';                                                
     151               3 :         $url .= 'v=' . $this->version . '&';                                                     
     152               3 :         $url .= 'method=' . $this->getQualifiedMethod() . '&';                                   
     153               3 :         $url .= 'sig=' . $this->generateSignature();                                             
     154                 :                                                                                                  
     155               3 :         if (!empty($this->parameters)) {                                                         
     156               0 :             foreach ($this->parameters as $name => $value) {                                     
     157               0 :                 $url .= '&' . $name . '=' . urlencode($value);                                   
     158               0 :             }                                                                                    
     159               0 :         }                                                                                        
     160                 :                                                                                                  
     161               3 :         return $url;                                                                             
     162                 :     }                                                                                            
     163                 :                                                                                                  
     164                 :     public function getQualifiedMethod() {                                                       
     165               6 :         return $this->service . '.' . $this->method;                                             
     166                 :     }                                                                                            
     167                 :                                                                                                  
     168                 :     public function generateParamString() {                                                      
     169               5 :         $parameters = array_merge($this->parameters, array(                                      
     170               5 :                 'api_key' => $this->apiKey,                                                      
     171               5 :                 'v'       => $this->version,                                                     
     172               5 :                 'method'  => $this->getQualifiedMethod()                                         
     173               5 :         ));                                                                                      
     174               5 :         ksort($parameters);                                                                      
     175               5 :         $parameterString = '';                                                                   
     176                 :                                                                                                  
     177               5 :         foreach ($parameters as $name => $value) {                                               
     178               5 :             $parameterString .= $name . '=' . $value;                                            
     179               5 :         }                                                                                        
     180                 :                                                                                                  
     181               5 :         return $parameterString;                                                                 
     182                 :     }                                                                                            
     183                 :                                                                                                  
     184                 :     public function generateSignature() {                                                        
     185               4 :         $plain = $this->generateParamString() . $this->secret;                                   
     186               4 :         Api_Log::log('Hashing plain signature: ' . $plain);                                      
     187               4 :         return md5($plain);                                                                      
     188                 :     }                                                                                            

Generated by PHPUnit 3.4.15 and Xdebug 2.1.0 using PHP 5.3.1 at Tue Oct 5 19:23:04 CEST 2010.