ExpressionPostAggregator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 21
c 1
b 0
f 0
dl 0
loc 49
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A toArray() 0 21 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\PostAggregations;
5
6
use Level23\Druid\Types\DataType;
7
8
class ExpressionPostAggregator implements PostAggregatorInterface
9
{
10
    protected string $outputName;
11
12
    protected string $expression;
13
14
    protected ?string $ordering;
15
16
    protected DataType|string|null $outputType;
17
18 3
    public function __construct(
19
        string $outputName,
20
        string $expression,
21
        ?string $ordering = null,
22
        DataType|string|null $outputType = null
23
24
    ) {
25 3
        $this->outputName = $outputName;
26 3
        $this->expression = $expression;
27 3
        $this->ordering   = $ordering;
28 3
        $this->outputType = $outputType;
29
    }
30
31
    /**
32
     * Return the aggregator as it can be used in a druid query.
33
     *
34
     * @return array<string,string>
35
     */
36 3
    public function toArray(): array
37
    {
38 3
        $result = [
39 3
            'type'       => 'expression',
40 3
            'name'       => $this->outputName,
41 3
            'expression' => $this->expression,
42 3
        ];
43
44 3
        if ($this->ordering) {
45 1
            $result['ordering'] = $this->ordering;
46
        }
47
48 3
        if ($this->outputType) {
49 2
            if ($this->outputType instanceof DataType) {
50 1
                $result['outputType'] = $this->outputType->value;
51
            } else {
52 1
                $result['outputType'] = $this->outputType;
53
            }
54
        }
55
56 3
        return $result;
57
    }
58
}