Passed
Push — master ( eb8830...087291 )
by Nico
49:08 queued 24:09
created

ManageCrew::increaseCrew()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10.0036

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 27
c 1
b 0
f 0
nc 9
nop 6
dl 0
loc 47
ccs 29
cts 30
cp 0.9667
crap 10.0036
rs 7.6666

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 2
                if ($actualcrew >= $mincrew && $actualcrew + $additionalCrew > $maxcrew) {
116
117 2
                    if ($ship->hasShipSystem(ShipSystemTypeEnum::SYSTEM_TROOP_QUARTERS) && ($additionalCrew > 0
118 2
                        && $ship->getShipSystem(ShipSystemTypeEnum::SYSTEM_TROOP_QUARTERS)->getMode() === ShipSystemModeEnum::MODE_OFF
119 2
                        && !$this->helper->activate($wrapper, ShipSystemTypeEnum::SYSTEM_TROOP_QUARTERS, $informations))) {
120
                        $additionalCrew  = 0;
121
                    }
122
                }
123
            }
124
125 2
            $managerProvider->addShipCrew($ship, $additionalCrew);
126 2
            $msg[] = sprintf(
127 2
                _('%s: %d Crewman wurde(n) hochgebeamt'),
128 2
                $ship->getName(),
129 2
                $additionalCrew
130 2
            );
131
132 2
            if ($ship->hasShipSystem(ShipSystemTypeEnum::SYSTEM_LIFE_SUPPORT)) {
133 2
                $this->shipSystemManager->activate($wrapper, ShipSystemTypeEnum::SYSTEM_LIFE_SUPPORT, true);
134
            }
135
        }
136
    }
137
138
    /**
139
     * @param array<string> $msg
140
     */
141 3
    private function descreaseCrew(
142
        int $newCrewCount,
143
        ShipWrapperInterface $wrapper,
144
        ManagerProviderInterface $managerProvider,
145
        array &$msg
146
    ): void {
147 3
        $ship = $wrapper->get();
148 3
        $user = $managerProvider->getUser();
149
150 3
        if ($managerProvider->getFreeCrewStorage() == 0) {
151 1
            $msg[] = sprintf(
152 1
                _('%s: Kein Platz für die Crew auf der %s'),
153 1
                $ship->getName(),
154 1
                $managerProvider->getName()
155 1
            );
156 1
            return;
157
        }
158
159 2
        $ownCrewCount = $this->troopTransferUtility->ownCrewOnTarget($user, $ship);
160
161 2
        $removedCrew = min(
162 2
            $ownCrewCount,
163 2
            $ship->getCrewCount() - $newCrewCount,
164 2
            $managerProvider->getFreeCrewStorage()
165 2
        );
166
167 2
        $this->dumpForeignCrew($ship);
168
169 2
        $managerProvider->addCrewAssignments(array_slice(
170 2
            $ship->getCrewAssignments()->toArray(),
171 2
            0,
172 2
            $removedCrew
173 2
        ));
174
175 2
        $msg[] = sprintf(
176 2
            _('%s: %d Crewman wurde(n) runtergebeamt'),
177 2
            $ship->getName(),
178 2
            $removedCrew
179 2
        );
180
181 2
        if ($removedCrew === $ownCrewCount) {
182 1
            $this->shipShutdown->shutdown($wrapper);
183
        }
184
    }
185
186 2
    private function dumpForeignCrew(ShipInterface $ship): void
187
    {
188 2
        foreach ($ship->getCrewAssignments() as $shipCrew) {
189 2
            if ($shipCrew->getCrew()->getUser() !== $ship->getUser()) {
190 1
                $this->shipLeaver->dumpCrewman(
191 1
                    $shipCrew,
192 1
                    sprintf(
193 1
                        'Die Dienste von Crewman %s werden nicht mehr auf der Station %s von Spieler %s benötigt.',
194 1
                        $shipCrew->getCrew()->getName(),
195 1
                        $ship->getName(),
196 1
                        $ship->getUser()->getName(),
197 1
                    )
198 1
                );
199
            }
200
        }
201
    }
202
}
203