Completed
Push — master ( 699478...5c97e7 )
by Dan Michael O.
02:26
created

GoogleBooks::raw()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 35
rs 8.439
cc 6
eloc 18
nc 10
nop 3
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) {
0 ignored issues
show
Bug introduced by
The variable $response does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
107
                return;
108
            }
109
            if (!isset($response->items[$n])) {
110
                return;
111
            }
112
            yield $response->items[$n];
113
            $i++;
114
        }
115
    }
116
117
}
118