|
1
|
|
|
<?php namespace Packback\Prices; |
|
2
|
|
|
|
|
3
|
|
|
use Packback\Prices\PriceDto; |
|
4
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
5
|
|
|
|
|
6
|
|
|
abstract class PriceClient |
|
7
|
|
|
{ |
|
8
|
|
|
const CONDITION_NEW = 'new'; |
|
9
|
|
|
const CONDITION_GOOD = 'good'; |
|
10
|
|
|
const CONDITION_ACCEPTABLE = 'acceptable'; |
|
11
|
|
|
const CONDITION_POOR = 'poor'; |
|
12
|
|
|
|
|
13
|
|
|
const TERM_PERPETUAL = 'perpetual'; |
|
14
|
|
|
const TERM_SEMESTER = 'semester'; |
|
15
|
|
|
const TERM_QUARTER = 'quarter'; |
|
16
|
|
|
const TERM_HALFQUARTER = 'half-quarter'; |
|
17
|
|
|
const TERM_TWOMONTH = 'two-month'; |
|
18
|
|
|
const TERM_ONEMONTH = 'one-month'; |
|
19
|
|
|
const TERM_DAILY = 'daily'; |
|
20
|
|
|
|
|
21
|
|
|
public $client; |
|
22
|
|
|
public $query = []; |
|
23
|
|
|
public $collection = []; |
|
24
|
|
|
public $baseUrl = ''; |
|
25
|
|
|
|
|
26
|
40 |
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
40 |
|
$this->client = new GuzzleClient(); |
|
29
|
40 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
abstract public function getPricesForIsbns($isbns = []); |
|
32
|
|
|
|
|
33
|
|
|
abstract public function addPricesToCollection($response); |
|
34
|
|
|
|
|
35
|
12 |
|
public function addParam($key, $value) |
|
36
|
|
|
{ |
|
37
|
12 |
|
$this->query[$key] = $value; |
|
38
|
12 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
6 |
|
public function send() |
|
42
|
|
|
{ |
|
43
|
6 |
|
$querystring = http_build_query($this->query); |
|
44
|
|
|
try { |
|
45
|
6 |
|
$response = $this->client->get($this->baseUrl.'?'.$querystring); |
|
|
|
|
|
|
46
|
6 |
|
if ($response->getStatusCode() == '200') { |
|
47
|
6 |
|
return $this->decodeXml($response->getBody(true)); |
|
48
|
|
|
} |
|
49
|
2 |
|
} catch (\Exception $e) { |
|
50
|
|
|
// Return error messaging |
|
51
|
2 |
|
return $e->getMessage(); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
6 |
|
public function decodeXml($string) |
|
56
|
|
|
{ |
|
57
|
6 |
|
return json_decode( |
|
58
|
6 |
|
json_encode( |
|
59
|
6 |
|
simplexml_load_string( |
|
60
|
|
|
$string |
|
61
|
6 |
|
) |
|
62
|
4 |
|
), |
|
63
|
|
|
true |
|
64
|
4 |
|
); |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
48 |
|
public function createNewPrice() |
|
68
|
|
|
{ |
|
69
|
48 |
|
return new PriceDto(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
8 |
|
public function addPriceToCollection($price) |
|
73
|
|
|
{ |
|
74
|
8 |
|
$this->collection[] = $price; |
|
75
|
8 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
18 |
|
public function getConditionFromString($string) |
|
79
|
|
|
{ |
|
80
|
18 |
|
switch (strtolower($string)) { |
|
81
|
18 |
|
case 'new': |
|
82
|
18 |
|
case 'rent for 125 days': |
|
83
|
18 |
|
case 'rent for 90 days': |
|
84
|
18 |
|
case 'rent for 60 days': |
|
85
|
18 |
|
case 'rent for 45 days': |
|
86
|
18 |
|
case 'rent for 30 days': |
|
87
|
6 |
|
return self::CONDITION_NEW; |
|
88
|
14 |
|
case 'very good': |
|
89
|
14 |
|
case 'like new': |
|
90
|
14 |
|
case 'as new': |
|
91
|
14 |
|
case 'good': |
|
92
|
14 |
|
case 'used': |
|
93
|
14 |
|
case 'collectible': |
|
94
|
8 |
|
return self::CONDITION_GOOD; |
|
95
|
6 |
|
case 'near fine': |
|
96
|
6 |
|
case 'fine': |
|
97
|
6 |
|
case 'fair': |
|
98
|
6 |
|
case 'acceptable': |
|
99
|
2 |
|
return self::CONDITION_ACCEPTABLE; |
|
100
|
4 |
|
case 'poor': |
|
101
|
4 |
|
return self::CONDITION_POOR; |
|
102
|
|
|
default: |
|
103
|
|
|
throw new \Exception("Unrecognized book condition", 1); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
12 |
|
public function getTermFromString($string = null) |
|
108
|
|
|
{ |
|
109
|
12 |
|
switch (strtolower($string)) { |
|
110
|
12 |
|
case 'used': |
|
111
|
12 |
|
case 'new': |
|
112
|
4 |
|
return self::TERM_PERPETUAL; |
|
113
|
8 |
|
case 'rent for 125 days': |
|
114
|
8 |
|
case 'semester': |
|
115
|
2 |
|
return self::TERM_SEMESTER; |
|
116
|
8 |
|
case 'rent for 90 days': |
|
117
|
8 |
|
case 'quarter': |
|
118
|
2 |
|
return self::TERM_QUARTER; |
|
119
|
8 |
|
case 'rent for 60 days': |
|
120
|
2 |
|
return self::TERM_TWOMONTH; |
|
121
|
6 |
|
case 'rent for 45 days': |
|
122
|
2 |
|
return self::TERM_HALFQUARTER; |
|
123
|
4 |
|
case 'rent for 30 days': |
|
124
|
2 |
|
return self::TERM_ONEMONTH; |
|
125
|
2 |
|
default: |
|
126
|
2 |
|
return null; |
|
127
|
2 |
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
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: