Completed
Pull Request — master (#1670)
by
unknown
09:01
created

Builder   B

Complexity

Total Complexity 23

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 16
dl 0
loc 234
ccs 56
cts 60
cp 0.9333
rs 8.4614
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A bucket() 0 4 1
A bucketAuto() 0 4 1
A match() 0 4 1
A out() 0 4 1
A replaceRoot() 0 4 1
A sortByCount() 0 4 1
A sort() 0 5 2
A unwind() 0 5 1
A getPipeline() 0 15 3
A applyFilters() 0 9 1
A getDocumentPersister() 0 4 1
A graphLookup() 0 4 1
A __construct() 0 7 1
A execute() 0 9 1
A hydrate() 0 6 1
A matchExpr() 0 7 1
A expr() 0 4 1
A prepareCursor() 0 9 2
A lookup() 0 4 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\Aggregation\Stage\GeoNear;
24
use Doctrine\MongoDB\CommandCursor as BaseCommandCursor;
25
use Doctrine\ODM\MongoDB\CommandCursor;
26
use Doctrine\ODM\MongoDB\DocumentManager;
27
use Doctrine\ODM\MongoDB\Query\Expr as QueryExpr;
28
29
/**
30
 * Fluent interface for building aggregation pipelines.
31
 */
32
class Builder extends BaseBuilder
33
{
34
    /**
35
     * The DocumentManager instance for this query
36
     *
37
     * @var DocumentManager
38
     */
39
    private $dm;
40
41
    /**
42
     * The ClassMetadata instance.
43
     *
44
     * @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata
45
     */
46
    private $class;
47
48
    /**
49
     * @var string
50
     */
51
    private $hydrationClass;
52
53
    /**
54
     * Create a new aggregation builder.
55
     *
56
     * @param DocumentManager $dm
57
     * @param string $documentName
58
     */
59 18
    public function __construct(DocumentManager $dm, $documentName)
60
    {
61 18
        $this->dm = $dm;
62 18
        $this->class = $this->dm->getClassMetadata($documentName);
63
64 18
        parent::__construct($this->dm->getDocumentCollection($documentName));
65 18
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 2
    public function execute($options = [])
71
    {
72
        // Force cursor to be used
73 2
        $options = array_merge($options, ['cursor' => true]);
74
75 2
        $cursor = parent::execute($options);
76
77 2
        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...
78
    }
79
80
    /**
81
     * Set which class to use when hydrating results as document class instances.
82
     *
83
     * @param string $className
84
     *
85
     * @return self
86
     */
87 1
    public function hydrate($className)
88
    {
89 1
        $this->hydrationClass = $className;
90
91 1
        return $this;
92
    }
93
94
    /**
95
     * @return QueryExpr
96
     */
97 3
    public function matchExpr()
98
    {
99 3
        $expr = new QueryExpr($this->dm);
100 3
        $expr->setClassMetadata($this->class);
101
102 3
        return $expr;
103
    }
104
105
    /**
106
     * @return Expr
107
     */
108 10
    public function expr()
109
    {
110 10
        return new Expr($this->dm, $this->class);
111
    }
112
113
    /**
114
     * @return Stage\Bucket
115
     */
116 1
    public function bucket()
117
    {
118 1
        return $this->addStage(new Stage\Bucket($this, $this->dm, $this->class));
119
    }
120
121
    /**
122
     * @return Stage\BucketAuto
123
     */
124 1
    public function bucketAuto()
125
    {
126 1
        return $this->addStage(new Stage\BucketAuto($this, $this->dm, $this->class));
127
    }
128
129
    /**
130
     * @param string $from
131
     *
132
     * @return Stage\GraphLookup
133
     */
134
    public function graphLookup($from)
135
    {
136
        return $this->addStage(new Stage\GraphLookup($this, $from, $this->dm, $this->class));
137
    }
138
139
    /**
140
     * @return Stage\Match
141
     */
142 2
    public function match()
143
    {
144 2
        return $this->addStage(new Stage\Match($this));
145
    }
146
147
    /**
148
     * @param string $from
149
     * @return Stage\Lookup
150
     */
151
    public function lookup($from)
152
    {
153
        return $this->addStage(new Stage\Lookup($this, $from, $this->dm, $this->class));
154
    }
155
156
    /**
157
     * @param string $from
158
     * @return Stage\Out
159
     */
160 4
    public function out($from)
161
    {
162 4
        return $this->addStage(new Stage\Out($this, $from, $this->dm));
163
    }
164
165
    /**
166
     * @param string|null $expression Optional. A replacement expression that
167
     * resolves to a document.
168
     * @return Stage\ReplaceRoot
169
     */
170 6
    public function replaceRoot($expression = null)
171
    {
172 6
        return $this->addStage(new Stage\ReplaceRoot($this, $this->dm, $this->class, $expression));
173
    }
174
175
    /**
176
     * @return Stage\SortByCount
177
     */
178 1
    public function sortByCount($expression)
179
    {
180 1
        return $this->addStage(new Stage\SortByCount($this, $expression, $this->dm, $this->class));
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 2
    public function sort($fieldName, $order = null)
187
    {
188 2
        $fields = is_array($fieldName) ? $fieldName : [$fieldName => $order];
189 2
        return parent::sort($this->getDocumentPersister()->prepareSortOrProjection($fields));
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195 2
    public function unwind($fieldName)
196
    {
197 2
        $fieldName = $this->getDocumentPersister()->prepareFieldName($fieldName);
198 2
        return parent::unwind($fieldName);
199
    }
200
201
    /**
202
     * Returns the assembled aggregation pipeline
203
     *
204
     * For pipelines where the first stage is a $geoNear stage, it will apply
205
     * the document filters and discriminator queries to the query portion of
206
     * the geoNear operation. For all other pipelines, it prepends a $match stage
207
     * containing the required query.
208
     *
209
     * @return array
210
     */
211 11
    public function getPipeline()
212
    {
213 11
        $pipeline = parent::getPipeline();
214
215 11
        if ($this->getStage(0) instanceof GeoNear) {
216 1
            $pipeline[0]['$geoNear']['query'] = $this->applyFilters($pipeline[0]['$geoNear']['query']);
217
        } else {
218 10
            $matchStage = $this->applyFilters([]);
219 10
            if ($matchStage !== []) {
220 1
                array_unshift($pipeline, ['$match' => $matchStage]);
221
            }
222
        }
223
224 11
        return $pipeline;
225
    }
226
227
    /**
228
     * Applies filters and discriminator queries to the pipeline
229
     *
230
     * @param array $query
231
     * @return array
232
     */
233 11
    private function applyFilters(array $query)
234
    {
235 11
        $documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
236
237 11
        $query = $documentPersister->addDiscriminatorToPreparedQuery($query);
238 11
        $query = $documentPersister->addFilterToPreparedQuery($query);
239
240 11
        return $query;
241
    }
242
243
    /**
244
     * @param BaseCommandCursor $cursor
245
     *
246
     * @return CommandCursor
247
     */
248 2
    private function prepareCursor(BaseCommandCursor $cursor)
249
    {
250 2
        $class = null;
251 2
        if ($this->hydrationClass) {
252 1
            $class = $this->dm->getClassMetadata($this->hydrationClass);
253
        }
254
255 2
        return new CommandCursor($cursor, $this->dm->getUnitOfWork(), $class);
256
    }
257
258
    /**
259
     * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister
260
     */
261 2
    private function getDocumentPersister()
262
    {
263 2
        return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
264
    }
265
}
266