AbstractPaginator   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
eloc 38
c 6
b 2
f 0
dl 0
loc 162
ccs 0
cts 48
cp 0
rs 10
wmc 18

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getPage() 0 3 1
A getPages() 0 3 1
A count() 0 3 1
A getItemsPerPage() 0 3 1
A paginate() 0 7 1
A setQuery() 0 3 1
A getCount() 0 6 2
A setItemsPerPage() 0 6 2
A getCountQuery() 0 7 1
A getLimitStart() 0 3 1
A doCount() 0 14 3
A getQuery() 0 3 1
A setPage() 0 6 2
1
<?php
2
3
namespace Nip\Records\Navigator\Pagination;
4
5
use Nip\Database\Query\Select as SelectQuery;
6
7
/**
8
 * Class AbstractPaginator
9
 * @package Nip\Records\Navigator\Pagination
10
 */
11
abstract class AbstractPaginator
12
{
13
14
    /**
15
     * @var SelectQuery
16
     */
17
    protected $query = null;
18
19
    /**
20
     * @var int
21
     */
22
    protected $page = 1;
23
24
    /**
25
     * @var int
26
     */
27
    protected $itemsPerPage = 20;
28
29
    /**
30
     * @var null|int
31
     */
32
    protected $count = null;
33
34
    /**
35
     * @var int
36
     */
37
    protected $pages;
38
39
    /**
40
     * @param SelectQuery $query
41
     * @return SelectQuery
42
     */
43
    public function paginate($query)
44
    {
45
        $query->limit($this->getLimitStart(), $this->getItemsPerPage());
0 ignored issues
show
Bug introduced by
$this->getItemsPerPage() of type integer is incompatible with the type boolean expected by parameter $offset of Nip\Database\Query\AbstractQuery::limit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $query->limit($this->getLimitStart(), /** @scrutinizer ignore-type */ $this->getItemsPerPage());
Loading history...
46
47
        $this->setQuery($query);
48
49
        return $query;
50
    }
51
52
    /**
53
     * @return SelectQuery
54
     */
55
    public function getQuery()
56
    {
57
        return $this->query;
58
    }
59
60
    /**
61
     * @param SelectQuery $query
62
     */
63
    public function setQuery($query)
64
    {
65
        $this->query = $query;
66
    }
67
68
    /**
69
     * @deprecated Method is called automatically on getCount
70
     */
71
    public function count()
72
    {
73
        $this->getCount();
74
    }
75
76
    /**
77
     * Does the count for all records
78
     */
79
    protected function doCount()
80
    {
81
        $query = $this->getCountQuery();
82
        $result = $query->execute()->fetchResult();
83
84
        $this->count = intval($result['count']);
85
        $this->pages = intval($this->count / $this->itemsPerPage);
86
87
        if ($this->count % $this->itemsPerPage != 0) {
88
            $this->pages++;
89
        }
90
91
        if ($this->pages == 0) {
92
            $this->pages = 1;
93
        }
94
    }
95
96
    /**
97
     * @return SelectQuery
98
     */
99
    protected function getCountQuery()
100
    {
101
        $query = clone $this->getQuery();
102
        $query->setCols();
0 ignored issues
show
Bug introduced by
The call to Nip\Database\Query\AbstractQuery::setCols() has too few arguments starting with |. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
        $query->/** @scrutinizer ignore-call */ 
103
                setCols();

This check compares calls to functions or methods with their respective definitions. If the call has less 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. Please note the @ignore annotation hint above.

Loading history...
103
        $query->count('*', 'count');
104
        $query->limit(1);
105
        return $query;
106
    }
107
108
    /**
109
     * @param bool $page
110
     * @return $this
111
     */
112
    public function setPage($page = false)
113
    {
114
        if ($page) {
115
            $this->page = $page;
0 ignored issues
show
Documentation Bug introduced by
The property $page was declared of type integer, but $page is of type true. 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...
116
        }
117
        return $this;
118
    }
119
120
    /**
121
     * @return int
122
     */
123
    public function getPage()
124
    {
125
        return $this->page;
126
    }
127
128
    /**
129
     * @return mixed
130
     */
131
    public function getPages()
132
    {
133
        return $this->pages;
134
    }
135
136
    /**
137
     * @param $items
138
     * @return $this
139
     */
140
    public function setItemsPerPage($items)
141
    {
142
        if ($items > 0) {
143
            $this->itemsPerPage = $items;
144
        }
145
        return $this;
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function getItemsPerPage()
152
    {
153
        return $this->itemsPerPage;
154
    }
155
156
    /**
157
     * @return float|int
158
     */
159
    public function getLimitStart()
160
    {
161
        return ($this->getPage() - 1) * $this->getItemsPerPage();
162
    }
163
164
    /**
165
     * @return int
166
     */
167
    public function getCount()
168
    {
169
        if ($this->count === null) {
170
            $this->doCount();
171
        }
172
        return $this->count;
173
    }
174
}
175