Passed
Pull Request — master (#2310)
by
unknown
17:04 queued 11:52
created

SpacecraftAlertStateEnum::getRandomAlertLevel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Spacecraft;
6
7
use RuntimeException;
8
9
enum SpacecraftAlertStateEnum: int
10
{
11
    case ALERT_GREEN = 1;
12
    case ALERT_YELLOW = 2;
13
    case ALERT_RED = 3;
14
15 1
    public function getDescription(): string
16
    {
17 1
        return match ($this) {
18 1
            self::ALERT_GREEN => "Alarm Grün",
19 1
            self::ALERT_YELLOW => "Alarm Gelb",
20 1
            self::ALERT_RED => "Alarm Rot"
21 1
        };
22
    }
23
24 20
    public function getEpsUsage(): int
25
    {
26 20
        return match ($this) {
27 20
            self::ALERT_GREEN => 0,
28 17
            self::ALERT_YELLOW => 1,
29 20
            self::ALERT_RED => 2
30 20
        };
31
    }
32
33 1
    public function isAtLeast(SpacecraftAlertStateEnum $minimum): bool
34
    {
35 1
        return $this->value >= $minimum->value;
36
    }
37
38 1
    public static function getRandomAlertLevel(): SpacecraftAlertStateEnum
39
    {
40
        /** @var array<int> */
41 1
        $values = array_map(fn(SpacecraftAlertStateEnum $alertState) => $alertState->value, self::cases());
42 1
        if($values === []) {
43
            throw new RuntimeException('No alert states defined');
44
        }
45
46 1
        return self::from($values[array_rand($values)]);
47
    }
48
}
49