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