Completed
Push — 1.2 ( d3ea0d...d8c512 )
by David
16:21
created

SimplePager   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 16
c 1
b 1
f 1
lcom 1
cbo 3
dl 0
loc 143
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getResults() 0 15 3
A resetIterator() 0 5 1
A __construct() 0 5 1
A getNbResults() 0 8 2
A haveToPaginate() 0 4 2
B init() 0 25 5
A setThreshold() 0 4 1
A getThreshold() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Datagrid;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\ODM\PHPCR\Query\Query as PHPCRQuery;
16
17
class SimplePager extends Pager
18
{
19
    /**
20
     * @var  boolean
21
     */
22
    protected $haveToPaginate;
23
24
    /**
25
     * How many pages to look forward to create links to next pages.
26
     *
27
     * @var int
28
     */
29
    protected $threshold;
30
31
    /**
32
     * @var int
33
     */
34
    protected $thresholdCount;
35
36
    /**
37
     * The threshold parameter can be used to determine how far ahead the pager
38
     * should fetch results.
39
     *
40
     * If set to 1 which is the minimal value the pager will generate a link to the next page
41
     * If set to 2 the pager will generate links to the next two pages
42
     * If set to 3 the pager will generate links to the next three pages
43
     * etc.
44
     *
45
     * @param integer $maxPerPage Number of records to display per page
46
     * @param int $threshold
47
     */
48
    public function __construct($maxPerPage = 10, $threshold = 2)
49
    {
50
        parent::__construct($maxPerPage);
51
        $this->setThreshold($threshold);
52
    }
53
54
    /**
55
     * Returns the exact count when there is only one page or when the current
56
     * equals the last page.
57
     *
58
     * In all other cases an estimate of the total count is returned.
59
     *
60
     * @return integer
61
     */
62
    public function getNbResults()
63
    {
64
        $n = ceil(($this->getLastPage() -1) * $this->getMaxPerPage());
65
        if ($this->getLastPage() == $this->getPage()) {
66
            return $n + $this->thresholdCount;
67
        }
68
        return $n;
69
    }
70
71
    /**
72
     * Get all the results for the pager instance
73
     *
74
     * @param mixed $hydrationMode A hydration mode identifier
75
     *
76
     * @return array
77
     */
78
    public function getResults($hydrationMode = null)
79
    {
80
        if (!$this->results) {
81
            $this->results = $this->getQuery()->execute(array(), $hydrationMode);
82
            $this->thresholdCount = count($this->results);
83
            if (count($this->results) > $this->getMaxPerPage()) {
84
                $this->haveToPaginate = true;
85
                $this->results = new ArrayCollection($this->results->slice(0, $this->getMaxPerPage()));
86
            } else {
87
                $this->haveToPaginate = false;
88
            }
89
        }
90
91
        return $this->results;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function haveToPaginate()
98
    {
99
        return $this->haveToPaginate || $this->getPage() > 1;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    protected function resetIterator()
106
    {
107
        parent::resetIterator();
108
        $this->haveToPaginate = false;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     *
114
     * @throws \RuntimeException the QueryBuilder is uninitialized.
115
     */
116
    public function init()
117
    {
118
        if (!$this->getQuery()) {
119
            throw new \RuntimeException("Uninitialized QueryBuilder");
120
        }
121
        $this->resetIterator();
122
123
        if (0 == $this->getPage() || 0 == $this->getMaxPerPage()) {
124
            $this->setLastPage(0);
125
            $this->getQuery()->setFirstResult(0);
126
            $this->getQuery()->setMaxResults(0);
127
        } else {
128
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
129
            $this->getQuery()->setFirstResult($offset);
130
131
            $maxOffset = $this->getThreshold() > 0
132
                ? $this->getMaxPerPage() * $this->threshold + 1 : $this->getMaxPerPage() + 1;
133
134
            $this->getQuery()->setMaxResults($maxOffset);
135
            $this->initializeIterator();
136
137
            $t = (int) ceil($this->thresholdCount / $this->getMaxPerPage()) + $this->getPage() - 1;
138
            $this->setLastPage($t);
139
        }
140
    }
141
142
    /**
143
     * Set how many pages to look forward to create links to next pages.
144
     *
145
     * @param int $threshold
146
     */
147
    public function setThreshold($threshold)
148
    {
149
        $this->threshold = (int) $threshold;
150
    }
151
152
    /**
153
     * @return int
154
     */
155
    public function getThreshold()
156
    {
157
        return $this->threshold;
158
    }
159
}
160