Pager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A computeNbResult() 0 10 2
A getResults() 0 4 1
A getQuery() 0 4 1
A init() 0 24 5
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 getQuery()
54
    {
55
        return $this->query;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function init(): void
62
    {
63
        $this->resetIterator();
64
65
        $this->setNbResults($this->computeNbResult());
66
67
        $this->getQuery()->setFirstResult(0);
68
        $this->getQuery()->setMaxResults(0);
69
70
        if (\count($this->getParameters()) > 0) {
71
            $this->getQuery()->setParameters($this->getParameters());
72
        }
73
74
        if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->getNbResults()) {
75
            $this->setLastPage(0);
76
        } else {
77
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
78
79
            $this->setLastPage((int) ceil($this->getNbResults() / $this->getMaxPerPage()));
80
81
            $this->getQuery()->setFirstResult($offset);
82
            $this->getQuery()->setMaxResults($this->getMaxPerPage());
83
        }
84
    }
85
}
86