Failed Conditions
Push — dev ( 56c6e5...f96e05 )
by Janko
17:26
created

SpacecraftStorageCrewLogic::acceptsCrewFrom()   B

Complexity

Conditions 11
Paths 8

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 25
c 1
b 0
f 0
nc 8
nop 4
dl 0
loc 42
ccs 0
cts 25
cp 0
crap 132
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Transfer\Wrapper;
6
7
use Stu\Component\Spacecraft\Crew\SpacecraftCrewCalculatorInterface;
8
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
9
use Stu\Component\Spacecraft\System\SpacecraftSystemModeEnum;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Component\Spacecraft\System\Type\UplinkShipSystem;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft...m\Type\UplinkShipSystem 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...
12
use Stu\Component\Station\Dock\DockPrivilegeUtilityInterface;
13
use Stu\Lib\Information\InformationInterface;
14
use Stu\Component\Spacecraft\System\Control\ActivatorDeactivatorHelperInterface;
15
use Stu\Module\Spacecraft\Lib\Auxiliary\SpacecraftShutdownInterface;
16
use Stu\Module\Spacecraft\Lib\Crew\TroopTransferUtilityInterface;
17
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
18
use Stu\Orm\Entity\SpacecraftInterface;
19
use Stu\Orm\Entity\UserInterface;
20
21
class SpacecraftStorageCrewLogic
22
{
23 1
    public function __construct(
24
        private TroopTransferUtilityInterface $troopTransferUtility,
25
        private DockPrivilegeUtilityInterface $dockPrivilegeUtility,
26
        private ActivatorDeactivatorHelperInterface $activatorDeactivatorHelper,
27
        private SpacecraftSystemManagerInterface $spacecraftSystemManager,
28
        private SpacecraftCrewCalculatorInterface $shipCrewCalculator,
29
        private SpacecraftShutdownInterface $spacecraftShutdown
30 1
    ) {}
31
32 4
    public function getMaxTransferrableCrew(SpacecraftInterface $spacecraft, bool $isTarget, UserInterface $user): int
33
    {
34 4
        return min(
35 4
            $this->troopTransferUtility->ownCrewOnTarget($user, $spacecraft),
36 4
            $isTarget ? PHP_INT_MAX : $this->troopTransferUtility->getBeamableTroopCount($spacecraft)
37 4
        );
38
    }
39
40 4
    public function getFreeCrewSpace(SpacecraftInterface $spacecraft, UserInterface $user): int
41
    {
42 4
        if ($user !== $spacecraft->getUser()) {
43
            if (!$spacecraft->hasUplink()) {
44
                return 0;
45
            }
46
47
            $userCrewOnTarget = $this->troopTransferUtility->ownCrewOnTarget($user, $spacecraft);
48
            return $userCrewOnTarget === 0 ? 1 : 0;
49
        }
50
51 4
        return $this->troopTransferUtility->getFreeQuarters($spacecraft);
52
    }
53
54
    public function checkCrewStorage(SpacecraftWrapperInterface $wrapper, int $amount, bool $isUnload, InformationInterface $information): bool
55
    {
56
        $spacecraft = $wrapper->get();
57
58
        if (!$spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)) {
59
            return true;
60
        }
61
62
        $maxRumpCrew = $this->shipCrewCalculator->getMaxCrewCountByRump($spacecraft->getRump());
63
        $newCrewAmount = $spacecraft->getCrewCount() + ($isUnload ? -$amount : $amount);
64
        if ($newCrewAmount <= $maxRumpCrew) {
65
            return true;
66
        }
67
68
        if (!$spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::TROOP_QUARTERS)) {
69
            $information->addInformation("Die Truppenquartiere sind zerstört");
70
            return false;
71
        }
72
73
        if ($spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)->getMode()->isActivated()) {
74
            return true;
75
        }
76
77
        if (!$this->activatorDeactivatorHelper->activate(
78
            $wrapper,
79
            SpacecraftSystemTypeEnum::TROOP_QUARTERS,
80
            $information
81
        )) {
82
            $information->addInformation("Die Truppenquartiere konnten nicht aktiviert werden");
83
            return false;
84
        }
85
86
        return true;
87
    }
88
89
    public function acceptsCrewFrom(SpacecraftWrapperInterface $wrapper, int $amount, UserInterface $user, InformationInterface $information): bool
90
    {
91
        $spacecraft = $wrapper->get();
92
93
        if (!$spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::LIFE_SUPPORT)) {
94
            $information->addInformationf('Die %s hat keine Lebenserhaltungssysteme', $spacecraft->getName());
95
96
            return false;
97
        }
98
99
        $needsTroopQuarters = $spacecraft->getCrewCount() + $amount > $this->shipCrewCalculator->getMaxCrewCountByRump($spacecraft->getRump());
100
        if (
101
            $needsTroopQuarters
102
            && $spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)
103
            && $spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)->getMode() === SpacecraftSystemModeEnum::MODE_OFF
