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