Completed
Push — master ( 6c3bf7...0f5529 )
by Kevin
02:05
created

CallbackPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace Zenstruck\Porpaginas\Callback;
4
5
use Zenstruck\Porpaginas\Page;
6
7
/**
8
 * @author Kevin Bond <[email protected]>
9
 */
10
final class CallbackPage implements Page
11
{
12
    private $resultCallback;
13
    private $totalCountCallback;
14
    private $offset;
15
    private $limit;
16
    private $totalCount;
17
    private $results;
18
19
    public function __construct(callable $resultCallback, callable $totalCountCallback, $offset, $limit)
20
    {
21
        $this->resultCallback = $resultCallback;
22
        $this->totalCountCallback = $totalCountCallback;
23
        $this->offset = $offset;
24
        $this->limit = $limit;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getCurrentOffset()
31
    {
32
        return $this->offset;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getCurrentPage()
39
    {
40
        return floor($this->offset / $this->limit) + 1;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getCurrentLimit()
47
    {
48
        return $this->limit;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function count()
55
    {
56
        return count($this->getResults());
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function totalCount()
63
    {
64
        if ($this->totalCount !== null) {
65
            return $this->totalCount;
66
        }
67
68
        return $this->totalCount = call_user_func($this->totalCountCallback);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getIterator()
75
    {
76
        return $this->getResults();
77
    }
78
79
    /**
80
     * @return \ArrayIterator
81
     */
82
    private function getResults()
83
    {
84
        if ($this->results !== null) {
85
            return $this->results;
86
        }
87
88
        return $this->results = new \ArrayIterator(call_user_func($this->resultCallback, $this->offset, $this->limit));
89
    }
90
}
91