Passed
Pull Request — master (#1976)
by Janko
21:47 queued 12:50
created

checkActivationConditions()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 9
nop 4
dl 0
loc 42
ccs 23
cts 23
cp 1
crap 11
rs 7.3166
c 0
b 0
f 0

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\Component\Spacecraft\System;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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\Exception\ActivationConditionsNotMetException;
9
use Stu\Component\Spacecraft\System\Exception\AlreadyActiveException;
10
use Stu\Component\Spacecraft\System\Exception\AlreadyOffException;
11
use Stu\Component\Spacecraft\System\Exception\DeactivationConditionsNotMetException;
12
use Stu\Component\Spacecraft\System\Exception\InsufficientCrewException;
13
use Stu\Component\Spacecraft\System\Exception\InsufficientEnergyException;
14
use Stu\Component\Spacecraft\System\Exception\InvalidSystemException;
15
use Stu\Component\Spacecraft\System\Exception\SpacecraftSystemException;
16
use Stu\Component\Spacecraft\System\Exception\SystemCooldownException;
17
use Stu\Component\Spacecraft\System\Exception\SystemDamagedException;
18
use Stu\Component\Spacecraft\System\Exception\SystemNotActivatableException;
19
use Stu\Component\Spacecraft\System\Exception\SystemNotDeactivatableException;
20
use Stu\Component\Spacecraft\System\Exception\SystemNotFoundException;
21
use Stu\Module\Control\StuTime;
22
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
23
use Stu\Orm\Entity\SpacecraftSystemInterface;
24
use Stu\Orm\Entity\SpacecraftInterface;
25
26
final class SpacecraftSystemManager implements SpacecraftSystemManagerInterface
27
{
28
    /**
29
     * @param array<SpacecraftSystemTypeInterface> $systemList
30
     */
31 20
    public function __construct(private array $systemList, private StuTime $stuTime) {}
32
33 11
    #[Override]
34
    public function activate(
35
        SpacecraftWrapperInterface $wrapper,
36
        SpacecraftSystemTypeEnum $type,
37
        bool $force = false,
38
        bool $isDryRun = false
39
    ): void {
40 11
        $time = $this->stuTime->time();
41 11
        $system = $this->lookupSystem($type);
42
43 11
        if (!$force) {
44 11
            $this->checkActivationConditions($wrapper, $system, $type, $time);
45
        }
46
47 3
        if ($isDryRun) {
48 1
            return;
49
        }
50
51 2
        $epsSystem = $wrapper->getEpsSystemData();
52 2
        if ($epsSystem !== null) {
53 2
            $epsSystem->lowerEps($system->getEnergyUsageForActivation())->update();
54
        }
55
56
        //cooldown
57 2
        $shipSystem = $wrapper->get()->getSystems()[$type->value] ?? null;
58 2
        if ($shipSystem !== null && $system->getCooldownSeconds() !== null) {
59 1
            $shipSystem->setCooldown($time + $system->getCooldownSeconds());
60
        }
61
62 2
        $system->activate($wrapper, $this);
63
    }
64
65 7
    #[Override]
66
    public function deactivate(SpacecraftWrapperInterface $wrapper, SpacecraftSystemTypeEnum $type, bool $force = false): void
67
    {
68 7
        $system = $this->lookupSystem($type);
69
70 6
        if (!$force) {
71 4
            $this->checkDeactivationConditions($wrapper, $system, $type);
72
        }
73
74 3
        $system->deactivate($wrapper);
75
    }
76
77 2
    #[Override]
78
    public function deactivateAll(SpacecraftWrapperInterface $wrapper): void
79
    {
80 2
        foreach ($wrapper->get()->getSystems() as $shipSystem) {
81
            try {
82 2
                $this->deactivate($wrapper, $shipSystem->getSystemType(), true);
83 1
            } catch (SpacecraftSystemException) {
84 1
                continue;
85
            }
86
        }
87
    }
88
89
    #[Override]
90
    public function getEnergyUsageForActivation(SpacecraftSystemTypeEnum $type): int
91
    {
92
        $system = $this->lookupSystem($type);
93
94
        return $system->getEnergyUsageForActivation();
95
    }
96
97 3
    #[Override]
98
    public function getEnergyConsumption(SpacecraftSystemTypeEnum $type): int
99
    {
100 3
        $system = $this->lookupSystem($type);
101
102 3
        return $system->getEnergyConsumption();
103
    }
104
105 21
    #[Override]
106
    public function lookupSystem(SpacecraftSystemTypeEnum $type): SpacecraftSystemTypeInterface
107
    {
108 21
        $system = $this->systemList[$type->value] ?? null;
109 21
        if ($system === null) {
110 1
            throw new InvalidSystemException();
111
        }
112
113 20
        return $system;
114
    }
115
116 11
    private function checkActivationConditions(
117
        SpacecraftWrapperInterface $wrapper,
118
        SpacecraftSystemTypeInterface $system,
119
        SpacecraftSystemTypeEnum $type,
120
        int $time
121
    ): void {
122 11
        $ship = $wrapper->get();
123 11
        $shipSystem = $ship->getSystems()[$type->value] ?? null;
124 11
        if ($shipSystem === null) {
125 1
            throw new SystemNotFoundException();
126
        }
127
128 10
        if ($shipSystem->getStatus() === 0) {
129 1
            throw new SystemDamagedException();
130
        }
131
132 9
        $mode = $shipSystem->getMode();
133 9
        if ($mode === SpacecraftSystemModeEnum::MODE_ALWAYS_OFF) {
134 1
            throw new SystemNotActivatableException();
135
        }
136
137 8
        if ($mode->isActivated()) {
138 1
            throw new AlreadyActiveException();
139
        }
140
141 7
        if (!$ship->hasEnoughCrew()) {
142 1
            throw new InsufficientCrewException();
143
        }
144
145 6
        $epsSystem = $wrapper->getEpsSystemData();
146 6
        if ($epsSystem === null || $epsSystem->getEps() < $system->getEnergyUsageForActivation()) {
147 1
            throw new InsufficientEnergyException($system->getEnergyUsageForActivation());
148
        }
149
150 5
        $cooldown = $shipSystem->getCooldown();
151 5
        if ($cooldown !== null && $cooldown > $time) {
152 1
            throw new SystemCooldownException($cooldown - $time);
153
        }
154
155 4
        $reason = '';
156 4
        if (!$system->checkActivationConditions($wrapper, $reason)) {
157 1
            throw new ActivationConditionsNotMetException($reason);
158
        }
159
    }
160
161 4
    private function checkDeactivationConditions(
162
        SpacecraftWrapperInterface $wrapper,
163
        SpacecraftSystemTypeInterface $system,
164
        SpacecraftSystemTypeEnum $type
165
    ): void {
166 4
        $ship = $wrapper->get();
167 4
        $shipSystem = $ship->getSystems()[$type->value] ?? null;
168 4
        if ($shipSystem === null) {
169
            throw new SystemNotFoundException();
170
        }
171
172 4
        $mode = $shipSystem->getMode();
173 4
        if ($mode === SpacecraftSystemModeEnum::MODE_ALWAYS_ON) {
174 1
            throw new SystemNotDeactivatableException();
175
        }
176
177
        if (
178 3
            $mode === SpacecraftSystemModeEnum::MODE_OFF
179 3
            ||  $mode === SpacecraftSystemModeEnum::MODE_ALWAYS_OFF
180
        ) {
181 1
            throw new AlreadyOffException();
182
        }
183
184 2
        $reason = '';
185 2
        if (!$system->checkDeactivationConditions($wrapper, $reason)) {
186 1
            throw new DeactivationConditionsNotMetException($reason);
187
        }
188
    }
189
190
    #[Override]
191
    public function handleDestroyedSystem(SpacecraftWrapperInterface $wrapper, SpacecraftSystemTypeEnum $type): void
192
    {
193
        $system = $this->lookupSystem($type);
194
195
        $system->handleDestruction($wrapper);
196
    }
197
198
    #[Override]
199
    public function handleDamagedSystem(SpacecraftWrapperInterface $wrapper, SpacecraftSystemTypeEnum $type): void
200
    {
201
        $system = $this->lookupSystem($type);
202
203
        $system->handleDamage($wrapper);
204
    }
205
206 3
    #[Override]
207
    public function getActiveSystems(SpacecraftInterface $ship, bool $sort = false): array
208
    {
209 3
        $activeSystems = [];
210 3
        $prioArray = [];
211 3
        foreach ($ship->getSystems() as $system) {
212 3
            if ($system->getMode()->isActivated()) {
213 3
                $activeSystems[] = $system;
214 3
                if ($sort) {
215
                    $prioArray[$system->getSystemType()->value] = $this->lookupSystem($system->getSystemType())->getPriority();
216
                }
217
            }
218
        }
219
220 3
        if ($sort) {
221
            usort(
222
                $activeSystems,
223
                fn(SpacecraftSystemInterface $a, SpacecraftSystemInterface $b): int => $prioArray[$a->getSystemType()->value] <=> $prioArray[$b->getSystemType()->value]
224
            );
225
        }
226
227 3
        return $activeSystems;
228
    }
229
}
230