Passed
Push — master ( 91454b...2408fc )
by Nico
22:07 queued 10:43
created

SpacecraftStateChanger::changeState()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 19.5936

Importance

Changes 0
Metric Value
cc 12
eloc 24
nc 8
nop 2
dl 0
loc 35
ccs 15
cts 24
cp 0.625
crap 19.5936
rs 6.9666
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\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 13
    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 13
    ) {}
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 6
    #[Override]
72
    public function changeAlertState(
73
        SpacecraftWrapperInterface $wrapper,
74
        SpacecraftAlertStateEnum $alertState
75
    ): ?string {
76 6
        $ship = $wrapper->get();
77
78 6
        $msg = null;
79
80 6
        $currentAlertState = $ship->getAlertState();
81
82
        //nothing to do
83 6
        if ($currentAlertState === $alertState) {
84 1
            return null;
85
        }
86
87
        //check if enough energy
88
        if (
89 5
            $alertState == SpacecraftAlertStateEnum::ALERT_YELLOW
90 5
            && $currentAlertState == SpacecraftAlertStateEnum::ALERT_GREEN
91
        ) {
92 2
            $this->consumeEnergyForAlertChange($wrapper, SpacecraftStateChangerInterface::ALERT_YELLOW_EPS_USAGE);
93
        }
94
        if (
95 4
            $alertState == SpacecraftAlertStateEnum::ALERT_RED
96 4
            && $currentAlertState !== SpacecraftAlertStateEnum::ALERT_RED
97
        ) {
98 2
            $this->consumeEnergyForAlertChange($wrapper, SpacecraftStateChangerInterface::ALERT_RED_EPS_USAGE);
99
        }
100
101
        // cancel repair if not on alert green
102 3
        if ($alertState !== SpacecraftAlertStateEnum::ALERT_GREEN && $this->cancelRepair->cancelRepair($ship)) {
103 1
            $msg = _('Die Reparatur wurde abgebrochen');
104
        }
105
106
        if (
107 3
            $alertState !== SpacecraftAlertStateEnum::ALERT_GREEN
108 3
            && $ship instanceof ShipInterface
109 3
            && $this->cancelRetrofit->cancelRetrofit($ship)
110
        ) {
111
            $msg = _('Die Umrüstung wurde abgebrochen');
112
        }
113
114
        // now change
115 3
        $ship->setAlertState($alertState);
116
117 3
        return $msg;
118
    }
119
120 4
    private function consumeEnergyForAlertChange(SpacecraftWrapperInterface $wrapper, int $amount): void
121
    {
122 4
        $eps = $wrapper->getEpsSystemData();
123
124 4
        if ($eps === null || $eps->getEps() < $amount) {
125 2
            throw new InsufficientEnergyException($amount);
126
        }
127 2
        $eps->lowerEps($amount)->update();
128
    }
129
}
130