Completed
Push — 1.x ( e997bd )
by Sullivan
19:38
created

Pager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A computeNbResult() 0 4 1
A getResults() 0 4 1
B init() 0 20 5
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 Sonata\AdminBundle\Datagrid\Pager as BasePager;
15
use Doctrine\ODM\PHPCR\Query\Query as PHPCRQuery;
16
17
/**
18
 * Doctrine pager class.
19
 *
20
 * @author     Jonathan H. Wage <[email protected]>
21
 * @author     Nacho Martin <[email protected]>
22
 */
23
class Pager extends BasePager
24
{
25
    /**
26
     * Returns a query for counting the total results.
27
     *
28
     * @return integer
29
     */
30
    public function computeNbResult()
31
    {
32
        return count($this->getQuery()->execute(array(), PHPCRQuery::HYDRATE_PHPCR));
33
    }
34
35
    /**
36
     * Get all the results for the pager instance
37
     *
38
     * @param mixed $hydrationMode A hydration mode identifier
39
     *
40
     * @return array
41
     */
42
    public function getResults($hydrationMode = null)
43
    {
44
        return $this->getQuery()->execute(array(), $hydrationMode);
45
    }
46
47
    /**
48
     * Initializes the pager setting the offset and maxResults in ProxyQuery
49
     * and obtaining the total number of pages.
50
     *
51
     * @throws \RuntimeException the QueryBuilder is uninitialized.
52
     */
53
    public function init()
54
    {
55
        if (!$this->getQuery()) {
56
            throw new \RuntimeException("Uninitialized QueryBuilder");
57
        }
58
59
        $this->resetIterator();
60
        $this->setNbResults($this->computeNbResult());
61
62
        if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) {
63
            $this->setLastPage(0);
64
            $this->getQuery()->setFirstResult(0);
65
            $this->getQuery()->setMaxResults(0);
66
        } else {
67
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
68
            $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
69
            $this->getQuery()->setFirstResult($offset);
70
            $this->getQuery()->setMaxResults($this->getMaxPerPage());
71
        }
72
    }
73
}
74