Passed
Pull Request — master (#35)
by Teye
04:28
created

AnyAggregator::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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