Passed
Pull Request — master (#1866)
by Nico
52:42 queued 25:56
created

ManageCrew::increaseCrew()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7.0031

Importance

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