Passed
Push — master ( 92a2e9...9d73f4 )
by Nico
41:55
created

ShipStateChanger::consumeEnergyForAlertChange()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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