Passed
Push — master ( 8cd431...bc71f5 )
by Janko
09:12
created

AlertStateManager::setAlertGreen()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 3
rs 10
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 BadMethodCallException;
8
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...
9
use Stu\Component\Spacecraft\SpacecraftAlertStateEnum;
10
use Stu\Component\Spacecraft\System\Exception\InsufficientEnergyException;
11
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftLoaderInterface;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
16
17
final class AlertStateManager implements AlertStateManagerInterface
18
{
19
    use GetTargetWrapperTrait;
20
21
    /** @param SpacecraftLoaderInterface<SpacecraftWrapperInterface> $spacecraftLoader*/
22 4
    public function __construct(
23
        private readonly SpacecraftLoaderInterface $spacecraftLoader,
24
        private readonly SpacecraftRepositoryInterface $spacecraftRepository,
25
        private readonly SystemActivation $systemActivation,
26
        private readonly SystemDeactivation $systemDeactivation,
27
        private readonly GameControllerInterface $game
28 4
    ) {}
29
30 3
    #[Override]
31
    public function setAlertState(
32
        SpacecraftWrapperInterface|int $target,
33
        SpacecraftAlertStateEnum $alertState
34
    ): void {
35 3
        $wrapper = $this->getTargetWrapper(
36 3
            $target,
37 3
            false,
38 3
            $this->spacecraftLoader,
39 3
            $this->game
40 3
        );
41
42 3
        if (!$this->setAlertStateShip($wrapper, $alertState)) {
43
            return;
44
        }
45
46 3
        if ($alertState === SpacecraftAlertStateEnum::ALERT_RED) {
47 1
            $this->game->getInfo()->addInformation("Die Alarmstufe wurde auf [b][color=red]Rot[/color][/b] geändert");
48 2
        } elseif ($alertState === SpacecraftAlertStateEnum::ALERT_YELLOW) {
49 1
            $this->game->getInfo()->addInformation("Die Alarmstufe wurde auf [b][color=yellow]Gelb[/color][/b] geändert");
50 1
        } elseif ($alertState === SpacecraftAlertStateEnum::ALERT_GREEN) {
51 1
            $this->game->getInfo()->addInformation("Die Alarmstufe wurde auf [b][color=green]Grün[/color][/b] geändert");
52
        }
53
    }
54
55
    #[Override]
56
    public function setAlertStateFleet(
57
        int $shipId,
58
        SpacecraftAlertStateEnum $alertState
59
    ): void {
60
        $userId = $this->game->getUser()->getId();
61
62
        $wrapper = $this->spacecraftLoader->getWrapperByIdAndUser(
63
            $shipId,
64
            $userId
65
        );
66
67
        $fleetWrapper = $wrapper->getFleetWrapper();
68
        if ($fleetWrapper === null) {
69
            throw new BadMethodCallException('ship not in fleet');
70
        }
71
72
        $success = false;
73
        foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
74
            $success = $this->setAlertStateShip($wrapper, $alertState) || $success;
75
        }
76
77
        // only show info if at least one ship was able to change
78
        if (!$success) {
79
            return;
80
        }
81
82
        if ($alertState === SpacecraftAlertStateEnum::ALERT_RED) {
83
            $this->game->getInfo()->addInformation(_('Flottenbefehl ausgeführt: Alarmstufe [b][color=red]Rot[/color][/b]'));
84
        } elseif ($alertState === SpacecraftAlertStateEnum::ALERT_YELLOW) {
85
            $this->game->getInfo()->addInformation(_('Flottenbefehl ausgeführt: Alarmstufe [b][color=yellow]Gelb[/color][/b]'));
86
        } elseif ($alertState === SpacecraftAlertStateEnum::ALERT_GREEN) {
87
            $this->game->getInfo()->addInformation(_('Flottenbefehl ausgeführt: Alarmstufe [b][color=green]Grün[/color][/b]'));
88
        }
89
    }
90
91 3
    private function setAlertStateShip(SpacecraftWrapperInterface $wrapper, SpacecraftAlertStateEnum $alertState): bool
