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

SpacecraftStateChanger::changeAlertState()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 12.0135

Importance

Changes 0
Metric Value
cc 12
eloc 25
nc 18
nop 2
dl 0
loc 51
ccs 21
cts 22
cp 0.9545
crap 12.0135
rs 6.9666
c 0
b 0
f 0

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