Dimension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 21
dl 0
loc 71
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOutputName() 0 3 1
A toArray() 0 7 1
A getDimension() 0 3 1
A __construct() 0 21 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Dimensions;
5
6
use InvalidArgumentException;
7
use Level23\Druid\Types\DataType;
8
9
class Dimension implements DimensionInterface
10
{
11
    protected string $dimension;
12
13
    protected string $outputName;
14
15
    protected DataType $outputType;
16
17
    /**
18
     * Dimension constructor.
19
     *
20
     * @param string                   $dimension
21
     * @param string|null              $outputName
22
     * @param string|DataType          $outputType This can either be "long", "float" or "string"
23
     */
24 73
    public function __construct(
25
        string $dimension,
26
        ?string $outputName = null,
27
        string|DataType $outputType = DataType::STRING
28
    ) {
29 73
        $this->dimension  = $dimension;
30 73
        $this->outputName = $outputName ?: $dimension;
31
32 73
        if( empty($outputType)) {
33 1
            $outputType = DataType::STRING;
34
        } else {
35 72
            $outputType = is_string($outputType) ? DataType::from(strtolower($outputType)) : $outputType;
36
        }
37
38 72
        if (!in_array($outputType, [DataType::STRING, DataType::LONG, DataType::FLOAT])) {
39 1
            throw new InvalidArgumentException(
40 1
                'Incorrect type given: ' . $outputType->value . '. This can either be "long", "float" or "string"'
41 1
            );
42
        }
43
44 71
        $this->outputType         = $outputType;
45
    }
46
47
    /**
48
     * Return the dimension as it should be used in a druid query.
49
     *
50
     * @return array<string,string|array<mixed>>
51
     */
52 26
    public function toArray(): array
53
    {
54 26
        return [
55 26
            'type'       => 'default',
56 26
            'dimension'  => $this->dimension,
57 26
            'outputType' => $this->outputType->value,
58 26
            'outputName' => $this->outputName,
59 26
        ];
60
    }
61
62
    /**
63
     * Return the name of the dimension which is selected.
64
     *
65
     * @return string
66
     */
67 26
    public function getDimension(): string
68
    {
69 26
        return $this->dimension;
70
    }
71
72
    /**
73
     * Return the output name of this dimension
74
     *
75
     * @return string
76
     */
77 23
    public function getOutputName(): string
78
    {
79 23
        return $this->outputName;
80
    }
81
}