Passed
Push — master ( f4068b...77f637 )
by Nico
40:27 queued 14:09
created

ShipStateChanger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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