Passed
Pull Request — dev (#2304)
by
unknown
05:16
created

StationConstructionHandler::doConstructionStuff()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 53
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 64.7745

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 32
nc 10
nop 2
dl 0
loc 53
ccs 6
cts 33
cp 0.1818
crap 64.7745
rs 7.6666
c 1
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
namespace Stu\Module\Tick\Spacecraft\Handler;
4
5
use Stu\Component\Spacecraft\SpacecraftStateEnum;
6
use Stu\Component\Station\StationUtilityInterface;
7
use Stu\Lib\Information\InformationInterface;
8
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
9
use Stu\Module\Station\Lib\StationWrapperInterface;
10
use Stu\Module\Tick\Spacecraft\SpacecraftTickFinishedException;
11
12
class StationConstructionHandler implements SpacecraftTickHandlerInterface
13
{
14 1
    public function __construct(
15
        private StationUtilityInterface $stationUtility
16 1
    ) {}
17
18 1
    #[\Override]
19
    public function handleSpacecraftTick(
20
        SpacecraftWrapperInterface $wrapper,
21
        InformationInterface $information
22
    ): void {
23
24
        if (
25 1
            $wrapper instanceof StationWrapperInterface
26 1
            && $this->doConstructionStuff($wrapper, $information)
27
        ) {
28
            throw new SpacecraftTickFinishedException();
29
        }
30
    }
31
32 1
    private function doConstructionStuff(StationWrapperInterface $wrapper, InformationInterface $information): bool
33
    {
34 1
        $station = $wrapper->get();
35
36 1
        $progress =  $station->getConstructionProgress();
37 1
        if ($progress === null) {
38
            return false;
39
        }
40
41 1
        if ($progress->getRemainingTicks() === 0) {
42 1
            return false;
43
        }
44
45
        $isUnderConstruction = $station->getState() === SpacecraftStateEnum::UNDER_CONSTRUCTION;
46
47
        if (!$this->stationUtility->hasEnoughDockedWorkbees($station, $station->getRump())) {
48
            $neededWorkbees = $isUnderConstruction ? $station->getRump()->getNeededWorkbees() :
49
                (int)ceil($station->getRump()->getNeededWorkbees() / 2);
50
51
            $information->addInformationf(
52
                'Nicht genügend Workbees (%d/%d) angedockt um %s weiterführen zu können',
53
                $this->stationUtility->getDockedWorkbeeCount($station),
54
                $neededWorkbees ?? 0,
55
                $isUnderConstruction ? 'den Bau' : 'die Demontage'
56
            );
57
            return true;
58
        }
59
60
        if ($isUnderConstruction) {
61
            // raise hull
62
            $increase = (int)ceil($station->getMaxHull() / (2 * $station->getRump()->getBuildtime()));
63
            $station->getCondition()->changeHull($increase);
64
        }
65
66
        if ($progress->getRemainingTicks() === 1) {
67
68
            $information->addInformationf(
69
                '%s: %s bei %s fertiggestellt',
70
                $station->getRump()->getName(),
71
                $isUnderConstruction ? 'Bau' : 'Demontage',
72
                $station->getSectorString()
73
            );
74
75
            if ($isUnderConstruction) {
76
                $this->stationUtility->finishStation($progress);
77
            } else {
78
                $this->stationUtility->finishScrapping($progress, $information);
79
            }
80
        } else {
81
            $this->stationUtility->reduceRemainingTicks($progress);
82
        }
83
84
        return true;
85
    }
86
}
87