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
|
|
|
public function __construct($options = []) |
51
|
|
|
{ |
52
|
|
|
$this->http = new Client([ |
53
|
|
|
'base_uri' => $this->baseUri, |
54
|
|
|
'handler' => isset($options['handler']) ? $options['handler'] : null, |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$this->key = isset($options['key']) ? $options['key'] : null; |
58
|
|
|
$this->country = isset($options['country']) ? $options['country'] : null; |
59
|
|
|
|
60
|
|
|
$this->volumes = new Volumes($this); |
61
|
|
|
$this->bookshelves = new Bookshelves($this); |
62
|
|
|
|
63
|
|
|
$this->batchSize = isset($options['batchSize']) ? $options['batchSize'] : 40; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function raw($endpoint, $params = [], $method='GET') |
67
|
|
|
{ |
68
|
|
|
if (!is_null($this->key)) { |
69
|
|
|
$params['key'] = $this->key; |
70
|
|
|
} |
71
|
|
|
if (!is_null($this->country)) { |
72
|
|
|
$params['country'] = $this->country; |
73
|
|
|
} |
74
|
|
|
try { |
75
|
|
|
$response = $this->http->request($method, $endpoint, [ |
76
|
|
|
'query' => $params, |
77
|
|
|
]); |
78
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
79
|
|
|
// 400 level errors |
80
|
|
|
if ($e->getResponse()->getStatusCode() == 403) { |
81
|
|
|
$json = json_decode($e->getResponse()->getBody()); |
82
|
|
|
|
83
|
|
|
$domain = $json->error->errors[0]->domain; |
84
|
|
|
$reason = $json->error->errors[0]->reason; |
85
|
|
|
$message = $json->error->errors[0]->message; |
86
|
|
|
|
87
|
|
|
if ($domain == 'usageLimits') { |
88
|
|
|
throw new Exceptions\UsageLimitExceeded($message, $reason); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw $e; |
93
|
|
|
|
94
|
|
|
} catch (\GuzzleHttp\Exception\RequestException $e) { |
95
|
|
|
// networking error (connection timeout, DNS errors, etc.) |
96
|
|
|
|
97
|
|
|
// TODO: sleep and retry |
98
|
|
|
|
99
|
|
|
throw $e; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return json_decode($response->getBody()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getItem($path) |
106
|
|
|
{ |
107
|
|
|
return $this->raw($path); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function listItems($endpoint, $params = []) |
111
|
|
|
{ |
112
|
|
|
$params['maxResults'] = $this->batchSize; |
113
|
|
|
|
114
|
|
|
$i = 0; |
115
|
|
|
while (true) { |
116
|
|
|
$n = $i % $this->batchSize; |
117
|
|
|
if ($n == 0) { |
118
|
|
|
$params['startIndex'] = $i; |
119
|
|
|
$response = $this->raw($endpoint, $params); |
120
|
|
|
} |
121
|
|
|
if (isset($response->totalItems) && $i >= $response->totalItems) { |
|
|
|
|
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
if (!isset($response->items[$n])) { |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
yield $response->items[$n]; |
128
|
|
|
$i++; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
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: