Facet   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 54
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpression() 0 8 1
A field() 0 6 1
A pipeline() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Aggregation\Stage;
6
7
use Doctrine\ODM\MongoDB\Aggregation\Builder;
8
use Doctrine\ODM\MongoDB\Aggregation\Stage;
9
use InvalidArgumentException;
10
use LogicException;
11
use function array_map;
12
13
/**
14
 * Fluent interface for adding a $facet stage to an aggregation pipeline.
15
 */
16
class Facet extends Stage
17
{
18
    /** @var Builder[] */
19
    private $pipelines = [];
20
21
    /** @var string */
22
    private $field;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function getExpression() : array
28
    {
29
        return [
30
            '$facet' => array_map(static function (Builder $builder) {
31 2
                return $builder->getPipeline();
32 2
            }, $this->pipelines),
33
        ];
34
    }
35
36
    /**
37
     * Set the current field for building the pipeline stage.
38
     */
39 3
    public function field(string $field) : self
40
    {
41 3
        $this->field = $field;
42
43 3
        return $this;
44
    }
45
46
    /**
47
     * Use the given pipeline for the current field.
48
     *
49
     * @param Builder|Stage $builder
50
     */
51 4
    public function pipeline($builder) : self
52
    {
53 4
        if (! $this->field) {
54 1
            throw new LogicException(__METHOD__ . ' requires you set a current field using field().');
55
        }
56
57 3
        if ($builder instanceof Stage) {
58 2
            $builder = $builder->builder;
59
        }
60
61 3
        if (! $builder instanceof Builder) {
62 1
            throw new InvalidArgumentException(__METHOD__ . ' expects either an aggregation builder or an aggregation stage.');
63
        }
64
65 2
        $this->pipelines[$this->field] = $builder;
66
67 2
        return $this;
68
    }
69
}
70