Passed
Push — dev ( 3fd410...aa33df )
by Janko
13:32
created

AlertLevelBasedReaction::changeFromGreenToYellow()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.3244

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 2
dl 0
loc 17
ccs 8
cts 11
cp 0.7272
crap 4.3244
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Battle;
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\SpacecraftAlertStateEnum;
9
use Stu\Component\Spacecraft\System\Exception\InsufficientEnergyException;
10
use Stu\Component\Spacecraft\System\Exception\SpacecraftSystemException;
11
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
12
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
13
use Stu\Lib\Information\InformationInterface;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
17
final class AlertLevelBasedReaction implements AlertLevelBasedReactionInterface
18
{
19 9
    public function __construct(private SpacecraftSystemManagerInterface $spacecraftSystemManager) {}
20
21 7
    #[Override]
22
    public function react(SpacecraftWrapperInterface $wrapper, InformationInterface $informations): void
23
    {
24 7
        if ($this->changeFromGreenToYellow($wrapper, $informations)) {
25 1
            return;
26
        }
27
28 6
        if ($wrapper->getAlertState() === SpacecraftAlertStateEnum::ALERT_YELLOW && $this->doAlertYellowReactions($wrapper, $informations)) {
29 1
            return;
30
        }
31
32 5
        if ($wrapper->getAlertState() === SpacecraftAlertStateEnum::ALERT_RED) {
33 1
            if ($this->doAlertYellowReactions($wrapper, $informations)) {
34
                return;
35
            }
36 1
            $this->doAlertRedReactions($wrapper, $informations);
37
        }
38
    }
39
40 7
    private function changeFromGreenToYellow(SpacecraftWrapperInterface $wrapper, InformationInterface $informations): bool
41
    {
42 7
        if ($wrapper->getAlertState() == SpacecraftAlertStateEnum::ALERT_GREEN) {
43
            try {
44 1
                $alertMsg = $wrapper->setAlertState(SpacecraftAlertStateEnum::ALERT_YELLOW);
45 1
                $informations->addInformation("- Erhöhung der Alarmstufe wurde durchgeführt, Grün -> Gelb");
46 1
                if ($alertMsg !== null) {
47 1
                    $informations->addInformation("- " . $alertMsg);
48
                }
49 1
                return true;
50
            } catch (InsufficientEnergyException) {
51
                $informations->addInformation("- Nicht genügend Energie vorhanden um auf Alarm-Gelb zu wechseln");
52
                return true;
53
            }
54
        }
55
56 6
        return false;
57
    }
58
59 6
    private function doAlertYellowReactions(SpacecraftWrapperInterface $wrapper, InformationInterface $informations): bool
60
    {
61 6
        $ship = $wrapper->get();
62
63 6
        if ($ship->isCloaked()) {
64
            try {
65 1
                $this->spacecraftSystemManager->deactivate($wrapper, SpacecraftSystemTypeEnum::CLOAK);
66 1
                $informations->addInformation("- Die Tarnung wurde deaktiviert");
67
            } catch (SpacecraftSystemException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
68
            }
69
70 1
            return true;
71
        }
72
73 5
        if (!$ship->isTractoring() && (!$ship instanceof ShipInterface || !$ship->isTractored())) {
74
            try {
75 3
                $this->spacecraftSystemManager->activate($wrapper, SpacecraftSystemTypeEnum::SHIELDS);
76
77 2
                $informations->addInformation("- Die Schilde wurden aktiviert");
78 1
            } catch (SpacecraftSystemException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
79
            }
80
        } else {
81 2
            $informations->addInformation("- Die Schilde konnten wegen aktiviertem Traktorstrahl nicht aktiviert werden");
82
        }
83
        try {
84 5
            $this->spacecraftSystemManager->activate($wrapper, SpacecraftSystemTypeEnum::NBS);
85
86 4
            $informations->addInformation("- Die Nahbereichssensoren wurden aktiviert");
87 1
        } catch (SpacecraftSystemException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
88
        }
89
90
        try {
91 5
            $this->spacecraftSystemManager->activate($wrapper, SpacecraftSystemTypeEnum::PHASER);
92
93 4
            $informations->addInformation("- Die Energiewaffe wurde aktiviert");
94 1
        } catch (SpacecraftSystemException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
95
        }
96
97 5
        return false;
98
    }
99
100 1
    private function doAlertRedReactions(SpacecraftWrapperInterface $wrapper, InformationInterface $informations): void
101
    {
102
        try {
103 1
            $this->spacecraftSystemManager->activate($wrapper, SpacecraftSystemTypeEnum::TORPEDO);
104
105 1
            $informations->addInformation("- Der Torpedowerfer wurde aktiviert");
106
        } catch (SpacecraftSystemException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
107
        }
108
    }
109
}
110