MethodAggregator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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