Passed
Push — master ( 77a570...adeb98 )
by Nico
26:43
created

ShipStateChanger::changeAlertState()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 43
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10.017

Importance

Changes 0
Metric Value
cc 10
eloc 20
nc 17
nop 2
dl 0
loc 43
ccs 17
cts 18
cp 0.9444
crap 10.017
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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