Passed
Push — dev ( 48073e...2ba144 )
by Janko
16:20
created

ShipRumpModuleLevel::getModuleMandatory4()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Entity;
6
7
use Doctrine\ORM\Mapping\Column;
8
use Doctrine\ORM\Mapping\Entity;
9
use Doctrine\ORM\Mapping\Id;
10
use Doctrine\ORM\Mapping\JoinColumn;
11
use Doctrine\ORM\Mapping\OneToOne;
12
use Doctrine\ORM\Mapping\Table;
13
use Override;
14
use RuntimeException;
15
use Stu\Component\Spacecraft\SpacecraftModuleTypeEnum;
16
use Stu\Orm\Repository\ShipRumpModuleLevelRepository;
17
18
#[Table(name: 'stu_rump_module_level')]
19
#[Entity(repositoryClass: ShipRumpModuleLevelRepository::class)]
20
class ShipRumpModuleLevel implements ShipRumpModuleLevelInterface
21
{
22
    public const string DEFAULT_LEVEL_KEY = 'default';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 22 at column 24
Loading history...
23
    public const string MIN_LEVEL_KEY = 'min';
24
    public const string MAX_LEVEL_KEY = 'max';
25
    public const string MANDATORY_KEY = 'mandatory';
26
27
    #[Id]
28
    #[OneToOne(targetEntity: 'SpacecraftRump')]
29
    #[JoinColumn(name: 'rump_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
30
    private SpacecraftRumpInterface $rump;
31
32
    /** @var array<int, array<string, bool|int|string>> */
33
    #[Column(type: 'json', nullable: true)]
34
    private ?array $type_values = null;
35
36 4
    #[Override]
37
    public function getMinimumLevel(SpacecraftModuleTypeEnum $type): int
38
    {
39 4
        $value = $this->getValuesForType($type)[self::MIN_LEVEL_KEY];
40 4
        if (!is_integer($value)) {
41
            throw new RuntimeException(sprintf('minimum level of type %s is not an integer: %s', $type->name, (string)$value));
42
        }
43
44 4
        return $value;
45
    }
46
47 3
    #[Override]
48
    public function getDefaultLevel(SpacecraftModuleTypeEnum $type): int
49
    {
50 3
        $value = $this->getValuesForType($type)[self::DEFAULT_LEVEL_KEY];
51 3
        if (!is_integer($value)) {
52
            throw new RuntimeException(sprintf('default level of type %s is not an integer: %s', $type->name, (string)$value));
53
        }
54
55 3
        return $value;
56
    }
57
58 4
    #[Override]
59
    public function getMaximumLevel(SpacecraftModuleTypeEnum $type): int
60
    {
61 4
        $value = $this->getValuesForType($type)[self::MAX_LEVEL_KEY];
62 4
        if (!is_integer($value)) {
63
            throw new RuntimeException(sprintf('maximum level of type %s is not an integer: %s', $type->name, (string)$value));
64
        }
65
66 4
        return $value;
67
    }
68
69 3
    #[Override]
70
    public function isMandatory(SpacecraftModuleTypeEnum $type): bool
71
    {
72 3
        $value = $this->getValuesForType($type)[self::MANDATORY_KEY];
73 3
        if (!is_bool($value)) {
74
            throw new RuntimeException(sprintf('mandatory value of type %s is not a bool: %s', $type->name, (string)$value));
75
        }
76
77 3
        return $value;
78
    }
79
80
    #[Override]
81
    public function setValue(SpacecraftModuleTypeEnum $type, string $key, $value): ShipRumpModuleLevelInterface
82
    {
83
        if ($this->type_values === null) {
84
            $this->type_values = [];
85
        }
86
87
        if (!array_key_exists($type->value, $this->type_values)) {
88
            $this->type_values[$type->value] = [];
89
        }
90
91
        $this->type_values[$type->value][$key] = $value;
92
93
        return $this;
94
    }
95
96
    /** @return array<string, int|bool|string> */
97 4
    private function getValuesForType(SpacecraftModuleTypeEnum $type): array
98
    {
99
        if (
100 4
            $this->type_values === null
101 4
            || !array_key_exists($type->value, $this->type_values)
102
        ) {
103
            throw new RuntimeException(sprintf('no values for module type: %s', $type->name));
104
        }
105
106 4
        return $this->type_values[$type->value];
107
    }
108
109
    #[Override]
110
    public function getMandatoryModulesCount(): ?int
111
    {
112
        return array_reduce(
113
            array_filter(SpacecraftModuleTypeEnum::cases(), fn(SpacecraftModuleTypeEnum $type): bool => !$type->isSpecialSystemType()),
114
            fn(int $value, SpacecraftModuleTypeEnum $type): int => $value + $this->isMandatory($type) !== 0 ? 1 : 0,
115
            0
116
        );
117
    }
118
}
119