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

Volumes::fetchSearch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Scriptotek\GoogleBooks;
4
5
6
class Volumes
7
{
8
    protected $client;
9
    protected $chunk = false;
10
11
    public function __construct(GoogleBooks $client)
12
    {
13
        $this->client = $client;
14
    }
15
16
    /**
17
     * @param $query
18
     * @return \Generator|Volume || array
19
     */
20
    public function search($query)
21
    {
22
        if ($this->chunk) {
23
            return $this->client->chunk($this->fetchSearch($query), $this->chunk);
24
        }
25
        return $this->fetchSearch($query);
26
    }
27
28
    /**
29
     * @param $query
30
     * @return \Generator|Volume
31
     */
32
    private function fetchSearch($query)
33
    {
34
        foreach ($this->client->listItems('volumes', ['q' => $query]) as $item) {
35
            yield new Volume($this->client, $item);
36
        }
37
    }
38
39
    /**
40
     * @param $chunk
41
     * @return Volumes
42
     */
43
    public function chunk(int $chunk)
44
    {
45
        $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...
46
        return $this;
47
    }
48
49
    public function firstOrNull($query)
50
    {
51
        $res = $this->search($query)->current();
52
        return $res;
53
    }
54
55
    public function get($id)
56
    {
57
        return new Volume($this->client, $this->client->getItem("volumes/$id"), true);
58
    }
59
60
    public function byIsbn($isbn)
61
    {
62
        $isbn = preg_replace('/[^0-9Xx]/', '', $isbn);
63
64
        return $this->firstOrNull('isbn:' . $isbn);
65
    }
66
}
67