104
            && !$this->activatorDeactivatorHelper->activate($wrapper, SpacecraftSystemTypeEnum::TROOP_QUARTERS, $information)
105
        ) {
106
            return false;
107
        }
108
109
        if ($spacecraft->getUser() === $user) {
110
            return true;
111
        }
112
        if (!$spacecraft->hasUplink()) {
113
            return false;
114
        }
115
116
        if (!$this->dockPrivilegeUtility->checkPrivilegeFor($spacecraft->getId(), $user)) {
117
            $information->addInformation("Benötigte Andockerlaubnis wurde verweigert");
118
            return false;
119
        }
120
        if (!$spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::UPLINK)) {
121
            $information->addInformation("Das Ziel verfügt über keinen intakten Uplink");
122
            return false;
123
        }
124
125
        if ($this->troopTransferUtility->foreignerCount($spacecraft) >= UplinkShipSystem::MAX_FOREIGNERS) {
126
            $information->addInformation("Maximale Anzahl an fremden Crewman ist bereits erreicht");
127
            return false;
128
        }
129
130
        return true;
131
    }
132
133
    public function postCrewTransfer(SpacecraftWrapperInterface $wrapper, int $foreignCrewChangeAmount, InformationInterface $information): void
134
    {
135
        $spacecraft = $wrapper->get();
136
137
        // no crew left, so shut down
138
        if ($spacecraft->getCrewCount() === 0) {
139
            $this->spacecraftShutdown->shutdown($wrapper);
140
            return;
141
        }
142
143
        if ($foreignCrewChangeAmount !== 0) {
144
145
            $ownCrew = $this->getOwnCrewCount($spacecraft);
146
            $minOwnCrew = 0;
147
            $buildplan = $spacecraft->getBuildplan();
148
            if ($buildplan !== null) {
149
                $minOwnCrew = $buildplan->getCrew();
150
            }
151
152
            $hasForeigners = $this->troopTransferUtility->foreignerCount($spacecraft) > 0;
153
            if (
154
                !$hasForeigners
155
                && $spacecraft->getSystemState(SpacecraftSystemTypeEnum::UPLINK)
156
            ) {
157
                $spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::UPLINK)->setMode(SpacecraftSystemModeEnum::MODE_OFF);
158
            }
159
            if (
160
                $hasForeigners
161
                && !$spacecraft->getSystemState(SpacecraftSystemTypeEnum::UPLINK)
162
                && $ownCrew >= $minOwnCrew
163
            ) {
164
                $spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::UPLINK)->setMode(SpacecraftSystemModeEnum::MODE_ON);
165
            }
166
167
            $this->sendUplinkMessage($hasForeigners, $information, $spacecraft->getSystemState(SpacecraftSystemTypeEnum::UPLINK), $ownCrew >= $minOwnCrew);
168
        }
169
170
        if (
171
            $spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)
172
            && $spacecraft->getSystemState(SpacecraftSystemTypeEnum::TROOP_QUARTERS)
173
            && $spacecraft->getBuildplan() !== null
174
            && $spacecraft->getCrewCount() <= $this->shipCrewCalculator->getMaxCrewCountByRump($spacecraft->getRump())
175
        ) {
176
            $this->activatorDeactivatorHelper->deactivate($wrapper, SpacecraftSystemTypeEnum::TROOP_QUARTERS, $information);
177
        }
178
179
        if (!$spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::LIFE_SUPPORT)) {
180
            return;
181
        }
182
183
        if (
184
            $spacecraft->getCrewCount() > 0
185
            && !$spacecraft->getSystemState(SpacecraftSystemTypeEnum::LIFE_SUPPORT)
186
        ) {
187
            $this->spacecraftSystemManager->activate($wrapper, SpacecraftSystemTypeEnum::LIFE_SUPPORT, true);
188
        }
189
    }
190
191
    private function sendUplinkMessage(bool $hasForeigners, InformationInterface $information, bool $state, bool $enoughOwnCrew): void
192
    {
193
        if (!$hasForeigners) {
194
            $information->addInformationf(
195
                'Der Uplink ist %s',
196
                $state ? 'aktiviert' : 'deaktiviert'
197
            );
198
        } else {
199
            $information->addInformationf(
200
                'Der Uplink %s%s',
201
                $state ? 'ist aktiviert' : 'bleibt deaktiviert',
202
                $enoughOwnCrew ? '' : '. Es befindet sich nicht ausreichend Crew des Besitzers an Bord'
203
            );
204
        }
205
    }
206
207
    private function getOwnCrewCount(SpacecraftInterface $spacecraft): int
208
    {
209
        $count = 0;
210
        foreach ($spacecraft->getCrewAssignments() as $spacecraftCrew) {
211
            if ($spacecraftCrew->getCrew()->getUser() === $spacecraft->getUser()) {
212
                $count++;
213
            }
214
        }
215
        return $count;
216
    }
217
}
218