Skip to content

Commit

Permalink
Code formatting, because I am OCD with brackets...
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabeel Shahzad committed Nov 15, 2011
1 parent a000a00 commit fad2ea5
Showing 1 changed file with 45 additions and 56 deletions.
101 changes: 45 additions & 56 deletions amazon.php
Expand Up @@ -27,27 +27,24 @@
*
*/

class AmazonError extends Exception
{
class AmazonError extends Exception {
public $message, $code, $detail;

public function __construct($message, $code = -1 , $detail = '')
{
public function __construct($message, $code = -1 , $detail = '') {
$this->message = $message;
$this->code = $code;
$this->detail = $detail;

parent::__construct($message, $code, null);
}

public function getDetail()
{
public function getDetail() {
return $this->detail;
}
}

class AmazonProductLookup
{
class AmazonProductLookup {
protected $AWS_KEY = null;
protected $SECRET_KEY = null;
protected $LOCALE = 'US';
Expand Down Expand Up @@ -79,17 +76,15 @@ class AmazonProductLookup
'US' => 'com',
);

public function __construct($AWS_KEY, $SECRET_KEY, $locale = 'US')
{
public function __construct($AWS_KEY, $SECRET_KEY, $locale = 'US') {
$this->AWS_KEY = $AWS_KEY;
$this->SECRET_KEY = $SECRET_KEY;
$this->LOCALE = strtoupper($locale);

# Build the full URL based on the locale passed in
$this->SERVICE_DOMAIN = $this->SERVICE_DOMAIN.$this->locales[$this->LOCALE];

if(!function_exists('curl_init'))
{
if(!function_exists('curl_init')) {
$this->last_error = 'cURL must exist!';
return false;
}
Expand All @@ -100,28 +95,23 @@ public function __construct($AWS_KEY, $SECRET_KEY, $locale = 'US')
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0);
}

public function __destruct()
{
public function __destruct() {
curl_close($this->curl);
}

public function addArgument($name, $value)
{
public function addArgument($name, $value) {
$this->default_args[$name] = $value;
}

public function getError()
{
public function getError() {
return $this->last_error;
}

public function getErrorDetail()
{
public function getErrorDetail() {
return $this->last_errordetail;
}

public function setSSL($bool)
{
public function setSSL($bool) {
$this->USE_SSL = $bool;
}

Expand All @@ -138,17 +128,17 @@ public function setSSL($bool)
}
}*/

public function __call($requestName, $args)
{
if($this->AWS_KEY === null || $this->SECRET_KEY === null)
{
public function __call($requestName, $args) {

if($this->AWS_KEY === null || $this->SECRET_KEY === null) {
$this->last_error = 'Invalid AWS Key and/or Secret Key';
$this->last_errordetail = $this->last_error;

if($this->throw_exceptions === true)
if($this->throw_exceptions === true) {
throw new AmazonError($this->last_error, -1, $this->last_errordetail);
else
} else {
return false;
}
}

$args = array_merge($this->default_args, array(
Expand All @@ -162,13 +152,12 @@ public function __call($requestName, $args)
return $this->buildRequest($args);
}

public function buildRequest($params)
{
public function buildRequest($params) {
ksort($params, SORT_STRING);

$query_string = array();
foreach ($params as $key => $value)
{
foreach ($params as $key => $value) {
$value = str_replace("%7E", "~", rawurlencode($value));
$query_string[] = trim($key)."=".trim($value);
}
Expand All @@ -180,12 +169,9 @@ public function buildRequest($params)
$signature = base64_encode(hash_hmac('sha256', $signstring, $this->SECRET_KEY, true));
$signature = str_replace("%7E", "~", rawurlencode($signature));

if($this->USE_SSL === true)
{
if($this->USE_SSL === true) {
$prefix = 'https://';
}
else
{
} else {
$prefix = 'http://';
}

Expand All @@ -194,37 +180,42 @@ public function buildRequest($params)
# Make the request and load the XML
$response = simplexml_load_string($this->makeRequest($request));

if(!$response)
{
if(!$response) {
$this->last_error = 'Invalid XML';
$this->last_errordetail = $this->last_error;

if($this->throw_exceptions === true)
if($this->throw_exceptions === true) {
throw new AmazonError($this->last_error, -1, $this->last_errordetail);
else
} else {
return false;
}

}

if(isset($response->Error))
{
if(isset($response->Error)) {
$this->last_error = (string) $response->Error->Code;
$this->last_errordetail = (string) $response->Error->Message;

if($this->throw_exceptions === true)
if($this->throw_exceptions === true) {
throw new AmazonError($this->last_error, -1, $this->last_errordetail);
else
} else {
return false;
}

}

if(isset($response->Items->Request->Errors->Error))
{
if(isset($response->Items->Request->Errors->Error)) {
$this->last_error = (string) $response->Items->Request->Errors->Error->Code;
$this->last_errordetail = (string) $response->Items->Request->Errors->Error->Message;

if($this->throw_exceptions === true)
if($this->throw_exceptions === true) {
throw new AmazonError($this->last_error, -1, $this->last_errordetail);
else
return false;
} else {
return false
}
}

# Finally good
Expand All @@ -239,16 +230,14 @@ public function buildRequest($params)
* @param string $url The URL to request
* @return Returns the raw data from Amazon
*/
protected function makeRequest($url)
{
protected function makeRequest($url) {
// @TODO: Error handling
curl_setopt($this->curl, CURLOPT_URL, $url);
$response = curl_exec($this->curl);

if($response === false)
{
if($this->throw_exceptions === true)
{
if($response === false) {
if($this->throw_exceptions === true) {
throw new Exception ('');
}
}
Expand Down

0 comments on commit fad2ea5

Please sign in to comment.