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

ShipAlertStateEnum::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
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