AnyAggregator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 16
dl 0
loc 59
ccs 18
cts 18
cp 1
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethod() 0 3 1
A __construct() 0 14 3
A toArray() 0 13 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Aggregations;
5
6
use Level23\Druid\Types\DataType;
7
8
class AnyAggregator extends MethodAggregator
9
{
10
    protected ?int $maxStringBytes;
11
12
    /**
13
     * constructor.
14
     *
15
     * @param string          $metricName
16
     * @param string          $outputName                   When not given, we will use the same name as the metric.
17
     * @param string|DataType $type                         The type of field. This can either be "long", "float" or
18
     *                                                      "double"
19
     * @param int|null        $maxStringBytes               optional, defaults to 1024
20
     *
21
     * @noinspection PhpMissingParentConstructorInspection
22
     */
23 6
    public function __construct(
24
        string $metricName,
25
        string $outputName = '',
26
        string|DataType $type = DataType::LONG,
27
        ?int $maxStringBytes = null
28
    ) {
29 6
        if (is_string($type)) {
30 2
            $type = DataType::from($type);
31
        }
32
33 5
        $this->type           = $type;
34 5
        $this->metricName     = $metricName;
35 5
        $this->outputName     = $outputName ?: $metricName;
36 5
        $this->maxStringBytes = $maxStringBytes;
37
    }
38
39
    /**
40
     * Return the aggregator as it can be used in a druid query.
41
     *
42
     * @return array<string,string|int>
43
     */
44 5
    public function toArray(): array
45
    {
46 5
        $response = [
47 5
            'type'      => $this->type->value . ucfirst($this->getMethod()),
48 5
            'name'      => $this->outputName,
49 5
            'fieldName' => $this->metricName,
50 5
        ];
51
52 5
        if ($this->type == DataType::STRING && $this->maxStringBytes !== null) {
53 1
            $response['maxStringBytes'] = $this->maxStringBytes;
54
        }
55
56 5
        return $response;
57
    }
58
59
    /**
60
     * Returns the method for the type aggregation
61
     *
62
     * @return string
63
     */
64 5
    protected function getMethod(): string
65
    {
66 5
        return 'any';
67
    }
68
}