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

Pager::computeNbResult()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 4
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\AbstractQuery;
15
use Doctrine\ORM\Query;
16
use Doctrine\ORM\Tools\Pagination\CountWalker;
17
use Sonata\AdminBundle\Datagrid\Pager as BasePager;
18
19
/**
20
 * Doctrine pager class.
21
 *
22
 * @author Jonathan H. Wage <[email protected]>
23
 */
24
class Pager extends BasePager
25
{
26
    /**
27
     * NEXT_MAJOR: remove this property.
28
     *
29
     * @deprecated This property is deprecated since version 2.4 and will be removed in 3.0
30
     */
31
    protected $queryBuilder = null;
32
33
    public function computeNbResult()
34
    {
35
        $countQuery = clone $this->getQuery();
36
37
        if (count($this->getParameters()) > 0) {
38
            $countQuery->setParameters($this->getParameters());
39
        }
40
41
        $query = $countQuery->getQuery();
42
        $isDistinct = $countQuery instanceof ProxyQuery ? $countQuery->isDistinct() : true;
43
        $query->setHint(CountWalker::HINT_DISTINCT, $isDistinct);
44
        $this->appendTreeWalker($query, CountWalker::class);
45
46
        return $query->getSingleScalarResult();
47
    }
48
49
    public function getResults($hydrationMode = Query::HYDRATE_OBJECT)
50
    {
51
        return $this->getQuery()->execute([], $hydrationMode);
52
    }
53
54
    public function getQuery()
55
    {
56
        return $this->query;
57
    }
58
59
    public function init()
60
    {
61
        $this->resetIterator();
62
63
        $this->setNbResults($this->computeNbResult());
64
65
        $this->getQuery()->setFirstResult(null);
66
        $this->getQuery()->setMaxResults(null);
67
68
        if (count($this->getParameters()) > 0) {
69
            $this->getQuery()->setParameters($this->getParameters());
70
        }
71
72
        if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) {
73
            $this->setLastPage(0);
74
        } else {
75
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
76
77
            $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
78
79
            $this->getQuery()->setFirstResult($offset);
80
            $this->getQuery()->setMaxResults($this->getMaxPerPage());
81
        }
82
    }
83
84
    /**
85
     * Appends a custom tree walker to the tree walkers hint.
86
     *
87
     * @param AbstractQuery  $query
88
     * @param string         $walkerClass
89
     */
90
    private function appendTreeWalker(AbstractQuery $query, $walkerClass)
91
    {
92
        $hints = $query->getHint(Query::HINT_CUSTOM_TREE_WALKERS);
93
94
        if (false === $hints) {
95
            $hints = [];
96
        }
97
98
        $hints[] = $walkerClass;
99
        $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $hints);
100
    }
101
}
102