DoctrineQueryPager   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 15
c 4
b 1
f 1
dl 0
loc 74
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getTotal() 0 7 2
A setRange() 0 3 1
A setCountQuery() 0 3 1
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\FrameworkExtraBundle\Doctrine;
7
8
use Zicht\Bundle\FrameworkExtraBundle\Pager\Pageable;
9
use Doctrine\ORM\QueryBuilder;
10
11
/**
12
 * Pageable for doctrine DQL queries
13
 */
14
class DoctrineQueryPager implements Pageable
15
{
16
    /**
17
     * @var QueryBuilder
18
     */
19
    protected $qb;
20
21
    /**
22
     * @var string
23
     */
24
    protected $alias;
25
26
    /**
27
     * @var string
28
     */
29
    protected $countAlias;
30
31
    /**
32
     * @var null
33
     */
34
    protected $countQuery;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param \Doctrine\ORM\QueryBuilder $q
40
     * @param string $alias
41
     * @param string $countAlias
42
     */
43
    public function __construct(QueryBuilder $q, $alias = 'f', $countAlias = '__count')
44
    {
45
        $this->qb = $q;
46
        $this->alias = $alias;
47
        $this->countAlias = $countAlias;
48
        $this->countQuery = null;
49
    }
50
51
52
    /**
53
     * Set the count query to override the default generated count query.
54
     *
55
     * @param \Doctrine\ORM\Query $countQuery
56
     * @return void
57
     */
58
    public function setCountQuery($countQuery)
59
    {
60
        $this->countQuery = $countQuery;
0 ignored issues
show
Documentation Bug introduced by
It seems like $countQuery of type Doctrine\ORM\Query is incompatible with the declared type null of property $countQuery.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
    }
62
63
64
    /**
65
     * Returns the absolute total of the pageable set of elements.
66
     *
67
     * @return int
68
     */
69
    public function getTotal()
70
    {
71
        if (!isset($this->countQuery)) {
72
            $c = clone $this->qb;
73
            $this->countQuery = $c->select('COUNT(' . $this->alias . ') ' . $this->countAlias)->getQuery();
0 ignored issues
show
Documentation Bug introduced by
It seems like $c->select('COUNT(' . $t...countAlias)->getQuery() of type Doctrine\ORM\Query is incompatible with the declared type null of property $countQuery.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
        }
75
        return $this->countQuery->getSingleScalarResult();
76
    }
77
78
    /**
79
     * Sets the range that needs to be displayed on the current page
80
     *
81
     * @param int $start
82
     * @param int $length
83
     * @return int
84
     */
85
    public function setRange($start, $length)
86
    {
87
        $this->qb->setFirstResult($start)->setMaxResults($length);
88
    }
89
}
90