Passed
Push — dev ( 996fbb...25004f )
by Janko
16:40
created

deactivateFleetIntern()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 7
nop 3
dl 0
loc 29
ccs 0
cts 15
cp 0
crap 30
rs 9.5222
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Spacecraft\System\Control;
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 RuntimeException;
9
use Stu\Component\Spacecraft\SpacecraftLssModeEnum;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Lib\Information\InformationInterface;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Ship\Lib\ShipWrapperInterface;
14
use Stu\Module\Spacecraft\Lib\Movement\Component\PreFlight\ConditionCheckResult;
15
use Stu\Module\Spacecraft\Lib\SpacecraftLoaderInterface;
16
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
17
18
final class ActivatorDeactivatorHelper implements ActivatorDeactivatorHelperInterface
19
{
20
    use GetTargetWrapperTrait;
21
22
    /** @param SpacecraftLoaderInterface<SpacecraftWrapperInterface> $spacecraftLoader*/
23 1
    public function __construct(
24
        private readonly SpacecraftLoaderInterface $spacecraftLoader,
25
        private readonly SystemActivation $systemActivation,
26
        private readonly SystemDeactivation $systemDeactivation,
27
        private readonly GameControllerInterface $game
28 1
    ) {}
29
30
    #[Override]
31
    public function activate(
32
        SpacecraftWrapperInterface|int $target,
33
        spacecraftSystemTypeEnum $type,
34
        ConditionCheckResult|InformationInterface $logger,
35
        bool $allowUplink = false,
36
        bool $isDryRun = false
37
    ): bool {
38
        $wrapper = $this->getTargetWrapper(
39
            $target,
40
            $allowUplink,
41
            $this->spacecraftLoader,
42
            $this->game->getUser()
43
        );
44
45
        return $this->systemActivation->activateIntern($wrapper, $type, $logger, $isDryRun);
46
    }
47
48
    #[Override]
49
    public function activateFleet(
50
        int $shipId,
51
        spacecraftSystemTypeEnum $type,
52
        GameControllerInterface $game
53
    ): void {
54
        $userId = $game->getUser()->getId();
55
56
        $wrapper = $this->spacecraftLoader->getWrapperByIdAndUser(
57
            $shipId,
58
            $userId
59
        );
60
61
        $fleetWrapper = $wrapper->getFleetWrapper();
62
        if ($fleetWrapper === null) {
63
            throw new RuntimeException('ship not in fleet');
64
        }
65
66
        $success = false;
67
        foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
68
            if ($this->systemActivation->activateIntern($wrapper, $type, $game, false)) {
69
                $success = true;
70
            }
71
        }
72
73
        // only show info if at least one ship was able to change
74
        if (!$success) {
75
            return;
76
        }
77
78
        $game->addInformation(sprintf(
79
            _('Flottenbefehl ausgeführt: System %s aktiviert'),
80
            $type->getDescription()
81
        ));
82
    }
83
84
    #[Override]
85
    public function deactivate(
86
        SpacecraftWrapperInterface|int $target,
87
        spacecraftSystemTypeEnum $type,
88
        InformationInterface $informations,
89
        bool $allowUplink = false
90
    ): bool {
91
        $wrapper = $this->getTargetWrapper(
92
            $target,
93
            $allowUplink,
94
            $this->spacecraftLoader,
95
            $this->game->getUser()
96
        );
97
98
        return $this->systemDeactivation->deactivateIntern($wrapper, $type, $informations);
99
    }
100
101
    #[Override]
102
    public function deactivateFleet(
103
        ShipWrapperInterface|int $target,
104
        spacecraftSystemTypeEnum $type,
105
        InformationInterface $informations
106
    ): bool {
107
        $wrapper = $this->getTargetWrapper(
108
            $target,
109
            false,
110
            $this->spacecraftLoader,
111
            $this->game->getUser()
112
        );
113
114
        if (!$wrapper instanceof ShipWrapperInterface) {
115
            throw new RuntimeException('not a ship!');
116
        }
117
118
        return $this->deactivateFleetIntern($wrapper, $type, $informations);
119
    }
120
121
    private function deactivateFleetIntern(
122
        ShipWrapperInterface $wrapper,
123
        spacecraftSystemTypeEnum $type,
124
        InformationInterface $informations
125
    ): bool {
126
127
        $fleetWrapper = $wrapper->getFleetWrapper();
128
        if ($fleetWrapper === null) {
129
            throw new RuntimeException('ship not in fleet');
130
        }
131
132
        $success = false;
133
        foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
134
            if ($this->systemDeactivation->deactivateIntern($wrapper, $type, $informations)) {
135
                $success = true;
136
            }
137
        }
138
139
        // only show info if at least one ship was able to change
140
        if (!$success) {
141
            return false;
142
        }
143
144
        $informations->addInformationf(
145
            'Flottenbefehl ausgeführt: System %s deaktiviert',
146
            $type->getDescription()
147
        );
148
149
        return true;
150
    }
151
152
    public function setLssMode(
153
        int $shipId,
154
        SpacecraftLssModeEnum $lssMode,
155
        GameControllerInterface $game
156
    ): void {
157
158
        $lss = $this->getTargetWrapper(
159
            $shipId,
160
            true,
161
            $this->spacecraftLoader,
162
            $this->game->getUser()
163
        )->getLssSystemData();
164
        if ($lss === null) {
165
            throw new RuntimeException('this should not happen!');
166
        }
167
168
        $lss->setMode($lssMode)->update();
169
170
        if ($lssMode->isBorderMode()) {
171
            $game->addInformationf("%s für die Langstreckensensoren aktiviert", $lssMode->getDescription());
172
        } else {
173
            $game->addInformation("Filter für Langstreckensensoren wurde deaktiviert");
174
        }
175
    }
176
}
177