|
1
|
|
|
<?php namespace Packback\Prices\Clients; |
|
2
|
|
|
|
|
3
|
|
|
use Packback\Prices\PriceClient; |
|
4
|
|
|
use ApaiIO\Configuration\GenericConfiguration; |
|
5
|
|
|
use ApaiIO\Operations\Search; |
|
6
|
|
|
use ApaiIO\ApaiIO; |
|
7
|
|
|
|
|
8
|
|
|
class AmazonPriceClient extends PriceClient |
|
9
|
|
|
{ |
|
10
|
|
|
const RETAILER = 'amazon'; |
|
11
|
|
|
|
|
12
|
|
|
public $conf; |
|
13
|
|
|
public $container; |
|
14
|
|
|
public $search; |
|
15
|
|
|
|
|
16
|
16 |
|
public function __construct($config = []) |
|
17
|
|
|
{ |
|
18
|
16 |
|
$this->conf = new GenericConfiguration(); |
|
19
|
16 |
|
$this->setConfiguration($config); |
|
20
|
16 |
|
$this->container = new ApaiIO($this->conf); |
|
21
|
16 |
|
$this->search = new Search(); |
|
22
|
16 |
|
} |
|
23
|
|
|
|
|
24
|
16 |
|
public function setConfiguration($config = []) |
|
25
|
|
|
{ |
|
26
|
16 |
|
$this->conf |
|
27
|
16 |
|
->setCountry('com') |
|
28
|
16 |
|
->setAccessKey($config['access_key']) |
|
29
|
16 |
|
->setSecretKey($config['secret_key']) |
|
30
|
16 |
|
->setAssociateTag($config['associate_tag']); |
|
31
|
16 |
|
$this->conf->setResponseTransformer('\ApaiIO\ResponseTransformer\XmlToSimpleXmlObject'); |
|
32
|
16 |
|
} |
|
33
|
|
|
|
|
34
|
6 |
|
public function getPricesForIsbns($isbns = []) |
|
35
|
|
|
{ |
|
36
|
6 |
|
$isbn_groups = []; |
|
37
|
|
|
|
|
38
|
6 |
|
if (count($isbns) > 10) { |
|
39
|
2 |
|
$isbn_groups = array_chunk($isbns, 10); |
|
40
|
2 |
|
} else { |
|
41
|
4 |
|
array_push($isbn_groups, $isbns); |
|
42
|
|
|
} |
|
43
|
6 |
|
foreach ($isbn_groups as $isbn_group) { |
|
44
|
6 |
|
$response = $this->addParam('Condition', 'New') |
|
|
|
|
|
|
45
|
6 |
|
->addParam('IdType', 'ISBN') |
|
46
|
6 |
|
->addParam('ItemId', $isbn_group) |
|
47
|
6 |
|
->addParam('Condition', 'All') |
|
48
|
6 |
|
->addParam('SearchIndex', 'Books') |
|
49
|
6 |
|
->addParam('Operation', 'ItemLookup') |
|
50
|
6 |
|
->addParam('Version', '2011-08-01') |
|
51
|
6 |
|
->addParam('ResponseGroup', ['Large']) |
|
52
|
6 |
|
->addParam('Service', 'AWSECommerceService'); |
|
53
|
|
|
|
|
54
|
|
|
// Get response for Marketplace books |
|
55
|
6 |
|
$response = $this->send(); |
|
56
|
|
|
// If there is an error with the API key, throw an exception |
|
57
|
6 |
|
if (isset($response->Error)) { |
|
58
|
|
|
throw new \Exception($response->Error->Code.': '.$response->Error->Message); |
|
59
|
|
|
} |
|
60
|
6 |
|
$this->addPricesToCollection($response); |
|
61
|
|
|
// Get response for only Amazon books |
|
62
|
6 |
|
$response = $this->addParam('MerchantId', 'Amazon')->send(); |
|
63
|
6 |
|
$this->addPricesToCollection($response); |
|
64
|
6 |
|
} |
|
65
|
|
|
|
|
66
|
6 |
|
return $this->collection; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
6 |
|
public function addPricesToCollection($response) |
|
70
|
|
|
{ |
|
71
|
6 |
|
if (isset($response->Items->Request->IsValid) && $response->Items->Request->IsValid) { |
|
72
|
6 |
|
if (isset($response->Items->Item)) { |
|
73
|
6 |
|
$items = $response->Items->Item; |
|
74
|
6 |
|
foreach ($items as $key => $item) { |
|
75
|
|
|
// Create price objects for each offer |
|
76
|
6 |
|
if (isset($item->Offers->Offer)) { |
|
77
|
6 |
|
if (is_array($item->Offers->Offer)) { |
|
78
|
2 |
|
foreach ($item->Offers->Offer as $offer) { |
|
79
|
2 |
|
$this->collection[] = $this->populatePriceData($offer, $item, self::RETAILER); |
|
80
|
2 |
|
} |
|
81
|
2 |
|
} else { |
|
82
|
4 |
|
$this->collection[] = $this->populatePriceData($item->Offers->Offer, $item, self::RETAILER); |
|
83
|
|
|
} |
|
84
|
6 |
|
} |
|
85
|
6 |
|
} |
|
86
|
6 |
|
} |
|
87
|
6 |
|
} |
|
88
|
6 |
|
return $this->collection; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
10 |
|
public function populatePriceData($offer, $item, $merchant_id) |
|
92
|
|
|
{ |
|
93
|
|
|
// New price model object |
|
94
|
8 |
|
$price = $this->createNewPrice(); |
|
95
|
|
|
// Set retailer |
|
96
|
8 |
|
$price->retailer = $merchant_id; |
|
97
|
|
|
// Populate ISBN 13 |
|
98
|
8 |
|
if (isset($item->ItemAttributes->EAN)) { |
|
99
|
6 |
|
$price->isbn13 = $item->ItemAttributes->EAN; |
|
100
|
8 |
|
} elseif (isset($item->ItemAttributes->EISBN)) { |
|
101
|
2 |
|
$price->isbn13 = $item->ItemAttributes->EISBN; |
|
102
|
2 |
|
} |
|
103
|
|
|
// Populate URL |
|
104
|
10 |
|
if (isset($item->DetailPageURL)) { |
|
105
|
8 |
|
$price->url = $item->DetailPageURL; |
|
106
|
8 |
|
} |
|
107
|
|
|
// Populate condition |
|
108
|
8 |
|
$price->condition = parent::getConditionFromString($offer->OfferAttributes->Condition); |
|
|
|
|
|
|
109
|
|
|
// Populate term |
|
110
|
8 |
|
$price->term = parent::TERM_PERPETUAL; |
|
111
|
|
|
// Populate Price |
|
112
|
8 |
|
$price->price = ((float)$offer->OfferListing->Price->Amount)/100; |
|
|
|
|
|
|
113
|
8 |
|
return $price; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
6 |
|
public function addParam($key, $value) |
|
117
|
|
|
{ |
|
118
|
6 |
|
$this->search->{'set'.$key}($value); |
|
119
|
6 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
10 |
|
public function send() |
|
123
|
|
|
{ |
|
124
|
10 |
|
$response = $this->container->runOperation($this->search); |
|
125
|
8 |
|
return json_decode(json_encode($response)); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.