Failed Conditions
Pull Request — 2.6 (#7882)
by
unknown
06:45
created

CountWalker::walkSelectStatement()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4.0105

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 39
ccs 21
cts 23
cp 0.913
rs 9.568
cc 4
nc 4
nop 1
crap 4.0105
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools\Pagination;
21
22
use Doctrine\ORM\Query\TreeWalkerAdapter;
23
use Doctrine\ORM\Query\AST\SelectStatement;
24
use Doctrine\ORM\Query\AST\SelectExpression;
25
use Doctrine\ORM\Query\AST\PathExpression;
26
use Doctrine\ORM\Query\AST\AggregateExpression;
27
28
/**
29
 * Replaces the selectClause of the AST with a COUNT statement.
30
 *
31
 * @category    DoctrineExtensions
32
 * @package     DoctrineExtensions\Paginate
33
 * @author      David Abdemoulaie <[email protected]>
34
 * @copyright   Copyright (c) 2010 David Abdemoulaie (http://hobodave.com/)
35
 * @license     http://hobodave.com/license.txt New BSD License
36
 */
37
class CountWalker extends TreeWalkerAdapter
38
{
39
    /**
40
     * Distinct mode hint name.
41
     */
42
    const HINT_DISTINCT = 'doctrine_paginator.distinct';
43
44
    /**
45
     * Walks down a SelectStatement AST node, modifying it to retrieve a COUNT.
46
     *
47
     * @param SelectStatement $AST
48
     *
49
     * @return void
50
     *
51
     * @throws \RuntimeException
52
     */
53 8
    public function walkSelectStatement(SelectStatement $AST)
54
    {
55 8
        if ($AST->havingClause) {
56 1
            throw new \RuntimeException('Cannot count query that uses a HAVING clause. Use the output walkers for pagination');
57
        }
58
59 7
        $queryComponents = $this->_getQueryComponents();
60
        // Get the root entity and alias from the AST fromClause
61 7
        $from = $AST->fromClause->identificationVariableDeclarations;
62
63 7
        if (count($from) > 1) {
64
            throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
65
        }
66
67 7
        $fromRoot            = reset($from);
68 7
        $rootAlias           = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable;
69 7
        $rootClass           = $queryComponents[$rootAlias]['metadata'];
70 7
        $identifierFieldName = $rootClass->getSingleIdentifierFieldName();
71
72 7
        $pathType = PathExpression::TYPE_STATE_FIELD;
73 7
        if (isset($rootClass->associationMappings[$identifierFieldName])) {
74
            $pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
75
        }
76
77 7
        $pathExpression = new PathExpression(
78 7
            PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias,
79 7
            $identifierFieldName
80
        );
81 7
        $pathExpression->type = $pathType;
82
83 7
        $distinct = $this->_getQuery()->getHint(self::HINT_DISTINCT);
84 7
        $AST->selectClause->selectExpressions = [
85 7
            new SelectExpression(
86 7
                new AggregateExpression('count', $pathExpression, $distinct), null
87
            )
88
        ];
89
90
        // ORDER BY is not needed, only increases query execution through unnecessary sorting.
91 7
        $AST->orderByClause = null;
92 7
    }
93
}
94
95