Current file: /Users/sst/Sites/kapit/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
80.00%80.00%
80.00% 8 / 10
87.10%87.10%
87.10% 54 / 62
 
Api_Kwick_Request
0.00%0.00%
0.00% 0 / 1
80.00%80.00%
80.00% 8 / 10
87.10%87.10%
87.10% 54 / 62
 public function __construct(Api_Kwick_Config $config)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 8 / 8
 public function setSecret($secret)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 2 / 2
 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
76.47%76.47%
76.47% 13 / 17
 public function generateUrl()
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                 : class Api_Kwick_Request {                                                                        
       4                 :     const VERSION_20 = '2.0';                                                                    
       5                 :                                                                                                  
       6                 :     const DEFAULT_METHOD  = 'getAPIVersion';                                                     
       7                 :     const DEFAULT_SERVICE = 'application';                                                       
       8                 :                                                                                                  
       9                 :     private $baseUrl;                                                                            
      10                 :     private $apiKey;                                                                             
      11                 :     private $secret;                                                                             
      12                 :     private $version;                                                                            
      13                 :     private $method;                                                                             
      14                 :     private $service;                                                                            
      15                 :     private $parameters;                                                                         
      16                 :                                                                                                  
      17                 :     public function  __construct(Api_Kwick_Config $config) {                                     
      18              11 :         $this->baseUrl = $config->get('baseUrl');                                                
      19              11 :         $this->apiKey  = $config->get('apiKey');                                                 
      20              11 :         $this->setSecret($config->get('secret'));                                                
      21              11 :         $this->version = $config->get('version', self::VERSION_20);                              
      22              11 :         $this->service = self::DEFAULT_SERVICE;                                                  
      23              11 :         $this->method  = self::DEFAULT_METHOD;                                                   
      24              11 :         $this->parameters = array();                                                             
      25              11 :     }                                                                                            
      26                 :                                                                                                  
      27                 :     public function setSecret($secret) {                                                         
      28               8 :         $this->secret = $secret;                                                                 
      29               8 :     }                                                                                            
      30                 :                                                                                                  
      31                 :     public function setParameter($name, $value) {                                                
      32               2 :         Api_Log::log('Set parameter: ' . $name . ' with: ' . $value);                            
      33               2 :         $this->parameters[$name] = $value;                                                       
      34               2 :     }                                                                                            
      35                 :                                                                                                  
      36                 :     public function setMethod($method) {                                                         
      37               1 :         Api_Log::log('Set method to: ' . $method);                                               
      38               1 :         $this->method = (string) $method;                                                        
      39               1 :     }                                                                                            
      40                 :                                                                                                  
      41                 :     public function setService($service) {                                                       
      42               1 :         Api_Log::log('Set service to: ' . $service);                                             
      43               1 :         $this->service = (string) $service;                                                      
      44               1 :     }                                                                                            
      45                 :                                                                                                  
      46                 :     public function send(HttpRequest $httpRequest = null) {                                      
      47               2 :         if (null === $httpRequest) {                                                             
      48               0 :             if (!class_exists('HttpRequest', false)) {                                           
      49               0 :                 throw new RuntimeException('Can not find class HttpRequest! Install pecl http.');
      50                 :             }                                                                                    
      51                 :                                                                                                  
      52               0 :             $httpRequest = new HttpRequest();                                                    
      53               0 :         }                                                                                        
      54                 :                                                                                                  
      55               2 :         $url = $this->generateUrl();                                                             
      56               2 :         Api_Log::log('Send request with url: ' . $url);                                          
      57               2 :         $httpRequest->setUrl($url);                                                              
      58               2 :         $httpRequest->setMethod(HttpRequest::METH_POST);                                         
      59               2 :         $start = microtime(true);                                                                
      60               2 :         $httpRequest->send();                                                                    
      61               2 :         $time  = microtime(true) - $start;                                                       
      62               2 :         Api_Log::log('Duration of api request: ' . round($time, 3) . 's');                       
      63                 :                                                                                                  
      64               2 :         if ($httpRequest->getResponseCode() === 200) {                                           
      65               1 :             return $httpRequest->getResponseBody();                                              
      66                 :         }                                                                                        
      67                 :                                                                                                  
      68               1 :         $message = 'Did not get 200 OK on requesting url: ' . $url;                              
      69               1 :         throw new RuntimeException($message, $httpRequest->getResponseCode());                   
      70                 :     }                                                                                            
      71                 :                                                                                                  
      72                 :     public function generateUrl() {                                                              
      73               3 :         $url  = $this->baseUrl . '?';                                                            
      74               3 :         $url .= 'api_key=' . $this->apiKey . '&';                                                
      75               3 :         $url .= 'v=' . $this->version . '&';                                                     
      76               3 :         $url .= 'method=' . $this->getQualifiedMethod() . '&';                                   
      77               3 :         $url .= 'sig=' . $this->generateSignature();                                             
      78                 :                                                                                                  
      79               3 :         if (!empty($this->parameters)) {                                                         
      80               0 :             foreach ($this->parameters as $name => $value) {                                     
      81               0 :                 $url .= '&' . $name . '=' . $value;                                              
      82               0 :             }                                                                                    
      83               0 :         }                                                                                        
      84                 :                                                                                                  
      85               3 :         return $url;                                                                             
      86                 :     }                                                                                            
      87                 :                                                                                                  
      88                 :     public function getQualifiedMethod() {                                                       
      89               6 :         return $this->service . '.' . $this->method;                                             
      90                 :     }                                                                                            
      91                 :                                                                                                  
      92                 :     public function generateParamString() {                                                      
      93               5 :         $parameters = array_merge($this->parameters, array(                                      
      94               5 :             'api_key' => $this->apiKey,                                                          
      95               5 :             'v'       => $this->version,                                                         
      96               5 :             'method'  => $this->getQualifiedMethod()                                             
      97               5 :         ));                                                                                      
      98               5 :         ksort($parameters);                                                                      
      99               5 :         $parameterString = '';                                                                   
     100                 :                                                                                                  
     101               5 :         foreach ($parameters as $name => $value) {                                               
     102               5 :             $parameterString .= $name . '=' . $value;                                            
     103               5 :         }                                                                                        
     104                 :                                                                                                  
     105               5 :         return $parameterString;                                                                 
     106                 :     }                                                                                            
     107                 :                                                                                                  
     108                 :     public function generateSignature() {                                                        
     109               4 :         $plain = $this->generateParamString() . $this->secret;                                   
     110               4 :         Api_Log::log('Hashing plain signature: ' . $plain);                                      
     111               4 :         return md5($plain);                                                                      
     112                 :     }                                                                                            

Generated by PHPUnit 3.4.11 and Xdebug 2.0.5 using PHP 5.3.0 at Thu Mar 18 10:20:58 CET 2010.