Completed
Push — master ( a9d0a0...c83be6 )
by Filipe
07:24
created

Pagination::setTotal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Utils;
11
12
use Slick\Common\Base;
13
use Slick\Filter\StaticFilter;
14
use Slick\Http\PhpEnvironment\Request;
15
use Slick\Validator\StaticValidator;
16
17
/**
18
 * Pagination utility
19
 *
20
 * @package Slick\Mvc\Utils
21
 * @author  Filipe Silva <[email protected]>
22
 *
23
 * @property Request $request
24
 * @property integer $offset
25
 * @property integer $rowsPerPage
26
 * @property integer $pages
27
 * @property integer $total
28
 * @property integer $current
29
 */
30
class Pagination extends Base
31
{
32
33
    /**
34
     * @readwrite
35
     * @var int Total pages
36
     */
37
    protected $pages = 0;
38
    /**
39
     * @readwrite
40
     * @var int Total records
41
     */
42
    protected $total;
43
    /**
44
     * @readwrite
45
     * @var int current page index
46
     */
47
    protected $current = 1;
48
    /**
49
     * @readwrite
50
     * @var int total rows per page
51
     */
52
    protected $rowsPerPage = 12;
53
    /**
54
     * @readwrite
55
     * @var int First row to return
56
     */
57
    protected $offset = 0;
58
    /**
59
     * @readwrite
60
     * @var Request
61
     */
62
    protected $request;
63
64
    /**
65
     * Overrides the constructor to calculate the properties for current
66
     * pagination state.
67
     *
68
     * @param array $options
69
     */
70 18
    public function __construct($options = array())
71
    {
72 18
        parent::__construct($options);
73 18
        if ($this->getRequest()->getQuery('rows')) {
74 10
            $this->setRowsPerPage($this->getRequest()->getQuery('rows'));
75 5
        }
76 18
        if ($this->getRequest()->getQuery('page')) {
77 10
            $this->setCurrent($this->getRequest()->getQuery('page'));
78 5
        }
79 18
    }
80
81
    /**
82
     * Lazy loads request object
83
     *
84
     * @return Request
85
     */
86 18
    public function getRequest()
87
    {
88 18
        if (is_null($this->request)) {
89 6
            $this->request = new Request();
90 3
        }
91 18
        return $this->request;
92
    }
93
    
94
    /**
95
     * Sets current page and calculates offset
96
     *
97
     * @param integer $value The number of the current page.
98
     *
99
     * @return Pagination A self instance for method chain calls
100
     */
101 10 View Code Duplication
    public function setCurrent($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103 10
        if (StaticValidator::validates('number', $value)) {
104 10
            $this->current = StaticFilter::filter('number',$value);
105 10
            $this->offset = $this->rowsPerPage * ($this->current - 1);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->rowsPerPage * ($this->current - 1) can also be of type double. However, the property $offset is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
106 5
        }
107 10
        return $this;
108
    }
109
    
110
    /**
111
     * Sets the total rows to paginate.
112
     *
113
     * @param integer $value The rows total to set.
114
     *
115
     * @return Pagination A self instance for method chain calls
116
     */
117 4 View Code Duplication
    public function setTotal($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119 4
        if (StaticValidator::validates('number', $value)) {
120 4
            $this->total = StaticFilter::filter('number',$value);
121 4
            $this->pages = ceil($this->total / $this->rowsPerPage);
0 ignored issues
show
Documentation Bug introduced by
The property $pages was declared of type integer, but ceil($this->total / $this->rowsPerPage) is of type double. 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...
122 2
        }
123 4
        return $this;
124
    }
125
    
126
    /**
127
     * Sets the total rows per page.
128
     *
129
     * @param integer $value The total rows per page to set.
130
     *
131
     * @return Pagination A self instance for method chain calls
132
     */
133 14 View Code Duplication
    public function setRowsPerPage($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
    {
135 14
        if (StaticValidator::validates('number', $value)) {
136 14
            $this->rowsPerPage = StaticFilter::filter('number',$value);
137 14
            $this->pages = ceil($this->total / $this->rowsPerPage);
0 ignored issues
show
Documentation Bug introduced by
The property $pages was declared of type integer, but ceil($this->total / $this->rowsPerPage) is of type double. 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...
138 7
        }
139 14
        return $this;
140
    }
141
    
142
    /**
143
     * Creates a request query for the provided page.
144
     *
145
     * This method check the current request query in order to maintain the
146
     * other parameters unchanged and sets the 'page' parameter to the
147
     * provided page number.
148
     *
149
     * @param integer $page The page number to build the query on.
150
     *
151
     * @return string The query string to use in the pagination links.
152
     */
153 2
    public function pageUrl($page)
154
    {
155 2
        $params = $this->getRequest()->getQuery();
156 2
        if (isset($params['url']))
157 1
            unset($params['url']);
158 2
        $params['page'] = $page;
159 2
        return '?' . http_build_query($params);
160
    }
161
}