1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\GoogleBooks; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
class GoogleBooks |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $baseUri = 'https://www.googleapis.com/books/v1/'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var integer (Number of results to retrieve per batch, between 1 and 40) |
16
|
|
|
*/ |
17
|
|
|
protected $batchSize = 40; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
protected $http; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var key string API key |
26
|
|
|
*/ |
27
|
|
|
protected $key; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var country string 2 letter ISO 639 country code. |
31
|
|
|
* |
32
|
|
|
* The Books API must honor copyright laws from various countries, and have |
33
|
|
|
* country-specific rights from publishers. It uses the IP address of the |
34
|
|
|
* client to geo-locate the user, but if this fails for some reason, it will |
35
|
|
|
* return 403 Forbidden with reason "unknownLocation". To avoid this, we can |
36
|
|
|
* manually set the country code. |
37
|
|
|
*/ |
38
|
|
|
protected $country; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Volumes |
42
|
|
|
*/ |
43
|
|
|
public $volumes; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Bookshelves |
47
|
|
|
*/ |
48
|
|
|
public $bookshelves; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var maxResults |
52
|
|
|
*/ |
53
|
|
|
public $maxResults; |
54
|
|
|
|
55
|
|
|
public function __construct($options = []) |
56
|
|
|
{ |
57
|
|
|
$this->http = new Client([ |
58
|
|
|
'base_uri' => $this->baseUri, |
59
|
|
|
'handler' => isset($options['handler']) ? $options['handler'] : null, |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
$this->key = isset($options['key']) ? $options['key'] : null; |
63
|
|
|
$this->country = isset($options['country']) ? $options['country'] : null; |
64
|
|
|
|
65
|
|
|
$this->volumes = new Volumes($this); |
66
|
|
|
$this->bookshelves = new Bookshelves($this); |
67
|
|
|
|
68
|
|
|
$this->batchSize = isset($options['batchSize']) ? $options['batchSize'] : 40; |
69
|
|
|
|
70
|
|
|
$this->maxResults = isset($options['maxResults']) ? $options['maxResults'] : null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function raw($endpoint, $params = [], $method='GET') |
74
|
|
|
{ |
75
|
|
|
if (!is_null($this->key)) { |
76
|
|
|
$params['key'] = $this->key; |
77
|
|
|
} |
78
|
|
|
if (!is_null($this->country)) { |
79
|
|
|
$params['country'] = $this->country; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!is_null($this->maxResults)) { |
83
|
|
|
$params['maxResults'] = $this->maxResults; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
$response = $this->http->request($method, $endpoint, [ |
88
|
|
|
'query' => $params, |
89
|
|
|
]); |
90
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
91
|
|
|
// 400 level errors |
92
|
|
|
if ($e->getResponse()->getStatusCode() == 403) { |
93
|
|
|
$json = json_decode($e->getResponse()->getBody()); |
94
|
|
|
|
95
|
|
|
$domain = $json->error->errors[0]->domain; |
96
|
|
|
$reason = $json->error->errors[0]->reason; |
97
|
|
|
$message = $json->error->errors[0]->message; |
98
|
|
|
|
99
|
|
|
if ($domain == 'usageLimits') { |
100
|
|
|
throw new Exceptions\UsageLimitExceeded($message, $reason); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
throw $e; |
105
|
|
|
|
106
|
|
|
} catch (\GuzzleHttp\Exception\RequestException $e) { |
107
|
|
|
// networking error (connection timeout, DNS errors, etc.) |
108
|
|
|
|
109
|
|
|
// TODO: sleep and retry |
110
|
|
|
|
111
|
|
|
throw $e; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return json_decode($response->getBody()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getItem($path) |
118
|
|
|
{ |
119
|
|
|
return $this->raw($path); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* If given a full URL, removes the base part. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function removeBaseUrl($url) |
128
|
|
|
{ |
129
|
|
|
if (strpos($url, $this->baseUri) === false) { |
130
|
|
|
throw new RuntimeError('Invalid URL ' . $url); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return substr($url, strlen($this->baseUri)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function listItems($endpoint, $params = []) |
137
|
|
|
{ |
138
|
|
|
$params['maxResults'] = $this->batchSize; |
139
|
|
|
|
140
|
|
|
$i = 0; |
141
|
|
|
while (true) { |
142
|
|
|
$n = $i % $this->batchSize; |
143
|
|
|
if ($n == 0) { |
144
|
|
|
$params['startIndex'] = $i; |
145
|
|
|
$response = $this->raw($endpoint, $params); |
146
|
|
|
} |
147
|
|
|
if (isset($response->totalItems) && $i >= $response->totalItems) { |
|
|
|
|
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
if (!isset($response->items[$n])) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
yield $response->items[$n]; |
154
|
|
|
$i++; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: