Passed
Push — dev ( eeaa0f...91a85a )
by Janko
26:10
created

RepairActions::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 8
dl 0
loc 10
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Tick\Ship\ManagerComponent;
6
7
use Stu\Component\Building\BuildingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Building\BuildingEnum 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...
8
use Stu\Component\Colony\ColonyFunctionManagerInterface;
9
use Stu\Component\Ship\Repair\RepairUtilInterface;
10
use Stu\Component\Ship\ShipStateEnum;
11
use Stu\Component\Ship\System\ShipSystemManagerInterface;
12
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
13
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
14
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...
15
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
16
use Stu\Module\Ship\Lib\ShipWrapperInterface;
17
use Stu\Orm\Entity\ColonyInterface;
18
use Stu\Orm\Entity\ColonyShipRepairInterface;
19
use Stu\Orm\Entity\ShipInterface;
20
use Stu\Orm\Entity\StationShipRepairInterface;
21
use Stu\Orm\Repository\ColonyShipRepairRepositoryInterface;
22
use Stu\Orm\Repository\ShipRepositoryInterface;
23
use Stu\Orm\Repository\StationShipRepairRepositoryInterface;
24
25
final class RepairActions
26
{
27
    public function __construct(
28
        private ShipRepositoryInterface $shipRepository,
29
        private PrivateMessageSenderInterface $privateMessageSender,
30
        private ShipSystemManagerInterface $shipSystemManager,
31
        private ColonyShipRepairRepositoryInterface $colonyShipRepairRepository,
32
        private StationShipRepairRepositoryInterface $stationShipRepairRepository,
33
        private RepairUtilInterface $repairUtil,
34
        private ShipWrapperFactoryInterface $shipWrapperFactory,
35
        private ColonyFunctionManagerInterface $colonyFunctionManager
36
    ) {
37
    }
38
39
    public function repairShipOnColony(ColonyShipRepairInterface $colonyShipRepair): void
40
    {
41
        $ship = $colonyShipRepair->getShip();
42
        $colony = $colonyShipRepair->getColony();
43
44
        $isRepairStationBonus = $this->colonyFunctionManager->hasActiveFunction($colony, BuildingEnum::BUILDING_FUNCTION_REPAIR_SHIPYARD);
45
        if ($this->repairShipOnEntity($ship, $colony, $isRepairStationBonus)) {
46
            $this->colonyShipRepairRepository->delete($colonyShipRepair);
47
            $this->shipRepository->save($ship);
48
        }
49
    }
50
51
    public function repairShipOnStation(StationShipRepairInterface $stationShipRepair): void
0 ignored issues
show
Unused Code introduced by
The parameter $stationShipRepair is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
    public function repairShipOnStation(/** @scrutinizer ignore-unused */ StationShipRepairInterface $stationShipRepair): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        foreach ($this->stationShipRepairRepository->getMostRecentJobs() as $obj) {
54
            $ship = $obj->getShip();
55
            $station = $obj->getStation();
56
57
            if (!$station->hasEnoughCrew()) {
58
                continue;
59
            }
60
61
            if ($this->repairShipOnEntity($ship, $station, false)) {
62
                $this->stationShipRepairRepository->delete($obj);
63
                $this->shipRepository->save($ship);
64
            }
65
        }
66
    }
67
68
    private function repairShipOnEntity(ShipInterface $ship, ColonyInterface|ShipInterface $entity, bool $isRepairStationBonus): bool
69
    {
70
        // check for U-Mode
71
        if ($entity->getUser()->isVacationRequestOldEnough()) {
72
            return false;
73
        }
74
75
        $wrapper = $this->shipWrapperFactory->wrapShip($ship);
76
        $neededParts = $this->repairUtil->determineSpareParts($wrapper, true);
77
78
        // parts stored?
79
        if (!$this->repairUtil->enoughSparePartsOnEntity($neededParts, $entity, $ship)) {
80
            return false;
81
        }
82
83
        $repairFinished = false;
84
85
        $this->repairHull($ship, $isRepairStationBonus);
86
        $this->repairShipSystems($wrapper, $isRepairStationBonus);
87
88
        // consume spare parts
89
        $this->repairUtil->consumeSpareParts($neededParts, $entity);
90
91
        if (!$wrapper->canBeRepaired()) {
92
            $repairFinished = true;
93
94
            $ship->setHull($ship->getMaxHull());
95
            $ship->setState(ShipStateEnum::SHIP_STATE_NONE);
96
97
            $this->sendPrivateMessages($ship, $entity);
98
        }
99
        $this->shipRepository->save($ship);
100
101
        return $repairFinished;
102
    }
