1
|
|
|
<?php namespace Packback\Prices\Clients; |
2
|
|
|
|
3
|
|
|
use CROSCON\CommissionJunction\Client; |
4
|
|
|
use Packback\Prices\PriceClient; |
5
|
|
|
|
6
|
|
|
class CommissionJunctionPriceClient extends PriceClient |
7
|
|
|
{ |
8
|
20 |
|
public function __construct($config = []) |
9
|
|
|
{ |
10
|
20 |
|
$this->client = new Client($config['key']); |
11
|
20 |
|
$this->query['website-id'] = $config['website']; |
12
|
20 |
|
$this->query['advertiser-ids'] = $config['cj_advertiser_id']; |
13
|
20 |
|
} |
14
|
|
|
|
15
|
4 |
|
public function getPricesForIsbns($isbns = []) |
16
|
|
|
{ |
17
|
4 |
|
foreach ($isbns as $isbn) { |
18
|
4 |
|
$response = $this->addParam('isbn', $isbn)->send(); |
19
|
4 |
|
$this->addPricesToCollection($response); |
20
|
4 |
|
} |
21
|
|
|
|
22
|
4 |
|
return $this->collection; |
23
|
|
|
} |
24
|
|
|
|
25
|
16 |
|
public function addPricesToCollection($response) |
26
|
|
|
{ |
27
|
16 |
|
if ($this->responseHasProducts($response)) { |
28
|
12 |
|
if (is_array($response->products->product)) { |
29
|
2 |
|
foreach ($response->products->product as $product) { |
30
|
2 |
|
$this->collection[] = $this->makePriceFromProduct($product); |
31
|
2 |
|
} |
32
|
2 |
|
} else { |
33
|
10 |
|
$this->collection[] = $this->makePriceFromProduct($response->products->product); |
34
|
|
|
} |
35
|
12 |
|
} |
36
|
16 |
|
return $this->collection; |
37
|
|
|
} |
38
|
|
|
|
39
|
12 |
|
public function makePriceFromProduct($product) |
40
|
|
|
{ |
41
|
12 |
|
$price = $this->createNewPrice(); |
42
|
12 |
|
$price->isbn13 = $product->isbn; |
43
|
12 |
|
$price->price = $product->price; |
44
|
12 |
|
$price->url = $product->{'buy-url'}; |
45
|
12 |
|
$price->shipping_price = null; |
46
|
12 |
|
return $price; |
47
|
|
|
} |
48
|
|
|
|
49
|
20 |
|
public function responseHasProducts($response) |
50
|
|
|
{ |
51
|
20 |
|
if (is_object($response) && isset($response->products) |
52
|
20 |
|
&& isset($response->products->{'@attributes'}) |
53
|
20 |
|
&& isset($response->products->{'@attributes'}->{'records-returned'}) |
54
|
20 |
|
&& $response->products->{'@attributes'}->{'records-returned'} > 0) { |
55
|
14 |
|
return true; |
56
|
|
|
} |
57
|
6 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
10 |
|
public function send() |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
10 |
|
return json_decode(json_encode($this->client->productSearch($this->query)), false); |
|
|
|
|
64
|
2 |
|
} catch (\Exception $e) { |
65
|
|
|
// Return error messaging |
66
|
2 |
|
return $e->getMessage(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: