Passed
Pull Request — master (#2331)
by Janko
11:37 queued 06:03
created

AlertStateManager   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 78
dl 0
loc 153
ccs 75
cts 87
cp 0.8621
rs 10
c 1
b 0
f 0
wmc 28

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setAlertYellow() 0 8 2
B setAlertStateShip() 0 42 7
B setAlertStateFleet() 0 33 8
A __construct() 0 6 1
A setAlertState() 0 22 5
A setAlertRed() 0 11 2
A setAlertGreen() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Spacecraft\System\Control;
6
7
use BadMethodCallException;
8
use Stu\Component\Spacecraft\SpacecraftAlertStateEnum;
9
use Stu\Component\Spacecraft\System\Exception\InsufficientEnergyException;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Spacecraft\Lib\SpacecraftLoaderInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
15
final class AlertStateManager implements AlertStateManagerInterface
16
{
17
    use GetTargetWrapperTrait;
18
19
    /** @param SpacecraftLoaderInterface<SpacecraftWrapperInterface> $spacecraftLoader*/
20 4
    public function __construct(
21
        private readonly SpacecraftLoaderInterface $spacecraftLoader,
22
        private readonly SystemActivation $systemActivation,
23
        private readonly SystemDeactivation $systemDeactivation,
24
        private readonly GameControllerInterface $game
25 4
    ) {}
26
27 7
    #[\Override]
28
    public function setAlertState(
29
        SpacecraftWrapperInterface|int $target,
30
        SpacecraftAlertStateEnum $alertState
31
    ): void {
32 7
        $wrapper = $this->getTargetWrapper(
33 7
            $target,
34 7
            false,
35 7
            $this->spacecraftLoader,
36 7
            $this->game
37 7
        );
38
39 7
        if (!$this->setAlertStateShip($wrapper, $alertState)) {
40
            return;
41
        }
42
43 7
        if ($alertState === SpacecraftAlertStateEnum::ALERT_RED) {
44 3
            $this->game->getInfo()->addInformation("Die Alarmstufe wurde auf [b][color=red]Rot[/color][/b] geändert");
45 4
        } elseif ($alertState === SpacecraftAlertStateEnum::ALERT_YELLOW) {
46 2
            $this->game->getInfo()->addInformation("Die Alarmstufe wurde auf [b][color=yellow]Gelb[/color][/b] geändert");
47 2
        } elseif ($alertState === SpacecraftAlertStateEnum::ALERT_GREEN) {
48 2
            $this->game->getInfo()->addInformation("Die Alarmstufe wurde auf [b][color=green]Grün[/color][/b] geändert");
49
        }
50
    }
51
52 3
    #[\Override]
53
    public function setAlertStateFleet(
54
        int $shipId,
55
        SpacecraftAlertStateEnum $alertState
56
    ): void {
57 3
        $userId = $this->game->getUser()->getId();
58
59 3
        $wrapper = $this->spacecraftLoader->getWrapperByIdAndUser(
60 3
            $shipId,
61 3
            $userId
62 3
        );
63
64 3
        $fleetWrapper = $wrapper->getFleetWrapper();
65 3
        if ($fleetWrapper === null) {
66
            throw new BadMethodCallException('ship not in fleet');
67
        }
68
69 3
        $success = false;
70 3
        foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
71 3
            $success = $this->setAlertStateShip($wrapper, $alertState) || $success;
72
        }
73
74
        // only show info if at least one ship was able to change
75 3
        if (!$success) {
76
            return;
77
        }
78
79 3
        if ($alertState === SpacecraftAlertStateEnum::ALERT_RED) {
80 1
            $this->game->getInfo()->addInformation(_('Flottenbefehl ausgeführt: Alarmstufe [b][color=red]Rot[/color][/b]'));
81 2
        } elseif ($alertState === SpacecraftAlertStateEnum::ALERT_YELLOW) {
82 1
            $this->game->getInfo()->addInformation(_('Flottenbefehl ausgeführt: Alarmstufe [b][color=yellow]Gelb[/color][/b]'));
83 1
        } elseif ($alertState === SpacecraftAlertStateEnum::ALERT_GREEN) {
84 1
            $this->game->getInfo()->addInformation(_('Flottenbefehl ausgeführt: Alarmstufe [b][color=green]Grün[/color][/b]'));
85
        }
86
    }
87
88 10
    private function setAlertStateShip(SpacecraftWrapperInterface $wrapper, SpacecraftAlertStateEnum $alertState): bool
89
    {
90 10
        $ship = $wrapper->get();
91
92
        // station constructions can't change alert state
93 10
        if ($ship->isConstruction()) {
94
            $this->game->getInfo()->addInformationf('%s: [b][color=#ff2626]Konstrukte können die Alarmstufe nicht ändern[/color][/b]', $ship->getName());
95
            return false;
96
        }
97
98
        // can only change when there is enough crew
99 10
        if (!$ship->hasEnoughCrew()) {
100
            $this->game->getInfo()->addInformationf('%s: [b][color=#ff2626]Mangel an Crew verhindert den Wechsel der Alarmstufe[/color][/b]', $ship->getName());
101
            return false;
102
        }
103
104 10
        if ($alertState === SpacecraftAlertStateEnum::ALERT_RED && $ship->isCloaked()) {
105
            $this->game->getInfo()->addInformationf('%s: [b][color=#ff2626]Tarnung verhindert den Wechsel zu Alarm-Rot[/color][/b]', $ship->getName());
106
            return false;
107
        }
108
109
        try {
110 10
            $alertMsg = $wrapper->setAlertState($alertState);
111
112 10
            if ($alertMsg !== null) {
113 10
                $this->game->getInfo()->addInformationf('%s: [b][color=FAFA03]%s[/color][/b]', $ship->getName(), $alertMsg);
114
            }
115
        } catch (InsufficientEnergyException $e) {
116
            $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());
117
            return false;
118
        }
119
120 10
        match ($alertState) {
121 10
            SpacecraftAlertStateEnum::ALERT_RED =>
122 4
            $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...
123 10
            SpacecraftAlertStateEnum::ALERT_YELLOW =>
124 3
            $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...
125 10
            SpacecraftAlertStateEnum::ALERT_GREEN =>
126 3
            $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...
127 10
        };
128
129 10
        return true;
130
    }
131
132 4
    private function setAlertRed(SpacecraftWrapperInterface $wrapper): void
133
    {
134 4
        $alertSystems = [
135 4
            SpacecraftSystemTypeEnum::SHIELDS,
136 4
            SpacecraftSystemTypeEnum::NBS,
137 4
            SpacecraftSystemTypeEnum::PHASER,
138 4
            SpacecraftSystemTypeEnum::TORPEDO
139 4
        ];
140
141 4
        foreach ($alertSystems as $type) {
142 4
            $this->systemActivation->activateIntern($wrapper, $type, $this->game->getInfo(), false);
143
        }
144
    }
145
146 3
    private function setAlertYellow(SpacecraftWrapperInterface $wrapper): void
147
    {
148 3
        $alertSystems = [
149 3
            SpacecraftSystemTypeEnum::NBS
150 3
        ];
151
152 3
        foreach ($alertSystems as $type) {
153 3
            $this->systemActivation->activateIntern($wrapper, $type, $this->game->getInfo(), false);
154
        }
155
    }
156
157 3
    private function setAlertGreen(SpacecraftWrapperInterface $wrapper): void
158
    {
159 3
        $deactivateSystems = [
160 3
            SpacecraftSystemTypeEnum::PHASER,
161 3
            SpacecraftSystemTypeEnum::TORPEDO,
162 3
            SpacecraftSystemTypeEnum::SHIELDS
163 3
        ];
164
165 3
        foreach ($deactivateSystems as $type) {
166 3
            if ($wrapper->get()->hasSpacecraftSystem($type)) {
167 3
                $this->systemDeactivation->deactivateIntern($wrapper, $type, $this->game->getInfo());
168
            }
169
        }
170
    }
171
}
172