Completed
Push — master ( 11f27c...4713a5 )
by Andreas
08:33
created

Stage   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 354
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 59.18%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 354
ccs 29
cts 49
cp 0.5918
rs 10
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
getExpression() 0 1 ?
A execute() 0 4 1
A bucket() 0 4 1
A bucketAuto() 0 4 1
A collStats() 0 4 1
A count() 0 4 1
A facet() 0 4 1
A geoNear() 0 4 1
A getPipeline() 0 4 1
A graphLookup() 0 4 1
A group() 0 4 1
A indexStats() 0 4 1
A limit() 0 4 1
A lookup() 0 4 1
A match() 0 4 1
A out() 0 4 1
A project() 0 4 1
A redact() 0 4 1
A replaceRoot() 0 4 1
A sample() 0 4 1
A skip() 0 4 1
A sortByCount() 0 4 1
A sort() 0 4 1
A unwind() 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\ODM\MongoDB\Iterator\Iterator;
23
use GeoJson\Geometry\Point;
24
25
/**
26
 * Fluent interface for building aggregation pipelines.
27
 *
28
 * @author alcaeus <[email protected]>
29
 * @since 1.2
30
 * @internal
31
 */
32
abstract class Stage
33
{
34
    /**
35
     * @var Builder
36
     */
37
    protected $builder;
38
39
    /**
40
     * @param Builder $builder
41
     */
42 253
    public function __construct(Builder $builder)
43
    {
44 253
        $this->builder = $builder;
45 253
    }
46
47
    /**
48
     * Assembles the aggregation stage
49
     *
50
     * @return array
51
     */
52
    abstract public function getExpression();
53
54
    /**
55
     * Executes the aggregation pipeline
56
     *
57
     * @param array $options
58
     * @return Iterator
59
     */
60 1
    public function execute($options = [])
61
    {
62 1
        return $this->builder->execute($options);
63
    }
64
65
    /**
66
     * Categorizes incoming documents into groups, called buckets, based on a
67
     * specified expression and bucket boundaries.
68
     *
69
     * Each bucket is represented as a document in the output. The document for
70
     * each bucket contains an _id field, whose value specifies the inclusive
71
     * lower bound of the bucket and a count field that contains the number of
72
     * documents in the bucket. The count field is included by default when the
73
     * output is not specified.
74
     *
75
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/
76
     *
77
     * @return Stage\Bucket
78
     */
79
    public function bucket()
80
    {
81
        return $this->builder->bucket();
82
    }
83
84
    /**
85
     * Categorizes incoming documents into a specific number of groups, called
86
     * buckets, based on a specified expression.
87
     *
88
     * Bucket boundaries are automatically determined in an attempt to evenly
89
     * distribute the documents into the specified number of buckets. Each
90
     * bucket is represented as a document in the output. The document for each
91
     * bucket contains an _id field, whose value specifies the inclusive lower
92
     * bound and the exclusive upper bound for the bucket, and a count field
93
     * that contains the number of documents in the bucket. The count field is
94
     * included by default when the output is not specified.
95
     *
96
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/bucketAuto/
97
     *
98
     * @return Stage\BucketAuto
99
     */
100
    public function bucketAuto()
101
    {
102
        return $this->builder->bucketAuto();
103
    }
104
105
    /**
106
     * Returns statistics regarding a collection or view.
107
     *
108
     * $collStats must be the first stage in an aggregation pipeline, or else
109
     * the pipeline returns an error.
110
     *
111
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/geoNear/
112
     * @since 1.5
113
     *
114
     * @return Stage\CollStats
115
     */
116
    public function collStats()
117
    {
118
        return $this->builder->collStats();
119
    }
120
121
    /**
122
     * Returns a document that contains a count of the number of documents input
123
     * to the stage.
124
     *
125
     * @see https://docs.mongodb.com/manual/reference/operator/aggregation/count/
126
     *
127
     * @return Stage\Count
128
     */
129
    public function count($fieldName)
130
    {
131
        return $this->builder->count($fieldName);
132
    }
133
134
    /**
135
     * Processes multiple aggregation pipelines within a single stage on the
136
     * same set of input documents.
137
     *
138
     * Each sub-pipeline has its own field in the output document where its
139
     * results are stored as an array of documents.
140
     *
141
     * @return Stage\Facet
142
     */
143
    public function facet()
144
    {
145
        return $this->builder->facet();
146
    }
147
148
    /**
149
     * Outputs documents in order of nearest to farthest from a specified point.
150
     * You can only use this as the first stage of a pipeline.
151
     *
152
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/geoNear/
153
     *
154
     * @param float|array|Point $x
155
     * @param float $y
156
     * @return Stage\GeoNear
157
     */
158
    public function geoNear($x, $y = null)
159
    {
160
        return $this->builder->geoNear($x, $y);
161
    }
162
163
    /**
164
     * Returns the assembled aggregation pipeline
165
     *
166
     * @return array
167
     */
168
    public function getPipeline()
169
    {
170
        return $this->builder->getPipeline();
171
    }
172
173
    /**
174
     * Performs a recursive search on a collection, with options for restricting
175
     * the search by recursion depth and query filter.
176
     *
177
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/graphLookup/
178
     *
179
     * @param string $from Target collection for the $graphLookup operation to
180
     * search, recursively matching the connectFromField to the connectToField.
181
     * @return Stage\GraphLookup
182
     */
183
    public function graphLookup($from)
184
    {
185
        return $this->builder->graphLookup($from);
186
    }
187
188
    /**
189
     * Groups documents by some specified expression and outputs to the next
190
     * stage a document for each distinct grouping.
191
     *
192
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/group/
193
     *
194
     * @return Stage\Group
195
     */
196 2
    public function group()
197
    {
198 2
        return $this->builder->group();
199
    }
200
201
    /**
202
     * Returns statistics regarding the use of each index for the collection.
203
     *
204
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/indexStats/
205
     *
206
     * @return Stage\IndexStats
207
     */
208
    public function indexStats()
209
    {
210
        return $this->builder->indexStats();
211
    }
212
213
    /**
214
     * Limits the number of documents passed to the next stage in the pipeline.
215
     *
216
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/limit/
217
     *
218
     * @param integer $limit
219
     * @return Stage\Limit
220
     */
221 1
    public function limit($limit)
222
    {
223 1
        return $this->builder->limit($limit);
224
    }
225
226
    /**
227
     * Performs a left outer join to an unsharded collection in the same
228
     * database to filter in documents from the “joined” collection for
229
     * processing.
230
     *
231
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/
232
     *
233
     * @param string $from
234
     * @return Stage\Lookup
235
     */
236 7
    public function lookup($from)
237
    {
238 7
        return $this->builder->lookup($from);
239
    }
240
241
    /**
242
     * Filters the documents to pass only the documents that match the specified
243
     * condition(s) to the next pipeline stage.
244
     *
245
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/match/
246
     *
247
     * @return Stage\Match
248
     */
249 1
    public function match()
250
    {
251 1
        return $this->builder->match();
252
    }
253
254
    /**
255
     * Takes the documents returned by the aggregation pipeline and writes them
256
     * to a specified collection. This must be the last stage in the pipeline.
257
     *
258
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/out/
259
     *
260
     * @param string $collection
261
     * @return Stage\Out
262
     */
263 1
    public function out($collection)
264
    {
265 1
        return $this->builder->out($collection);
266
    }
267
268
    /**
269
     * Passes along the documents with only the specified fields to the next
270
     * stage in the pipeline. The specified fields can be existing fields from
271
     * the input documents or newly computed fields.
272
     *
273
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/project/
274
     *
275
     * @return Stage\Project
276
     */
277 2
    public function project()
278
    {
279 2
        return $this->builder->project();
280
    }
281
282
    /**
283
     * Restricts the contents of the documents based on information stored in
284
     * the documents themselves.
285
     *
286
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/redact/
287
     *
288
     * @return Stage\Redact
289
     */
290 1
    public function redact()
291
    {
292 1
        return $this->builder->redact();
293
    }
294
295
    /**
296
     * Promotes a specified document to the top level and replaces all other
297
     * fields.
298
     *
299
     * The operation replaces all existing fields in the input document,
300
     * including the _id field. You can promote an existing embedded document to
301
     * the top level, or create a new document for promotion.
302
     *
303
     * @param string|null $expression Optional. A replacement expression that
304
     * resolves to a document.
305
     *
306
     * @return Stage\ReplaceRoot
307
     */
308 2
    public function replaceRoot($expression = null)
309
    {
310 2
        return $this->builder->replaceRoot($expression);
311
    }
312
313
    /**
314
     * Randomly selects the specified number of documents from its input.
315
     *
316
     * @see https://docs.mongodb.org/manual/reference/operator/aggregation/sample/
317
     *
318
     * @param integer $size
319
     * @return Stage\Sample
320
     */
321 1
    public function sample($size)
322
    {
323 1
        return $this->builder->sample($size);
324
    }
325
326
    /**
327
     * Skips over the specified number of documents that pass into the stage and
328
     * passes the remaining documents to the next stage in the pipeline.
329
     *
330
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/skip/
331
     *
332
     * @param integer $skip
333
     * @return Stage\Skip
334
     */
335 1
    public function skip($skip)
336
    {
337 1
        return $this->builder->skip($skip);
338
    }
339
340
    /**
341
     * Groups incoming documents based on the value of a specified expression,
342
     * then computes the count of documents in each distinct group.
343
     *
344
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/sortByCount/
345
     *
346
     * @param string $expression The expression to group by
347
     * @return Stage\SortByCount
348
     */
349
    public function sortByCount($expression)
350
    {
351
        return $this->builder->sortByCount($expression);
352
    }
353
354
    /**
355
     * Sorts all input documents and returns them to the pipeline in sorted order.
356
     *
357
     * If sorting by multiple fields, the first argument should be an array of
358
     * field name (key) and order (value) pairs.
359
     *
360
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/sort/
361
     *
362
     * @param array|string $fieldName Field name or array of field/order pairs
363
     * @param integer|string $order   Field order (if one field is specified)
364
     * @return Stage\Sort
365
     */
366 3
    public function sort($fieldName, $order = null)
367
    {
368 3
        return $this->builder->sort($fieldName, $order);
369
    }
370
371
    /**
372
     * Deconstructs an array field from the input documents to output a document
373
     * for each element. Each output document is the input document with the
374
     * value of the array field replaced by the element.
375
     *
376
     * @see http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/
377
     *
378
     * @param string $fieldName The field to unwind. It is automatically prefixed with the $ sign
379
     * @return Stage\Unwind
380
     */
381 3
    public function unwind($fieldName)
382
    {
383 3
        return $this->builder->unwind($fieldName);
384
    }
385
}
386