Completed
Pull Request — master (#4)
by
unknown
01:23
created

LibraryBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php 
2
3
namespace Scriptotek\GoogleBooks;
4
5
use GuzzleHttp\Client;
6
use Scriptotek\GoogleBooks\GoogleBooks;
7
8
class LibraryBuilder
9
{
10
    private $data;
11
    private $client;
12
    private $chunk = false;
13
    private $query;
14
    private $limit;
15
16
    /**
17
     * Accepts a GoogleBooks dependency and sets it as a client property
18
     *
19
     * @param GoogleBooks $GoogleBooks
20
     */
21
    public function __construct(GoogleBooks $GoogleBooks)
22
    {
23
        $this->client = $GoogleBooks;
24
    }
25
26
    /**
27
     * Limits the results. May be an integer between 1 and 40
28
     *
29
     * @param int $limit
30
     * @return self
31
     */
32
    public function limit($limit)
33
    {
34
        $this->limit = $limit;
35
        return $this;
36
    }
37
38
    /**
39
     * Sets the query property
40
     *
41
     * @param string $query
42
     * @return self
43
     */
44
    public function search($query)
45
    {
46
        $this->query = $query;
47
        return $this;
48
    }
49
50
    /**
51
     * Fetches results from the Google API
52
     *
53
     * @param string $id
54
     * @return array
55
     */
56
    private function fetch($id = false)
57
    {
58
        if ($this->limit <= 40) {
59
            if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
60
                return $this->client->getItem("volumes/$id");
61
            }
62
            return $this->client->raw('volumes', $this->getParams());
63
        }
64
        
65
        return $this->fetchAll();
66
    }
67
68
    /**
69
     * Performs multiple searches in order to overcome Googles max of 40 results per request
70
     *
71
     * @return array
72
     */
73
    private function fetchAll()
74
    {
75
        return $this->data = $this->client->listItems('volumes', ['q' => $this->query]);
76
    }
77
78
    /**
79
     * Finds and returns one result. Should accept a book ID (found on API responses)
80
     *
81
     * @param string $id
82
     * @return Scriptotek\GoogleBooks\Volume;
0 ignored issues
show
Documentation introduced by
The doc-type Scriptotek\GoogleBooks\Volume; could not be parsed: Expected "|" or "end of type", but got ";" at position 29. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
83
     */
84
    public function find($id)
85
    {
86
        return $this->fetch($id);
87
    }
88
89
    /**
90
     * Retrieves the first result
91
     *
92
     * @return Scriptotek\GoogleBooks\Volume
93
     */
94
    public function first()
95
    {
96
        return $this->fetch()->items[0];
97
    }
98
99
    /**
100
     * Gets all the results from the query. Possibly chunks the results
101
     *
102
     * @return array
103
     * @see chunk()
104
     */
105
    public function all()
106
    {
107
        if ($this->chunk) {
108
            return $this->data = array_chunk($this->fetch()->items, $this->chunk);
109
        }
110
        
111
        return $this->data = $this->fetch();
112
    }
113
114
    /**
115
     * Sets the chunk property. Used as an alias for array chunking. Primarily for views
116
     *
117
     * @param int $chunk
118
     * @return self
119
     */
120
    public function chunk($chunk)
121
    {
122
        $this->chunk = $chunk;
0 ignored issues
show
Documentation Bug introduced by
The property $chunk was declared of type boolean, but $chunk is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
123
        return $this;
124
    }
125
126
    /**
127
     * Gets the paramters to be passed to the API request.
128
     *
129
     * @return array
130
     */
131
    private function getParams()
132
    {
133
        $params['q'] = $this->query;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
134
        $params['maxResults'] = $this->limit ?? 40;
135
136
        return $params;
137
    }
138
}
139