Passed
Pull Request — master (#1930)
by Janko
14:58 queued 05:13
created

ManageCrew::increaseCrew()   B

Complexity

Conditions 10
Paths 7

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 10.0033

Importance

Changes 0
Metric Value
cc 10
eloc 29
nc 7
nop 6
dl 0
loc 49
ccs 30
cts 31
cp 0.9677
crap 10.0033
rs 7.6666
c 0
b 0
f 0

How to fix   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
declare(strict_types=1);
4
5
namespace Stu\Lib\ShipManagement\Manager;
6
7
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...
8
use RuntimeException;
9
use Stu\Component\Ship\Crew\ShipCrewCalculatorInterface;
10
use Stu\Component\Ship\System\ShipSystemManagerInterface;
11
use Stu\Component\Ship\System\ShipSystemTypeEnum;
12
use Stu\Component\Ship\System\ShipSystemModeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\System\ShipSystemModeEnum 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...
13
use Stu\Lib\ShipManagement\Provider\ManagerProviderInterface;
14
use Stu\Module\Ship\Lib\Auxiliary\ShipShutdownInterface;
15
use Stu\Module\Ship\Lib\Crew\ShipLeaverInterface;
16
use Stu\Module\Ship\Lib\Crew\TroopTransferUtilityInterface;
17
use Stu\Module\Ship\Lib\ActivatorDeactivatorHelperInterface;
18
use Stu\Lib\Information\InformationWrapper;
19
use Stu\Module\Ship\Lib\ShipWrapperInterface;
20
use Stu\Orm\Entity\ShipBuildplanInterface;
21
use Stu\Orm\Entity\ShipInterface;
22
23
class ManageCrew implements ManagerInterface
24
{
25 12
    public function __construct(
26
        private ShipCrewCalculatorInterface $shipCrewCalculator,
27
        private ShipSystemManagerInterface $shipSystemManager,
28
        private TroopTransferUtilityInterface $troopTransferUtility,
29
        private ShipShutdownInterface $shipShutdown,
30
        private ShipLeaverInterface $shipLeaver,
31
        private ActivatorDeactivatorHelperInterface $helper
32 12
    ) {}
33
34 12
    #[Override]
35
    public function manage(ShipWrapperInterface $wrapper, array $values, ManagerProviderInterface $managerProvider): array
36
    {
37 12
        $msg = [];
38 12
        $informations = new InformationWrapper();
39
40 12
        $newCrewCountArray = $values['crew'] ?? null;
41 12
        if ($newCrewCountArray === null) {
42 1
            throw new RuntimeException('value array not existent');
43
        }
44
45 11
        $ship = $wrapper->get();
46 11
        $user = $managerProvider->getUser();
47 11
        $buildplan = $ship->getBuildplan();
48
49
        if (
50 11
            isset($newCrewCountArray[$ship->getId()])
51 11
            && $ship->canMan()
52 11
            && $buildplan !== null
53 11
            && $ship->getUser() === $user
54
        ) {
55 7
            $newCrewCount = (int)$newCrewCountArray[$ship->getId()];
56 7
            if ($ship->getCrewCount() !== $newCrewCount) {
57 6
                $this->setNewCrew($newCrewCount, $wrapper, $buildplan, $managerProvider, $msg, $informations);
58
            }
59
        }
60
61 11
        return array_merge($msg, $informations->getInformations());
62
    }
63
64
    /**
65
     * @param array<string> $msg
66
     */
67 6
    private function setNewCrew(
68
        int $newCrewCount,
69
        ShipWrapperInterface $wrapper,
70
        ShipBuildplanInterface $buildplan,
71
        ManagerProviderInterface $managerProvider,
72
        array &$msg,
73
        InformationWrapper $informations
74
    ): void {
75 6
        $ship = $wrapper->get();
76
77 6
        if ($newCrewCount > $ship->getCrewCount()) {
78 3
            $this->increaseCrew($newCrewCount, $wrapper, $buildplan, $managerProvider, $msg, $informations);
79
        } else {
80 3
            $this->descreaseCrew($newCrewCount, $wrapper, $managerProvider, $msg);
81
        }
82
    }
83
84
    /**
85
     * @param array<string> $msg
86
     */
87 3
    private function increaseCrew(
88
        int $newCrewCount,
89
        ShipWrapperInterface $wrapper,
90
        ShipBuildplanInterface $buildplan,
91
        ManagerProviderInterface $managerProvider,
92
        array &$msg,
93
        InformationWrapper $informations
94
    ): void {
95 3
        $ship = $wrapper->get();
96
97 3
        if ($managerProvider->getFreeCrewAmount() == 0) {
98 1
            $msg[] = sprintf(
99 1
                _('%s: Keine Crew auf der %s vorhanden'),
100 1
                $ship->getName(),
101 1
                $managerProvider->getName(),
102 1
                $buildplan->getCrew()
103 1
            );
104
        } else {
105 2
            $additionalCrew = min(
106 2
                $newCrewCount - $ship->getCrewCount(),
107 2
                $managerProvider->getFreeCrewAmount()
108 2
            );
109
110 2
            if ($ship->getBuildplan() != null) {
111 2
                $mincrew = $ship->getBuildplan()->getCrew();
112 2
                $actualcrew = $ship->getCrewCount();
113 2
                $maxcrew = $this->shipCrewCalculator->getMaxCrewCountByRump($ship->getRump());
114
115
                if (
116 2
                    $actualcrew >= $mincrew
117 2
                    && $actualcrew + $additionalCrew > $maxcrew
118 2
                    && ($ship->hasShipSystem(ShipSystemTypeEnum::SYSTEM_TROOP_QUARTERS) && ($additionalCrew > 0
119 2
                        && $ship->getShipSystem(ShipSystemTypeEnum::SYSTEM_TROOP_QUARTERS)->getMode() === ShipSystemModeEnum::MODE_OFF
120 2
                        && !$this->helper->activate($wrapper, ShipSystemTypeEnum::SYSTEM_TROOP_QUARTERS, $informations)))
121
                ) {
122
123
                    $additionalCrew  = 0;
124
                }
125
            }
126
127 2
            $managerProvider->addShipCrew($ship, $additionalCrew);
128 2
            $msg[] = sprintf(
129 2
                _('%s: %d Crewman wurde(n) hochgebeamt'),
130 2
                $ship->getName(),
131 2
                $additionalCrew
132 2
            );
133
134 2
            if ($ship->hasShipSystem(ShipSystemTypeEnum::SYSTEM_LIFE_SUPPORT)) {
135 2
                $this->shipSystemManager->activate($wrapper, ShipSystemTypeEnum::SYSTEM_LIFE_SUPPORT, true);
136
            }
137
        }
138
    }
139
140
    /**
141
     * @param array<string> $msg
142
     */
143 3
    private function descreaseCrew(
144
        int $newCrewCount,
145
        ShipWrapperInterface $wrapper,
146
        ManagerProviderInterface $managerProvider,
147
        array &$msg
148
    ): void {
149 3
        $ship = $wrapper->get();
150 3
        $user = $managerProvider->getUser();
151
152 3
        if ($managerProvider->getFreeCrewStorage() == 0) {
153 1
            $msg[] = sprintf(
154 1
                _('%s: Kein Platz für die Crew auf der %s'),
155 1
                $ship->getName(),
156 1
                $managerProvider->getName()
157 1
            );
158 1
            return;
159
        }
160
161 2
        $ownCrewCount = $this->troopTransferUtility->ownCrewOnTarget($user, $ship);
162
163 2
        $removedCrew = min(
164 2
            $ownCrewCount,
165 2
            $ship->getCrewCount() - $newCrewCount,
166 2
            $managerProvider->getFreeCrewStorage()
167 2
        );
168
169 2
        $this->dumpForeignCrew($ship);
170
171 2
        $managerProvider->addCrewAssignments(array_slice(
172 2
            $ship->getCrewAssignments()->toArray(),
173 2
            0,
174 2
            $removedCrew
175 2
        ));
176
177 2
        $msg[] = sprintf(
178 2
            _('%s: %d Crewman wurde(n) runtergebeamt'),
179 2
            $ship->getName(),
180 2
            $removedCrew
181 2
        );
182
183 2
        if ($removedCrew === $ownCrewCount) {
184 1
            $this->shipShutdown->shutdown($wrapper);
185
        }
186
    }
187
188 2
    private function dumpForeignCrew(ShipInterface $ship): void
189
    {
190 2
        foreach ($ship->getCrewAssignments() as $shipCrew) {
191 2
            if ($shipCrew->getCrew()->getUser() !== $ship->getUser()) {
192 1
                $this->shipLeaver->dumpCrewman(
193 1
                    $shipCrew,
194 1
                    sprintf(
195 1
                        'Die Dienste von Crewman %s werden nicht mehr auf der Station %s von Spieler %s benötigt.',
196 1
                        $shipCrew->getCrew()->getName(),
197 1
                        $ship->getName(),
198 1
                        $ship->getUser()->getName(),
199 1
                    )
200 1
                );
201
            }
202
        }
203
    }
204
}
205