Passed
Pull Request — master (#2331)
by Janko
24:19 queued 18:44
created

SpacecraftStateChanger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 6
dl 0
loc 8
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib;
6
7
use Stu\Component\Ship\Mining\CancelMiningInterface;
8
use Stu\Component\Spacecraft\Repair\CancelRepairInterface;
9
use Stu\Component\Ship\Retrofit\CancelRetrofitInterface;
10
use Stu\Component\Spacecraft\SpacecraftAlertStateEnum;
11
use Stu\Component\Spacecraft\SpacecraftStateEnum;
12
use Stu\Component\Spacecraft\System\Exception\InsufficientEnergyException;
13
use Stu\Module\Ship\Lib\AstroEntryLibInterface;
14
use Stu\Module\Ship\Lib\ShipWrapperInterface;
15
use Stu\Module\Spacecraft\Lib\Interaction\ShipTakeoverManagerInterface;
16
use Stu\Module\Ship\Lib\TholianWebUtilInterface;
17
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
18
use Stu\Orm\Entity\Ship;
19
20
final class SpacecraftStateChanger implements SpacecraftStateChangerInterface
21
{
22 14
    public function __construct(
23
        private CancelMiningInterface $cancelMining,
24
        private CancelRepairInterface $cancelRepair,
25
        private AstroEntryLibInterface $astroEntryLib,
26
        private TholianWebUtilInterface $tholianWebUtil,
27
        private ShipTakeoverManagerInterface $shipTakeoverManager,
28
        private CancelRetrofitInterface $cancelRetrofit
29 14
    ) {}
30
31 11
    #[\Override]
32
    public function changeState(SpacecraftWrapperInterface $wrapper, SpacecraftStateEnum $newState): void
33
    {
34 11
        $ship = $wrapper->get();
35 11
        $currentState = $ship->getState();
36
37
        //nothing to do
38
        if (
39 11
            $currentState === SpacecraftStateEnum::DESTROYED
40 11
            || $currentState === $newState
41
        ) {
42 8
            return;
43
        }
44
45
        //repair stuff
46 4
        if ($currentState->isRepairState()) {
47 1
            $this->cancelRepair->cancelRepair($ship);
48 3
        } elseif ($currentState === SpacecraftStateEnum::ASTRO_FINALIZING) {
49 1
            $this->astroEntryLib->cancelAstroFinalizing($wrapper);
50 2
        } elseif ($currentState === SpacecraftStateEnum::RETROFIT && $ship instanceof Ship) {
51
            $this->cancelRetrofit->cancelRetrofit($ship);
52 2
        } elseif ($currentState === SpacecraftStateEnum::WEB_SPINNING && $wrapper instanceof ShipWrapperInterface) {
53 1
            $this->tholianWebUtil->releaseWebHelper($wrapper);
54 1
        } elseif ($currentState === SpacecraftStateEnum::ACTIVE_TAKEOVER) {
55
            $this->shipTakeoverManager->cancelTakeover(
56
                $ship->getTakeoverActive(),
57
                null,
58
                true
59
            );
60 1
        } elseif ($currentState === SpacecraftStateEnum::GATHER_RESOURCES && $wrapper instanceof ShipWrapperInterface) {
61
            $this->cancelMining->cancelMining($wrapper);
62
        }
63
64 4
        $ship->getCondition()->setState($newState);
65
    }
66
67 15
    #[\Override]
68
    public function changeAlertState(
69
        SpacecraftWrapperInterface $wrapper,
70
        SpacecraftAlertStateEnum $alertState
71
    ): ?string {
72
73 15
        $currentAlertState = $wrapper->getAlertState();
74
75
        //nothing to do
76 15
        if ($currentAlertState === $alertState) {
77 4
            return null;
78
        }
79
80 12
        if (!$wrapper->get()->hasComputer()) {
81 1
            return null;
82
        }
83
84
        //check if enough energy
85
        if (
86 11
            $alertState === SpacecraftAlertStateEnum::ALERT_YELLOW
87 11
            && $currentAlertState === SpacecraftAlertStateEnum::ALERT_GREEN
88
        ) {
89 3
            $this->consumeEnergyForAlertChange($wrapper, $alertState->getEpsUsage());
90
        }
91
        if (
92 10
            $alertState === SpacecraftAlertStateEnum::ALERT_RED
93 10
            && $currentAlertState !== SpacecraftAlertStateEnum::ALERT_RED
94
        ) {
95 4
            $this->consumeEnergyForAlertChange($wrapper, $alertState->getEpsUsage());
96
        }
97
98 9
        $msg = null;
99 9
        $ship = $wrapper->get();
100
101
        // cancel repair if not on alert green
102 9
        if ($alertState !== SpacecraftAlertStateEnum::ALERT_GREEN && $this->cancelRepair->cancelRepair($ship)) {
103 1
            $msg = _('Die Reparatur wurde abgebrochen');
104
        }
105
106
        if (
107 9
            $alertState !== SpacecraftAlertStateEnum::ALERT_GREEN
108 9
            && $ship instanceof Ship
109 9
            && $this->cancelRetrofit->cancelRetrofit($ship)
110
        ) {
111
            $msg = _('Die Umrüstung wurde abgebrochen');
112
        }
113
114
        // now change
115 9
        $wrapper->getComputerSystemDataMandatory()->setAlertState($alertState)->update();
116
117 9
        return $msg;
118
    }
119
120 7
    private function consumeEnergyForAlertChange(SpacecraftWrapperInterface $wrapper, int $amount): void
121
    {
122 7
        $eps = $wrapper->getEpsSystemData();
123
124 7
        if ($eps === null || $eps->getEps() < $amount) {
125 2
            throw new InsufficientEnergyException($amount);
126
        }
127 5
        $eps->lowerEps($amount)->update();
128
    }
129
}
130