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

SpacecraftAlertStateEnum::isAtLeast()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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