103
104
    private function repairHull(ShipInterface $ship, bool $isRepairStationBonus): void
105
    {
106
        $hullRepairRate = $isRepairStationBonus ? $ship->getRepairRate() * 2 : $ship->getRepairRate();
107
        $ship->setHull($ship->getHull() + $hullRepairRate);
108
        if ($ship->getHull() > $ship->getMaxHull()) {
109
            $ship->setHull($ship->getMaxHull());
110
        }
111
    }
112
113
    private function repairShipSystems(ShipWrapperInterface $wrapper, bool $isRepairStationBonus): void
114
    {
115
        $ship = $wrapper->get();
116
117
        $damagedSystems = $wrapper->getDamagedSystems();
118
        if ($damagedSystems !== []) {
119
            $firstSystem = $damagedSystems[0];
120
            $firstSystem->setStatus(100);
121
122
            if ($ship->getCrewCount() > 0) {
123
                $firstSystem->setMode($this->shipSystemManager->lookupSystem($firstSystem->getSystemType())->getDefaultMode());
124
            }
125
126
            // maximum of two systems get repaired
127
            if (count($damagedSystems) > 1) {
128
                $secondSystem = $damagedSystems[1];
129
                $secondSystem->setStatus(100);
130
131
                if ($ship->getCrewCount() > 0) {
132
                    $secondSystem->setMode($this->shipSystemManager->lookupSystem($secondSystem->getSystemType())->getDefaultMode());
133
                }
134
            }
135
136
            // maximum of two additional systems get repaired
137
            if ($isRepairStationBonus) {
138
                if (count($damagedSystems) > 2) {
139
                    $thirdSystem = $damagedSystems[2];
140
                    $thirdSystem->setStatus(100);
141
142
                    if ($ship->getCrewCount() > 0) {
143
                        $thirdSystem->setMode($this->shipSystemManager->lookupSystem($thirdSystem->getSystemType())->getDefaultMode());
144
                    }
145
                }
146
                if (count($damagedSystems) > 3) {
147
                    $fourthSystem = $damagedSystems[3];
148
                    $fourthSystem->setStatus(100);
149
150
                    if ($ship->getCrewCount() > 0) {
151
                        $fourthSystem->setMode($this->shipSystemManager->lookupSystem($fourthSystem->getSystemType())->getDefaultMode());
152
                    }
153
                }
154
            }
155
        }
156
    }
157
158
    private function sendPrivateMessages(ShipInterface $ship, ColonyInterface|ShipInterface $entity): void
159
    {
160
        $shipOwnerMessage = $entity instanceof ColonyInterface ? sprintf(
161
            "Die Reparatur der %s wurde in Sektor %s bei der Kolonie %s des Spielers %s fertiggestellt",
162
            $ship->getName(),
163
            $ship->getSectorString(),
164
            $entity->getName(),
165
            $entity->getUser()->getName()
166
        ) : sprintf(
167
            "Die Reparatur der %s wurde in Sektor %s von der %s %s des Spielers %s fertiggestellt",
168
            $ship->getName(),
169
            $ship->getSectorString(),
170
            $entity->getRump()->getName(),
171
            $entity->getName(),
172
            $entity->getUser()->getName()
173
        );
174
175
        $this->privateMessageSender->send(
176
            $entity->getUser()->getId(),
177
            $ship->getUser()->getId(),
178
            $shipOwnerMessage,
179
            PrivateMessageFolderTypeEnum::SPECIAL_SHIP
180
        );
181
182
        $entityOwnerMessage = $entity instanceof ColonyInterface ? sprintf(
183
            "Die Reparatur der %s von Siedler %s wurde in Sektor %s bei der Kolonie %s fertiggestellt",
184
            $ship->getName(),
185
            $ship->getUser()->getName(),
186
            $ship->getSectorString(),
187
            $entity->getName()
188
        ) : sprintf(
189
            "Die Reparatur der %s von Siedler %s wurde in Sektor %s von der %s %s fertiggestellt",
190
            $ship->getName(),
191
            $ship->getUser()->getName(),
192
            $ship->getSectorString(),
193
            $entity->getRump()->getName(),
194
            $entity->getName()
195
        );
196
197
        $this->privateMessageSender->send(
198
            UserEnum::USER_NOONE,
199
            $entity->getUser()->getId(),
200
            $entityOwnerMessage,
201
            $entity instanceof ColonyInterface ? PrivateMessageFolderTypeEnum::SPECIAL_COLONY :
202
                PrivateMessageFolderTypeEnum::SPECIAL_STATION
203
        );
204
    }
205
}
206