Passed
Push — master ( 8e1e0b...949968 )
by Teye
16:13
created

AnyAggregator::toArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 10
cc 3
nc 2
nop 0
crap 3.0175
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
class AnyAggregator extends MethodAggregator
10
{
11
    /**
12
     * @var int|null
13
     */
14
    protected $maxStringBytes;
15
16
    /**
17
     * constructor.
18
     *
19
     * @param string   $metricName
20
     * @param string   $outputName                          When not given, we will use the same name as the metric.
21
     * @param string   $type                                The type of field. This can either be "long", "float" or
22
     *                                                      "double"
23
     * @param int|null $maxStringBytes                      optional, defaults to 1024
24
     */
25 4
    public function __construct(
26
        string $metricName,
27
        string $outputName = '',
28
        string $type = 'long',
29
        int $maxStringBytes = null
30
    ) {
31 4
        $type = strtolower($type);
32 4
        if (!in_array($type, ['long', 'float', 'double'])) {
33 1
            throw new InvalidArgumentException(
34 1
                'Incorrect type given: ' . $type . '. This can either be "long", "float" or "double"'
35
            );
36
        }
37
38 3
        $this->type           = $type;
39 3
        $this->metricName     = $metricName;
40 3
        $this->outputName     = $outputName ?: $metricName;
41 3
        $this->maxStringBytes = $maxStringBytes;
42
    }
43
44
    /**
45
     * Return the aggregator as it can be used in a druid query.
46
     *
47
     * @return array
48
     */
49 3
    public function toArray(): array
50
    {
51 3
        $response = [
52 3
            'type'      => $this->type . ucfirst($this->getMethod()),
53 3
            'name'      => $this->outputName,
54 3
            'fieldName' => $this->metricName,
55
        ];
56
57 3
        if ($this->type == DataType::STRING && $this->maxStringBytes !== null) {
58
            $response['maxStringBytes'] = $this->maxStringBytes;
59
        }
60
61 3
        return $response;
62
    }
63
64
    /**
65
     * Returns the method for the type aggregation
66
     *
67
     * @return string
68
     */
69 3
    protected function getMethod(): string
70
    {
71 3
        return 'any';
72
    }
73
}