Passed
Pull Request — dev (#2264)
by
unknown
05:09
created

StationConstructionHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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