Passed
Pull Request — master (#1821)
by Nico
52:02 queued 25:23
created

ShipAlertStateEnum   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 12.5%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 34
ccs 2
cts 16
cp 0.125
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 6 1
A isAtLeast() 0 3 1
A getRandomAlertLevel() 0 6 1
A getAttackCause() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Ship;
6
7
use RuntimeException;
8
use Stu\Module\Ship\Lib\Battle\ShipAttackCauseEnum;
9
10
enum ShipAlertStateEnum: int
11
{
12
    case ALERT_GREEN = 1;
13
    case ALERT_YELLOW = 2;
14
    case ALERT_RED = 3;
15
16
    public function getDescription(): string
17
    {
18
        return match ($this) {
19
            ShipAlertStateEnum::ALERT_GREEN => _("Alarm Grün"),
20
            ShipAlertStateEnum::ALERT_YELLOW => _("Alarm Gelb"),
21
            ShipAlertStateEnum::ALERT_RED => _("Alarm Rot")
22
        };
23
    }
24
25 1
    public function isAtLeast(ShipAlertStateEnum $minimum): bool
26
    {
27 1
        return $this->value >= $minimum->value;
28
    }
29
30
    public static function getRandomAlertLevel(): ShipAlertStateEnum
31
    {
32
        /** @var array<int> */
33
        $values = array_map(fn (ShipAlertStateEnum $alertState) => $alertState->value, self::cases());
34
35
        return self::from($values[array_rand($values)]);
36
    }
37
38
    public function getAttackCause(): ShipAttackCauseEnum
39
    {
40
        return match ($this) {
41
            ShipAlertStateEnum::ALERT_GREEN => throw new RuntimeException('this should not happen'),
42
            ShipAlertStateEnum::ALERT_YELLOW => ShipAttackCauseEnum::ALERT_YELLOW,
43
            ShipAlertStateEnum::ALERT_RED => ShipAttackCauseEnum::ALERT_RED
44
        };
45
    }
46
}
47