Passed
Push — dev ( 80c162...225d4f )
by Janko
09:12
created

SpacecraftSystemCreation::addModuleSystems()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 9
nop 2
dl 0
loc 21
ccs 0
cts 13
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Creation;
4
5
use Doctrine\Common\Collections\Collection;
6
use Stu\Component\Spacecraft\SpacecraftModuleTypeEnum;
7
use Stu\Component\Spacecraft\SpacecraftRumpEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft\SpacecraftRumpEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Stu\Component\Spacecraft\System\SpacecraftSystemModeEnum;
9
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
10
use Stu\Orm\Entity\BuildplanModuleInterface;
11
use Stu\Orm\Entity\ModuleInterface;
12
use Stu\Orm\Entity\SpacecraftInterface;
13
use Stu\Orm\Repository\ModuleSpecialRepositoryInterface;
14
use Stu\Orm\Repository\SpacecraftSystemRepositoryInterface;
15
16
class SpacecraftSystemCreation implements SpacecraftSystemCreationInterface
17
{
18 1
    public function __construct(
19
        private SpacecraftSystemRepositoryInterface $shipSystemRepository,
20
        private ModuleSpecialRepositoryInterface $moduleSpecialRepository
21 1
    ) {}
22
23
    public function createShipSystemsByModuleList(
24
        SpacecraftInterface $spacecraft,
25
        Collection $buildplanModules,
26
        ?SpacecraftCreationConfigInterface $spacecraftCreationConfig
27
    ): void {
28
        $systems = $this->getDefaultSystems($spacecraft);
29
30
        $this->addModuleSystems($buildplanModules, $systems);
31
32
        if ($spacecraftCreationConfig !== null) {
33
            $this->addSpecialSystems($spacecraftCreationConfig, $systems);
34
        }
35
36
        foreach ($systems as $systemType => $module) {
37
            $this->createShipSystem($systemType, $spacecraft, $module);
38
        }
39
    }
40
41
42
    /** @return array<int, ModuleInterface|null>  */
43
    private function getDefaultSystems(SpacecraftInterface $spacecraft): array
44
    {
45
        $systems = [];
46
47
        //default systems, that almost every ship should have
48
        if ($spacecraft->getRump()->getCategoryId() !== SpacecraftRumpEnum::SHIP_CATEGORY_SHUTTLE) {
49
            $systems[SpacecraftSystemTypeEnum::DEFLECTOR->value] = null;
50
            $systems[SpacecraftSystemTypeEnum::TRACTOR_BEAM->value] = null;
51
        }
52
        $systems[SpacecraftSystemTypeEnum::LIFE_SUPPORT->value] = null;
53
        //TODO transporter
54
55
        if ($spacecraft->getRump()->getCategoryId() === SpacecraftRumpEnum::SHIP_CATEGORY_STATION) {
56
            $systems[SpacecraftSystemTypeEnum::BEAM_BLOCKER->value] = null;
57
        }
58
59
        if ($spacecraft->getRump()->isShipyard()) {
60
            $systems[SpacecraftSystemTypeEnum::CONSTRUCTION_HUB->value] = null;
61
        }
62
63
        if ($spacecraft->getRump()->getRoleId() === SpacecraftRumpEnum::SHIP_ROLE_SENSOR) {
64
            $systems[SpacecraftSystemTypeEnum::UPLINK->value] = null;
65
        }
66
67
        return $systems;
68
    }
69
70
    private function createShipSystem(int $systemType, SpacecraftInterface $spacecraft, ?ModuleInterface $module): void
71
    {
72
        $spacecraftSystem = $this->shipSystemRepository->prototype();
73
        $spacecraftSystem->setSpacecraft($spacecraft);
74
        $spacecraft->getSystems()->set($systemType, $spacecraftSystem);
75
        $spacecraftSystem->setSystemType(SpacecraftSystemTypeEnum::from($systemType));
76
        if ($module !== null) {
77
            $spacecraftSystem->setModule($module);
78
        }
79
        $spacecraftSystem->setStatus(100);
80
        $spacecraftSystem->setMode(SpacecraftSystemModeEnum::MODE_OFF);
81
82
        $this->shipSystemRepository->save($spacecraftSystem);
83
    }
84
85
    /**
86
     * @param Collection<int, BuildplanModuleInterface> $buildplanModules
87
     * @param array<int, ModuleInterface|null> $systems
88
     */
89
    private function addModuleSystems(Collection $buildplanModules, array &$systems): void
90
    {
91
        foreach ($buildplanModules as $buildplanmodule) {
92
            $module = $buildplanmodule->getModule();
93
94
            $systemType = $module->getSystemType();
95
            if (
96
                $systemType === null
97
                && $module->getType()->hasCorrespondingSystemType()
98
            ) {
99
                $systemType = $module->getType()->getSystemType();
100
            }
101
102
            if ($systemType !== null) {
103
                $systems[$systemType->value] = $module;
104
            }
105
106
            switch ($module->getType()) {
107
                case SpacecraftModuleTypeEnum::SENSOR:
108
                    $systems[SpacecraftSystemTypeEnum::NBS->value] = null;
109
                    break;
110
            }
111
        }
112
    }
113
114
    /**
115
     * @param array<int, ModuleInterface|null> $systems
116
     */
117
    private function addSpecialSystems(SpacecraftCreationConfigInterface $specialSystemsProvider, array &$systems): void
118
    {
119
        foreach ($specialSystemsProvider->getSpecialSystemModules() as $module) {
120
            $this->addSpecialSystem($module, $systems);
121
        }
122
    }
123
124
    /**
125
     * @param array<int, null|ModuleInterface> $systems
126
     */
127
    private function addSpecialSystem(ModuleInterface $module, array &$systems): void
128
    {
129
        $moduleSpecials = $this->moduleSpecialRepository->getByModule($module->getId());
130
131
        foreach ($moduleSpecials as $special) {
132
133
            $moduleSpecial = $special->getSpecialId();
134
            $systems[$moduleSpecial->getSystemType()->value] = $moduleSpecial->hasCorrespondingModule() ? $module : null;
135
        }
136
    }
137
}
138