92
    {
93 3
        $ship = $wrapper->get();
94
95
        // station constructions can't change alert state
96 3
        if ($ship->isConstruction()) {
97
            $this->game->getInfo()->addInformationf('%s: [b][color=#ff2626]Konstrukte können die Alarmstufe nicht ändern[/color][/b]', $ship->getName());
98
            return false;
99
        }
100
101
        // can only change when there is enough crew
102 3
        if (!$ship->hasEnoughCrew()) {
103
            $this->game->getInfo()->addInformationf('%s: [b][color=#ff2626]Mangel an Crew verhindert den Wechsel der Alarmstufe[/color][/b]', $ship->getName());
104
            return false;
105
        }
106
107 3
        if ($alertState === SpacecraftAlertStateEnum::ALERT_RED && $ship->isCloaked()) {
108
            $this->game->getInfo()->addInformationf('%s: [b][color=#ff2626]Tarnung verhindert den Wechsel zu Alarm-Rot[/color][/b]', $ship->getName());
109
            return false;
110
        }
111
112
        try {
113 3
            $alertMsg = $wrapper->setAlertState($alertState);
114 3
            $this->spacecraftRepository->save($ship);
115
116 3
            if ($alertMsg !== null) {
117 3
                $this->game->getInfo()->addInformationf('%s: [b][color=FAFA03]%s[/color][/b]', $ship->getName(), $alertMsg);
118
            }
119
        } catch (InsufficientEnergyException $e) {
120
            $this->game->getInfo()->addInformationf('%s: [b][color=#ff2626]Nicht genügend Energie um die Alarmstufe zu wechseln (%d benötigt)[/color][/b]', $ship->getName(), $e->getNeededEnergy());
121
            return false;
122
        }
123
124 3
        match ($alertState) {
125 3
            SpacecraftAlertStateEnum::ALERT_RED =>
126 1
            $this->setAlertRed($wrapper),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setAlertRed($wrapper) targeting Stu\Component\Spacecraft...eManager::setAlertRed() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
127 3
            SpacecraftAlertStateEnum::ALERT_YELLOW =>
128 1
            $this->setAlertYellow($wrapper),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setAlertYellow($wrapper) targeting Stu\Component\Spacecraft...nager::setAlertYellow() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
129 3
            SpacecraftAlertStateEnum::ALERT_GREEN =>
130 1
            $this->setAlertGreen($wrapper)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setAlertGreen($wrapper) targeting Stu\Component\Spacecraft...anager::setAlertGreen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
131 3
        };
132
133 3
        $this->spacecraftRepository->save($ship);
134
135 3
        return true;
136
    }
137
138 1
    private function setAlertRed(SpacecraftWrapperInterface $wrapper): void
139
    {
140 1
        $alertSystems = [
141 1
            SpacecraftSystemTypeEnum::SHIELDS,
142 1
            SpacecraftSystemTypeEnum::NBS,
143 1
            SpacecraftSystemTypeEnum::PHASER,
144 1
            SpacecraftSystemTypeEnum::TORPEDO
145 1
        ];
146
147 1
        foreach ($alertSystems as $type) {
148 1
            $this->systemActivation->activateIntern($wrapper, $type, $this->game->getInfo(), false);
149
        }
150
    }
151
152 1
    private function setAlertYellow(SpacecraftWrapperInterface $wrapper): void
153
    {
154 1
        $alertSystems = [
155 1
            SpacecraftSystemTypeEnum::NBS
156 1
        ];
157
158 1
        foreach ($alertSystems as $type) {
159 1
            $this->systemActivation->activateIntern($wrapper, $type, $this->game->getInfo(), false);
160
        }
161
    }
162
163 1
    private function setAlertGreen(SpacecraftWrapperInterface $wrapper): void
164
    {
165 1
        $deactivateSystems = [
166 1
            SpacecraftSystemTypeEnum::PHASER,
167 1
            SpacecraftSystemTypeEnum::TORPEDO,
168 1
            SpacecraftSystemTypeEnum::SHIELDS
169 1
        ];
170
171 1
        foreach ($deactivateSystems as $type) {
172 1
            if ($wrapper->get()->hasSpacecraftSystem($type)) {
173 1
                $this->systemDeactivation->deactivateIntern($wrapper, $type, $this->game->getInfo());
174
            }
175
        }
176
    }
177
}
178