Skip to content

Commit

Permalink
Add DataTransformerFactory + tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Littlemore committed Jul 20, 2017
1 parent 235bfc1 commit cd432d2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 56 deletions.
86 changes: 30 additions & 56 deletions src/AmazonAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@

use MarcL\CurlHttpRequest;
use MarcL\AmazonUrlBuilder;
use MarcL\Transformers\ArrayTransformer;
use MarcL\Transformers\JsonTransformer;
use MarcL\Transformers\SimpleArrayTransformer;
use MarcL\Transformers\XmlTransformer;
use MarcL\Transformers\DataTransformerFactory;

class AmazonAPI
{
private $m_retrieveArray = false;
private $urlBuilder = NULL;
private $dataTransformer = NULL;

Expand Down Expand Up @@ -75,61 +71,13 @@ private function throwIfNull($parameterValue, $parameterName) {

public function __construct($urlBuilder, $outputType) {
$this->urlBuilder = $urlBuilder;
$this->dataTransformer = $this->createDataTransformer($outputType);
}

public function SetRetrieveAsArray($retrieveArray = true) {
$this->m_retrieveArray = $retrieveArray;
$this->dataTransformer = DataTransformerFactory::create($outputType);
}

public function GetValidSearchNames() {
return $this->mValidSearchNames;
}

private function MakeSignedRequest($params) {
$signedUrl = $this->urlBuilder->generate($params);

try {
$request = new CurlHttpRequest();
$response = $request->execute($signedUrl);

$parsedXml = simplexml_load_string($response);

return($parsedXml);
} catch(\Exception $error) {
$this->AddError("Error downloading data : $signedUrl : " . $error->getMessage());
}

return false;
}

private function MakeAndParseRequest($params) {
$parsedXml = $this->MakeSignedRequest($params);
if ($parsedXml === false) {
return(false);
}

return $this->dataTransformer->execute($parsedXml);
}

private function createDataTransformer($outputType) {
switch($outputType) {
case 'array':
return new ArrayTransformer();
break;
case 'json':
return new JsonTransformer();
break;
case 'simple':
return new SimpleArrayTransformer();
break;
case 'xml':
default:
return new XmlTransformer();
break;
}
}

/**
* Search for items
*
Expand Down Expand Up @@ -177,12 +125,38 @@ public function ItemLookup($asinList, $onlyFromAmazon = false) {
return $this->MakeAndParseRequest($params);
}

public function GetErrors() {
return $this->mErrors;
}

private function AddError($error) {
array_push($this->mErrors, $error);
}

public function GetErrors() {
return $this->mErrors;
private function MakeSignedRequest($params) {
$signedUrl = $this->urlBuilder->generate($params);

try {
$request = new CurlHttpRequest();
$response = $request->execute($signedUrl);

$parsedXml = simplexml_load_string($response);

return($parsedXml);
} catch(\Exception $error) {
$this->AddError("Error downloading data : $signedUrl : " . $error->getMessage());
}

return false;
}

private function MakeAndParseRequest($params) {
$parsedXml = $this->MakeSignedRequest($params);
if ($parsedXml === false) {
return(false);
}

return $this->dataTransformer->execute($parsedXml);
}
}
?>
25 changes: 25 additions & 0 deletions src/Transformers/DataTransformerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace MarcL\Transformers;

class DataTransformerFactory {
static public function create($outputType) {
switch($outputType) {
case 'array':
return new ArrayTransformer();
break;
case 'json':
return new JsonTransformer();
break;
case 'simple':
return new SimpleArrayTransformer();
break;
case 'xml':
default:
return new XmlTransformer();
break;
}
}
}

?>

0 comments on commit cd432d2

Please sign in to comment.