Completed
Pull Request — master (#4)
by
unknown
01:26
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
0 ignored issues
show
Documentation introduced by
The doc-type [int] could not be parsed: Unknown type name "" at position 0. [(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...
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
0 ignored issues
show
Documentation introduced by
The doc-type [string] could not be parsed: Unknown type name "" at position 0. [(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...
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
0 ignored issues
show
Documentation introduced by
The doc-type [string] could not be parsed: Unknown type name "" at position 0. [(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...
54
     * @return array
55
     */
56
    private function fetch($id = false)
57
    {
58
        if ($this->limit <= 40) {
59
            if ($id) {
60
                return $this->client->getItem("volumes/$id", $this->getParams());
0 ignored issues
show
Unused Code introduced by
The call to GoogleBooks::getItem() has too many arguments starting with $this->getParams().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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
0 ignored issues
show
Documentation introduced by
The doc-type [string] could not be parsed: Unknown type name "" at position 0. [(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...
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;
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...
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
0 ignored issues
show
Documentation introduced by
The doc-type [int] could not be parsed: Unknown type name "" at position 0. [(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...
118
     * @return self
119
     */
120
    public function chunk($chunk)
121
    {
122
        $this->chunk = $chunk;
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