Completed
Push — master ( 32f37c...b12769 )
by Dan Michael O.
04:22
created

SimplePaginatedList::fetchBatch()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Model;
4
5
use Scriptotek\Alma\Exception\ClientException;
6
7
/**
8
 * A SimplePaginatedList is a list that is paged using the `offset`
9
 * and `limit` parameters and that provides a `totalRecordCount` in the first response,
10
 * so that we can return a count without having to retrieve all the pages.
11
 * A list which is not of this type is the Analytics report row list.
12
 */
13
abstract class SimplePaginatedList extends LazyResourceList
14
{
15
    use PaginatedList;
16
17
    /* @var integer */
18
    protected $offset = 0;
19
20
    /* @var integer */
21
    protected $limit = 10;
22
23
24
    protected function fetchBatch()
25
    {
26
        if (!is_null($this->totalRecordCount) && $this->offset >= $this->totalRecordCount) {
27
            return;
28
        }
29
30
        $response = $this->client->getJSON($this->url('', [
31
            'offset' => $this->offset,
32
            'limit' => $this->limit,
33
        ]));
34
        return $this->onData($response);
35
    }
36
37
    protected function fetchData()
38
    {
39
        do {
40
            $this->fetchBatch();
41
        } while (!$this->isInitialized($this->resources));
0 ignored issues
show
Documentation introduced by
$this->resources is of type array, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
        return null;
43
    }
44
45
    protected function onData($data)
46
    {
47
        parent::onData($data);
48
        $this->offset = count($this->resources);
49
    }
50
51
    /**
52
     * Check if we have the full representation of our data object.
53
     *
54
     * @param \stdClass $data
55
     * @return boolean
56
     */
57
    protected function isInitialized($data)
58
    {
59
        return count($data) === $this->totalRecordCount;
60
    }
61
62
    /**
63
     * Total number of resources.
64
     * @link http://php.net/manual/en/countable.count.php
65
     * @return int
66
     */
67
    public function count()
68
    {
69
        if (is_null($this->totalRecordCount)) {
70
            $this->fetchBatch();
71
        }
72
73
        return $this->totalRecordCount;
74
    }
75
}
76