Passed
Push — dev ( 302539...72decd )
by Janko
05:12
created

StationConstructionHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 12.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 73
ccs 5
cts 39
cp 0.1282
rs 10
c 1
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B doConstructionStuff() 0 53 10
A __construct() 0 3 1
A handleSpacecraftTick() 0 11 3
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
    private function doConstructionStuff(StationWrapperInterface $wrapper, InformationInterface $information): bool
34
    {
35
        $station = $wrapper->get();
36
37
        $progress =  $station->getConstructionProgress();
38
        if ($progress === null) {
39
            return false;
40
        }
41
42
        if ($progress->getRemainingTicks() === 0) {
43
            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