Pager::computeNbResult()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
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
    public function computeNbResult()
29
    {
30
        $countQuery = clone $this->getQuery();
31
32
        if (\count($this->getParameters()) > 0) {
33
            $countQuery->setParameters($this->getParameters());
34
        }
35
36
        return $countQuery->count()->getQuery()->execute();
37
    }
38
39
    public function getResults()
40
    {
41
        return $this->getQuery()->execute();
42
    }
43
44
    public function init(): void
45
    {
46
        $this->resetIterator();
47
48
        $this->setNbResults($this->computeNbResult());
49
50
        $this->getQuery()->setFirstResult(0);
51
        $this->getQuery()->setMaxResults(0);
52
53
        if (\count($this->getParameters()) > 0) {
54
            $this->getQuery()->setParameters($this->getParameters());
55
        }
56
57
        if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->getNbResults()) {
58
            $this->setLastPage(0);
59
        } else {
60
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
61
62
            $this->setLastPage((int) ceil($this->getNbResults() / $this->getMaxPerPage()));
63
64
            $this->getQuery()->setFirstResult($offset);
65
            $this->getQuery()->setMaxResults($this->getMaxPerPage());
66
        }
67
    }
68
}
69