Completed
Pull Request — 3.x (#740)
by
unknown
01:46
created

Pager::init()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 6.7272
cc 7
eloc 20
nc 8
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project 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\DoctrineORMAdminBundle\Datagrid;
13
14
use Doctrine\ORM\Query;
15
use Sonata\AdminBundle\Datagrid\Pager as BasePager;
16
17
/**
18
 * Doctrine pager class.
19
 *
20
 * @author Jonathan H. Wage <[email protected]>
21
 */
22
class Pager extends BasePager
23
{
24
    /**
25
     * NEXT_MAJOR: remove this property.
26
     *
27
     * @deprecated This property is deprecated since version 2.4 and will be removed in 3.0
28
     */
29
    protected $queryBuilder = null;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function computeNbResult()
35
    {
36
        $countQuery = clone $this->getQuery();
37
38
        if (count($this->getParameters()) > 0) {
39
            $countQuery->setParameters($this->getParameters());
40
        }
41
42
        $countQuery->select(sprintf(
43
            'count(DISTINCT %s.%s) as cnt',
44
            $countQuery->getRootAlias(),
45
            current($this->getCountColumn())
46
        ));
47
48
        return $countQuery->resetDQLPart('orderBy')->getQuery()->getSingleScalarResult();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getResults($hydrationMode = Query::HYDRATE_OBJECT)
55
    {
56
        return $this->getQuery()->execute([], $hydrationMode);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getQuery()
63
    {
64
        return $this->query;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function init()
71
    {
72
        $this->resetIterator();
73
74
        $this->setNbResults($this->computeNbResult());
75
76
        $this->getQuery()->setFirstResult(null);
77
        $this->getQuery()->setMaxResults(null);
78
79
        if (count($this->getParameters()) > 0) {
80
            $this->getQuery()->setParameters($this->getParameters());
81
        }
82
83
        if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) {
84
            $this->setLastPage(0);
85
        } else {
86
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
87
88
            $lastPage = ceil($this->getNbResults() / $this->getMaxPerPage());
89
            if ($this->getNbResults() <= $offset) {
90
                $resultsOnLastPage = $this->getNbResults() - $lastPage * $this->getMaxPerPage();
91
                $lastPage = $resultsOnLastPage > 0 ? $lastPage + 1 : $lastPage;
92
93
                $this->setPage(1);
94
                $offset = 1;
95
            }
96
97
            $this->setLastPage($lastPage);
98
99
            $this->getQuery()->setFirstResult($offset);
100
            $this->getQuery()->setMaxResults($this->getMaxPerPage());
101
        }
102
    }
103
}
104