Completed
Pull Request — 3.x (#360)
by
unknown
01:30
created

Pager::init()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Datagrid;
15
16
use Sonata\AdminBundle\Datagrid\Pager as BasePager;
17
18
/**
19
 * Doctrine pager class.
20
 *
21
 * @author Jonathan H. Wage <[email protected]>
22
 * @author Kévin Dunglas <[email protected]>
23
 */
24
class Pager extends BasePager
25
{
26
    protected $queryBuilder = null;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function computeNbResult()
32
    {
33
        $countQuery = clone $this->getQuery();
34
35
        if (\count($this->getParameters()) > 0) {
36
            $countQuery->setParameters($this->getParameters());
37
        }
38
39
        return $countQuery->count()->getQuery()->execute();
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getResults()
46
    {
47
        return $this->getQuery()->execute();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function init()
54
    {
55
        $this->resetIterator();
56
57
        $this->setNbResults($this->computeNbResult());
58
59
        $this->getQuery()->setFirstResult(0);
60
        $this->getQuery()->setMaxResults(0);
61
62
        if (\count($this->getParameters()) > 0) {
63
            $this->getQuery()->setParameters($this->getParameters());
64
        }
65
66
        if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->getNbResults()) {
67
            $this->setLastPage(0);
68
        } else {
69
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
70
71
            $this->setLastPage((int) ceil($this->getNbResults() / $this->getMaxPerPage()));
72
73
            $this->getQuery()->setFirstResult($offset);
74
            $this->getQuery()->setMaxResults($this->getMaxPerPage());
75
        }
76
    }
77
}
78