Passed
Pull Request — master (#38)
by Teye
05:43
created

Granularity   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 19
c 0
b 0
f 0
dl 0
loc 32
ccs 7
cts 7
cp 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Types;
5
6
use InvalidArgumentException;
7
8
/**
9
 * Class Granularity
10
 *
11
 * @package Level23\Druid\Types
12
 */
13
final class Granularity extends Enum
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_ENUM, expecting T_STRING or T_NAME_QUALIFIED or T_NAME_FULLY_QUALIFIED or T_NAME_RELATIVE on line 13 at column 32
Loading history...
14
{
15
    public const ALL            = 'all';
16
    public const NONE           = 'none';
17
    public const SECOND         = 'second';
18
    public const MINUTE         = 'minute';
19
    public const FIFTEEN_MINUTE = 'fifteen_minute';
20
    public const THIRTY_MINUTE  = 'thirty_minute';
21
    public const HOUR           = 'hour';
22
    public const DAY            = 'day';
23
    public const WEEK           = 'week';
24
    public const MONTH          = 'month';
25
    public const QUARTER        = 'quarter';
26
    public const YEAR           = 'year';
27
28
    /**
29
     * @param string $granularity
30
     *
31
     * @return string
32
     * @throws InvalidArgumentException
33
     */
34 376
    public static function validate(string $granularity): string
35
    {
36 376
        $granularity = strtolower($granularity);
37 376
        if (!Granularity::isValidValue($granularity)) {
38 5
            throw new InvalidArgumentException(
39 5
                'The given granularity is invalid: ' . $granularity . '. ' .
40 5
                'Allowed are: ' . implode(',', Granularity::values())
41
            );
42
        }
43
44 372
        return $granularity;
45
    }
46
}