Completed
Pull Request — master (#1263)
by Andreas
11:16
created

Builder::hydrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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\ODM\MongoDB\Aggregation;
21
22
use Doctrine\MongoDB\Aggregation\Builder as BaseBuilder;
23
use Doctrine\MongoDB\CommandCursor as BaseCommandCursor;
24
use Doctrine\ODM\MongoDB\CommandCursor;
25
use Doctrine\ODM\MongoDB\DocumentManager;
26
use Doctrine\ODM\MongoDB\Query\Expr as QueryExpr;
27
28
/**
29
 * Fluent interface for building aggregation pipelines.
30
 *
31
 * @author alcaeus <[email protected]>
32
 */
33
class Builder extends BaseBuilder
34
{
35
    /**
36
     * The DocumentManager instance for this query
37
     *
38
     * @var DocumentManager
39
     */
40
    private $dm;
41
42
    /**
43
     * The ClassMetadata instance.
44
     *
45
     * @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata
46
     */
47
    private $class;
48
49
    /**
50
     * @var string
51
     */
52
    private $hydrationClass;
53
54
    /**
55
     * Create a new aggregation builder.
56
     *
57
     * @param DocumentManager $dm
58
     * @param string $documentName
59
     */
60 4
    public function __construct(DocumentManager $dm, $documentName)
61
    {
62 4
        $this->dm = $dm;
63 4
        $this->class = $this->dm->getClassMetadata($documentName);
64
65 4
        parent::__construct($this->dm->getDocumentCollection($documentName));
66 4
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function execute($options = array())
72
    {
73
        // Force cursor to be used
74 1
        $options = array_merge($options, array('cursor' => true));
75
76 1
        $cursor = parent::execute($options);
77
78 1
        return $this->prepareCursor($cursor);
0 ignored issues
show
Compatibility introduced by
$cursor of type object<Doctrine\MongoDB\Iterator> is not a sub-type of object<Doctrine\MongoDB\CommandCursor>. It seems like you assume a concrete implementation of the interface Doctrine\MongoDB\Iterator to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
79
    }
80
81
    /**
82
     * @param string $className
83
     *
84
     * @return self
85
     */
86 1
    public function hydrate($className)
87
    {
88 1
        $this->hydrationClass = $className;
89
90 1
        return $this;
91
    }
92
93
    /**
94
     * @return QueryExpr
95
     */
96 1
    public function matchExpr()
97
    {
98 1
        $expr = new QueryExpr($this->dm);
99 1
        $expr->setClassMetadata($this->class);
100
101 1
        return $expr;
102
    }
103
104
    /**
105
     * @return Expr
106
     */
107 2
    public function expr()
108
    {
109 2
        return new Expr($this->dm, $this->class);
110
    }
111
112
    /**
113
     * @param BaseCommandCursor $cursor
114
     *
115
     * @return CommandCursor
116
     */
117 1
    protected function prepareCursor(BaseCommandCursor $cursor)
118
    {
119 1
        $class = null;
120 1
        if ($this->hydrationClass) {
121 1
            $class = $this->dm->getClassMetadata($this->hydrationClass);
122
        }
123
124 1
        return new CommandCursor($cursor, $this->dm->getUnitOfWork(), $class);
125
    }
126
}
127