1
|
|
|
<?php namespace Packback\Prices\Clients; |
2
|
|
|
|
3
|
|
|
use Packback\Prices\PriceClient; |
4
|
|
|
|
5
|
|
|
class CheggPriceClient extends PriceClient |
6
|
|
|
{ |
7
|
|
|
const RETAILER = 'chegg'; |
8
|
|
|
|
9
|
14 |
|
public function __construct($config = []) |
10
|
|
|
{ |
11
|
14 |
|
parent::__construct(); |
12
|
14 |
|
$this->baseUrl = 'http://api.chegg.com/rent.svc'; |
13
|
14 |
|
$this->query['KEY'] = $config['access_key']; |
14
|
14 |
|
$this->query['PW'] = $config['secret_key']; |
15
|
14 |
|
$this->query['R'] = 'XML'; |
16
|
14 |
|
$this->query['V'] = '2.0'; |
17
|
14 |
|
$this->query['with_pids'] = '1'; |
18
|
14 |
|
} |
19
|
|
|
|
20
|
2 |
|
public function getPricesForIsbns($isbns = []) |
21
|
|
|
{ |
22
|
2 |
|
foreach ($isbns as $isbn) { |
23
|
2 |
|
if ($isbn) { |
24
|
2 |
|
$this->query['isbn'] = $isbn; |
25
|
2 |
|
$response = $this->send(); |
26
|
2 |
|
$this->addPricesToCollection($response); |
27
|
2 |
|
} |
28
|
2 |
|
} |
29
|
2 |
|
return $this->collection; |
30
|
|
|
} |
31
|
|
|
|
32
|
14 |
|
public function addPricesToCollection($payload) |
33
|
|
|
{ |
34
|
14 |
|
if ($this->isValidCheggResponse($payload)) { |
35
|
12 |
|
foreach ($payload['Items']['Item']['Terms']['Term'] as $term) { |
36
|
12 |
|
$price = $this->createNewPrice(); |
37
|
12 |
|
$price->isbn13 = $payload['Items']['Item']['EAN']; |
38
|
12 |
|
$price->retailer = self::RETAILER; |
39
|
12 |
|
$price->price = $term['Price']; |
40
|
12 |
|
$price->shipping_price = $this->setCheggShippingPrice($payload['Items']['Item']); |
41
|
12 |
|
$base_url = "http://chggtrx.com/click.track?CID=267582&AFID=304350&ADID=1088031&SID=&isbn_ean="; |
42
|
12 |
|
$price->url = $base_url . $price->isbn13; |
43
|
12 |
|
$price->condition = parent::CONDITION_GOOD; |
44
|
12 |
|
if (isset($term)) { |
45
|
12 |
|
$price->term = $this->getTermFromString($term['Term']); |
46
|
12 |
|
} |
47
|
12 |
|
if ($term['Term'] != 'SUMMER') { |
48
|
12 |
|
$this->collection[] = $price; |
49
|
12 |
|
} |
50
|
12 |
|
} |
51
|
12 |
|
} |
52
|
14 |
|
return $this->collection; |
53
|
|
|
} |
54
|
|
|
|
55
|
12 |
|
public function setCheggShippingPrice($item) |
56
|
|
|
{ |
57
|
12 |
|
if (isset($item['ShippingPrices']) |
58
|
12 |
|
&& isset($item['ShippingPrices']['ShippingPrice'])) { |
59
|
4 |
|
foreach ($item['ShippingPrices']['ShippingPrice'] as $shipping_price) { |
60
|
4 |
|
$prices[] = $shipping_price['Cost_first']; |
|
|
|
|
61
|
4 |
|
} |
62
|
4 |
|
return min($prices); |
|
|
|
|
63
|
|
|
} |
64
|
8 |
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
14 |
|
public function isValidCheggResponse($payload) |
68
|
|
|
{ |
69
|
14 |
|
if (is_array($payload) |
70
|
14 |
|
&& isset($payload['Items']) |
71
|
14 |
|
&& isset($payload['Items']['Item']) |
72
|
14 |
|
&& isset($payload['Items']['Item']['Terms']) |
73
|
14 |
|
&& isset($payload['Items']['Item']['Terms']['Term'])) { |
74
|
12 |
|
return true; |
75
|
|
|
} |
76
|
2 |
|
return false; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.