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

LibraryBuilder::getParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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;
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 !== false) {
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
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
        if ($this->fetch()->totalItems) {
97
            return $this->fetch()->items[0];
98
        }
99
100
        return null;
101
        
102
    }
103
104
    /**
105
     * Gets all the results from the query. Possibly chunks the results
106
     *
107
     * @return array
108
     * @see chunk()
109
     */
110
    public function all()
111
    {
112
        if ($this->chunk) {
113
            return $this->data = array_chunk($this->fetch()->items, $this->chunk);
114
        }
115
        
116
        return $this->data = $this->fetch();
117
    }
118
119
    /**
120
     * Sets the chunk property. Used as an alias for array chunking. Primarily for views
121
     *
122
     * @param int $chunk
123
     * @return self
124
     */
125
    public function chunk($chunk)
126
    {
127
        $this->chunk = $chunk;
128
        return $this;
129
    }
130
131
    /**
132
     * Gets the paramters to be passed to the API request.
133
     *
134
     * @return array
135
     */
136
    private function getParams()
137
    {
138
        $params = [];
139
        $params['q'] = $this->query;
140
        $params['maxResults'] = $this->limit ?: 40;
141
142
        return $params;
143
    }
144
}
145