Passed
Push — dev ( 5b1d95...923b08 )
by Janko
23:33 queued 07:31
created

StationPassiveRepairHandler::doRepairStation()   B

Complexity

Conditions 9
Paths 30

Size

Total Lines 67
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 38
c 1
b 0
f 0
nc 30
nop 2
dl 0
loc 67
ccs 0
cts 41
cp 0
crap 90
rs 7.7564

How to fix   Long Method   

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\Repair\RepairUtilInterface;
7
use Stu\Component\Spacecraft\SpacecraftStateEnum;
8
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
9
use Stu\Component\Station\StationUtilityInterface;
10
use Stu\Lib\Information\InformationInterface;
11
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
12
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
13
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
use Stu\Module\Station\Lib\StationWrapperInterface;
16
17
class StationPassiveRepairHandler implements SpacecraftTickHandlerInterface
18
{
19 1
    public function __construct(
20
        private StationUtilityInterface $stationUtility,
21
        private RepairUtilInterface $repairUtil,
22
        private SpacecraftSystemManagerInterface $spacecraftSystemManager,
23
        private PrivateMessageSenderInterface $privateMessageSender
24 1
    ) {}
25
26
    #[Override]
27
    public function handleSpacecraftTick(
28
        SpacecraftWrapperInterface $wrapper,
29
        InformationInterface $information
30
    ): void {
31
32
        $spacecraft = $wrapper->get();
33
34
        if (
35
            $wrapper instanceof StationWrapperInterface
36
            && $spacecraft->getState() === SpacecraftStateEnum::REPAIR_PASSIVE
37
        ) {
38
            $this->doRepairStation($wrapper, $information);
39
        }
40
    }
41
42
    private function doRepairStation(StationWrapperInterface $wrapper, InformationInterface $information): void
43
    {
44
        $station = $wrapper->get();
45
46
        if (!$this->stationUtility->hasEnoughDockedWorkbees($station, $station->getRump())) {
47
            $neededWorkbees = (int)ceil($station->getRump()->getNeededWorkbees() / 5);
48
49
            $information->addInformationf(
50
                'Nicht genügend Workbees (%d/%d) angedockt um die Reparatur weiterführen zu können',
51
                $this->stationUtility->getDockedWorkbeeCount($station),
52
                $neededWorkbees
53
            );
54
            return;
55
        }
56
57
        $neededParts = $this->repairUtil->determineSpareParts($wrapper, true);
58
59
        // parts stored?
60
        if (!$this->repairUtil->enoughSparePartsOnEntity($neededParts, $station, $station)) {
61
            return;
62
        }
63
64
        //repair hull
65
        $station->setHuell($station->getHull() + $station->getRepairRate());
66
        if ($station->getHull() > $station->getMaxHull()) {
67
            $station->setHuell($station->getMaxHull());
68
        }
69
70
        //repair station systems
71
        $damagedSystems = $wrapper->getDamagedSystems();
72
        if ($damagedSystems !== []) {
73
            $firstSystem = $damagedSystems[0];
74
            $firstSystem->setStatus(100);
75
76
            if ($station->getCrewCount() > 0) {
77
                $firstSystem->setMode($this->spacecraftSystemManager->lookupSystem($firstSystem->getSystemType())->getDefaultMode());
78
            }
79
80
            // maximum of two systems get repaired
81
            if (count($damagedSystems) > 1) {
82
                $secondSystem = $damagedSystems[1];
83
                $secondSystem->setStatus(100);
84
85
                if ($station->getCrewCount() > 0) {
86
                    $secondSystem->setMode($this->spacecraftSystemManager->lookupSystem($secondSystem->getSystemType())->getDefaultMode());
87
                }
88
            }
89
        }
90
91
        // consume spare parts
92
        $this->repairUtil->consumeSpareParts($neededParts, $station);
93
94
        if (!$wrapper->canBeRepaired()) {
95
            $station->setHuell($station->getMaxHull());
96
            $station->setState(SpacecraftStateEnum::NONE);
97
98
            $shipOwnerMessage = sprintf(
99
                "Die Reparatur der %s wurde in Sektor %s fertiggestellt",
100
                $station->getName(),
101
                $station->getSectorString()
102
            );
103
104
            $this->privateMessageSender->send(
105
                UserEnum::USER_NOONE,
106
                $station->getUser()->getId(),
107
                $shipOwnerMessage,
108
                PrivateMessageFolderTypeEnum::SPECIAL_STATION
109
            );
110
        }
111
    }
112
}
113