Completed
Push — master ( 9137df...84432d )
by
unknown
09:58
created

Pageable::offset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 6/15/15
6
 * Time: 11:08 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Foundation\Domain\Model\Repository;
12
13
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields as FieldsInterface;
14
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter as FilterInterface;
15
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable as PageableInterface;
16
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort as SortInterface;
17
18
class Pageable implements PageableInterface
19
{
20
    /**
21
     * @var int
22
     */
23
    protected $pageNumber;
24
    /**
25
     * @var int
26
     */
27
    protected $pageSize;
28
    /**
29
     * @var Sort
30
     */
31
    protected $sort;
32
    /**
33
     * @var FilterInterface
34
     */
35
    protected $filter;
36
37
    /**
38
     * @var FieldsInterface
39
     */
40
    protected $fields;
41
42
    /**
43
     * Pageable constructor.
44
     *
45
     * @param                      $pageNumber
46
     * @param                      $pageSize
47
     * @param SortInterface|null   $sort
48
     * @param FilterInterface|null $filter
49
     * @param FieldsInterface|null $fields
50
     */
51
    public function __construct(
52
        $pageNumber,
53
        $pageSize,
54
        SortInterface $sort = null,
55
        FilterInterface $filter = null,
56
        FieldsInterface $fields = null
57
    ) {
58
        $this->pageNumber = (int) $pageNumber;
59
        $this->pageSize = (int) $pageSize;
60
        $this->sort = $sort;
0 ignored issues
show
Documentation Bug introduced by
It seems like $sort can also be of type object<NilPortugues\Foun...ository\Contracts\Sort>. However, the property $sort is declared as type object<NilPortugues\Foun...\Model\Repository\Sort>. 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...
61
        $this->filter = $filter;
62
        $this->fields = $fields;
63
    }
64
65
    /**
66
     * Returns the offset to be taken according to the underlying page and page size.
67
     *
68
     * @return int
69
     */
70
    public function offset()
71
    {
72
        $offset = $this->pageNumber();
73
74
        return ($offset > 0) ? ($offset) * $this->pageSize : $this->pageSize;
75
    }
76
77
    /**
78
     * Returns the page to be returned.
79
     *
80
     * @return int
81
     */
82
    public function pageNumber()
83
    {
84
        return ($this->pageNumber < 1) ? 1 : $this->pageNumber;
85
    }
86
87
    /**
88
     * Returns the sorting parameters.
89
     *
90
     * @return SortInterface
91
     */
92
    public function sortings()
93
    {
94
        return $this->sort;
95
    }
96
97
    /**
98
     * Returns the Pageable requesting the next Page.
99
     *
100
     * @return PageableInterface
101
     */
102
    public function next()
103
    {
104
        return new self($this->pageNumber() + 1, $this->pageSize(), $this->sort, $this->filter, $this->fields);
105
    }
106
107
    /**
108
     * Returns the number of items to be returned.
109
     *
110
     * @return int
111
     */
112
    public function pageSize()
113
    {
114
        return $this->pageSize;
115
    }
116
117
    /**
118
     * Returns the previous Pageable or the first Pageable if the current one already is the first one.
119
     *
120
     * @return PageableInterface
121
     */
122
    public function previousOrFirst()
123
    {
124
        if ($this->hasPrevious()) {
125
            return new self($this->pageNumber() - 1, $this->pageSize(), $this->sort, $this->filter,
126
                $this->fields);
127
        }
128
129
        return $this->first();
130
    }
131
132
    /**
133
     * Returns whether there's a previous Pageable we can access from the current one.
134
     *
135
     * @return bool
136
     */
137
    public function hasPrevious()
138
    {
139
        return ($this->pageNumber() - 1) > 0;
140
    }
141
142
    /**
143
     * Returns the Pageable requesting the first page.
144
     *
145
     * @return PageableInterface
146
     */
147
    public function first()
148
    {
149
        return new self(1, $this->pageSize(), $this->sort, $this->filter, $this->fields);
150
    }
151
152
    /**
153
     * @return FilterInterface
154
     */
155
    public function filters()
156
    {
157
        return $this->filter;
158
    }
159
160
    /**
161
     * @return Fields
162
     */
163
    public function fields()
164
    {
165
        return $this->fields;
166
    }
167
}
168