Pagination::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the Borobudur-Cqrs package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Cqrs\ReadModel\Storage\Finder;
12
13
/**
14
 * @author      Iqbal Maulana <[email protected]>
15
 * @created     3/25/16
16
 */
17
class Pagination
18
{
19
    /**
20
     * @var int
21
     */
22
    public $limit;
23
24
    /**
25
     * @var int
26
     */
27
    public $current;
28
29
    /**
30
     * @var int
31
     */
32
    public $prev = null;
33
34
    /**
35
     * @var int
36
     */
37
    public $next = null;
38
39
    /**
40
     * @var int
41
     */
42
    public $pages = 0;
43
44
    /**
45
     * @var int
46
     */
47
    public $offset = 0;
48
49
    /**
50
     * @var int
51
     */
52
    public $total;
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param int $limit
58
     * @param int $current
59
     */
60
    public function __construct($limit, $current)
61
    {
62
        $this->limit = (int) $limit;
63
        $this->current = (int) $current;
64
        $this->offset = ($this->current - 1) * $this->limit;
65
66
        if (1 !== $this->current) {
67
            $this->prev = $current - 1;
68
        }
69
    }
70
71
    /**
72
     * @param int $total
73
     */
74
    public function calc($total)
75
    {
76
        $this->pages = ceil($total / $this->limit);
0 ignored issues
show
Documentation Bug introduced by
The property $pages was declared of type integer, but ceil($total / $this->limit) 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...
77
        $this->total = $total;
78
79
        if ($this->current < $this->pages) {
80
            $this->next = $this->current + 1;
81
        }
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function serialize()
88
    {
89
        return array(
90
            'prev'    => $this->prev,
91
            'current' => $this->current,
92
            'next'    => $this->next,
93
            'pages'   => $this->pages,
94
            'total'   => $this->total,
95
        );
96
    }
97
}
98