Passed
Push — dev ( d7eff6...c22c5c )
by Janko
09:28
created

SpacecraftStorageEntityWrapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 11
dl 0
loc 14
ccs 2
cts 2
cp 1
crap 1
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\Lib\Transfer\Wrapper;
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 request;
9
use Stu\Component\Spacecraft\Crew\SpacecraftCrewCalculatorInterface;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
11
use Stu\Component\Spacecraft\System\SpacecraftSystemModeEnum;
12
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
13
use Stu\Component\Spacecraft\System\Type\UplinkShipSystem;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft...m\Type\UplinkShipSystem 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\Component\Station\Dock\DockPrivilegeUtilityInterface;
15
use Stu\Lib\Information\InformationInterface;
16
use Stu\Lib\Pirate\PirateReactionInterface;
17
use Stu\Lib\Pirate\PirateReactionTriggerEnum;
18
use Stu\Lib\Transfer\CommodityTransferInterface;
19
use Stu\Lib\Transfer\EntityWithStorageInterface;
20
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
21
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
22
use Stu\Module\Spacecraft\Lib\ActivatorDeactivatorHelperInterface;
23
use Stu\Module\Spacecraft\Lib\Auxiliary\ShipShutdownInterface;
24
use Stu\Module\Spacecraft\Lib\Crew\TroopTransferUtilityInterface;
25
use Stu\Module\Spacecraft\Lib\Torpedo\ShipTorpedoManagerInterface;
26
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
27
use Stu\Orm\Entity\LocationInterface;
28
use Stu\Orm\Entity\ShipInterface;
29
use Stu\Orm\Entity\SpacecraftInterface;
30
use Stu\Orm\Entity\TorpedoTypeInterface;
31
use Stu\Orm\Entity\UserInterface;
32
33
class SpacecraftStorageEntityWrapper implements StorageEntityWrapperInterface
34
{
35
    private SpacecraftInterface $spacecraft;
36
37 13
    public function __construct(
38
        private ShipTorpedoManagerInterface $shipTorpedoManager,
39
        private PirateReactionInterface $pirateReaction,
40
        private CommodityTransferInterface $commodityTransfer,
41
        private TroopTransferUtilityInterface $troopTransferUtility,
42
        private DockPrivilegeUtilityInterface $dockPrivilegeUtility,
43
        private ActivatorDeactivatorHelperInterface $activatorDeactivatorHelper,
44
        private SpacecraftSystemManagerInterface $spacecraftSystemManager,
45
        private SpacecraftCrewCalculatorInterface $shipCrewCalculator,
46
        private ShipShutdownInterface $shipShutdown,
47
        private PrivateMessageSenderInterface $privateMessageSender,
48
        private SpacecraftWrapperInterface $spacecraftWrapper
49
    ) {
50 13
        $this->spacecraft = $spacecraftWrapper->get();
51
    }
52
53
    // GENERAL
54 13
    #[Override]
55
    public function get(): EntityWithStorageInterface
56
    {
57 13
        return $this->spacecraft;
58
    }
59
60 13
    #[Override]
61
    public function getUser(): UserInterface
62
    {
63 13
        return $this->spacecraft->getUser();
64
    }
65
66 10
    #[Override]
67
    public function getName(): string
68
    {
69 10
        return $this->spacecraft->getName();
70
    }
71
72
    #[Override]
73
    public function canTransfer(InformationInterface $information): bool
74
    {
75
        if (!$this->spacecraft->hasEnoughCrew()) {
76
            $information->addInformation("Ungenügend Crew vorhanden");
77
            return false;
78
        }
79
80
        return true;
81
    }
82
83
    #[Override]
84
    public function getLocation(): LocationInterface
85
    {
86
        return $this->spacecraft->getLocation();
87
    }
88
89
    #[Override]
90
    public function canPenetrateShields(UserInterface $user, InformationInterface $information): bool
91
    {
92
        return true;
93
    }
94
95
    // COMMODITIES
96 11
    #[Override]
97
    public function getBeamFactor(): int
98
    {
99 11
        return $this->spacecraft->getBeamFactor();
100
    }
101
102
    #[Override]
103
    public function transfer(
104
        bool $isUnload,
105
        StorageEntityWrapperInterface $target,
106
        InformationInterface $information
107
    ): void {
108
109
        $hasTransfered = false;
110
111
        // check for fleet option
112
        $fleetWrapper = $this->spacecraftWrapper->getFleetWrapper();
113
        if (request::postInt('isfleet') && $fleetWrapper !== null) {
114
            foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
115
                if ($this->transferPerSpacecraft(
116
                    $isUnload,
117
                    $wrapper,
118
                    $target,
119
                    $information
120
                )) {
121
                    $hasTransfered = true;
122
                }
123
            }
124
        } else {
125
            $hasTransfered =  $this->transferPerSpacecraft($isUnload, $this->spacecraftWrapper, $target, $information);
126
        }
127
128
        $targetEntity = $target->get();
129
        if (
130
            !$isUnload
131
            && $hasTransfered
132
            && $this->spacecraft instanceof ShipInterface
133
            && $targetEntity instanceof ShipInterface
134
        ) {
135
            $this->pirateReaction->checkForPirateReaction(
136
                $targetEntity,
137
                PirateReactionTriggerEnum::ON_BEAM,
138
                $this->spacecraft
139
            );
140
        }
141
    }
142
143
    private function transferPerSpacecraft(
144
        bool $isUnload,
145
        SpacecraftWrapperInterface $wrapper,
146
        StorageEntityWrapperInterface $target,
147
        InformationInterface $information
148
    ): bool {
149
150
        $ship = $wrapper->get();
151
        $epsSystem = $wrapper->getEpsSystemData();
152
153
        //sanity checks
154
        $isDockTransfer = $this->commodityTransfer->isDockTransfer($ship, $target->get());
155
        if (!$isDockTransfer && ($epsSystem === null || $epsSystem->getEps() === 0)) {
156
            $information->addInformation("Keine Energie vorhanden");
157
            return false;
158
        }
159
        if ($ship->getCloakState()) {
160
            $information->addInformation("Die Tarnung ist aktiviert");
161
            return false;
162
        }
163
        if ($ship->isWarped()) {
164
            $information->addInformation("Schiff befindet sich im Warp");
165
            return false;
166
        }
167
168
        $transferTarget = $isUnload ? $target->get() : $ship;
169
        if ($transferTarget->getMaxStorage() <= $transferTarget->getStorageSum()) {
170
            $information->addInformationf('%s: Der Lagerraum ist voll', $isUnload ? $target->getName() : $ship->getName());
171
            return false;
172
        }
173
174
        $commodities = request::postArray('commodities');
175
        $gcount = request::postArray('count');
176
177
        $storage = $isUnload ? $ship->getBeamableStorage() : $target->get()->getBeamableStorage();
178
179
        if ($storage->isEmpty()) {
180
            $information->addInformation("Keine Waren zum Beamen vorhanden");
181
            return false;
182
        }
183
        if (count($commodities) == 0 || count($gcount) == 0) {
184
            $information->addInformation("Es wurden keine Waren zum Beamen ausgewählt");
185
            return false;
186
        }
187
        $information->addInformationf(
188
            'Die %s hat folgende Waren %s %s %s transferiert',
189
            $ship->getName(),
190
            $isUnload ? 'zur' : 'von der',
191
            $target->get()->getTransferEntityType()->getName(),
192
            $target->getName()
193
        );
194
195
        $hasTransfered = false;
196
        foreach ($commodities as $key => $value) {
197
            $commodityId = (int) $value;
198
199
            if (!array_key_exists($key, $gcount)) {
200
                continue;
201
            }
202
203
            if ($this->commodityTransfer->transferCommodity(
204
                $commodityId,
205
                $gcount[$key],
206
                $wrapper,
207
                $isUnload ? $ship : $target->get(),
208
                $transferTarget,
209
                $information
210
            )) {
211
                $hasTransfered = true;
212
            }
213
        }
214
215
        return $hasTransfered;
216
    }
217
218
    // CREW
219 4
    #[Override]
220
    public function getMaxTransferrableCrew(bool $isTarget, UserInterface $user): int
221
    {
222 4
        return min(
223 4
            $this->troopTransferUtility->ownCrewOnTarget($user, $this->spacecraft),
224 4
            $isTarget ? PHP_INT_MAX : $this->troopTransferUtility->getBeamableTroopCount($this->spacecraft)
225 4
        );
226
    }
227
228 4
    #[Override]
229
    public function getFreeCrewSpace(UserInterface $user): int
230
    {
231 4
        if ($user !== $this->spacecraft->getUser()) {
232
            if (!$this->spacecraft->hasUplink()) {
233
                return 0;
234
            }
235
236
            $userCrewOnTarget = $this->troopTransferUtility->ownCrewOnTarget($user, $this->spacecraft);
237
            return $userCrewOnTarget === 0 ? 1 : 0;
238
        }
239
240 4
        return $this->troopTransferUtility->getFreeQuarters($this->spacecraft);
241
    }
242
243
    #[Override]
244
    public function checkCrewStorage(int $amount, bool $isUnload, InformationInterface $information): bool
245
    {
246
        if (!$this->spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)) {
247
            return true;
248
        }
249
250
        $maxRumpCrew = $this->shipCrewCalculator->getMaxCrewCountByRump($this->spacecraft->getRump());
251
        $newCrewAmount = $this->spacecraft->getCrewCount() + ($isUnload ? -$amount : $amount);
252
        if ($newCrewAmount <= $maxRumpCrew) {
253
            return true;
254
        }
255
256
        if (!$this->spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::TROOP_QUARTERS)) {
257
            $information->addInformation("Die Truppenquartiere sind zerstört");
258
            return false;
259
        }
260
261
        if ($this->spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)->getMode()->isActivated()) {
262
            return true;
263
        }
264
265
        if (!$this->activatorDeactivatorHelper->activate(
266
            $this->spacecraftWrapper,
267
            SpacecraftSystemTypeEnum::TROOP_QUARTERS,
268
            $information
269
        )) {
270
            $information->addInformation("Die Truppenquartiere konnten nicht aktiviert werden");
271
            return false;
272
        }
273
274
        return true;
275
    }
276
277
    #[Override]
278
    public function acceptsCrewFrom(int $amount, UserInterface $user, InformationInterface $information): bool
279
    {
280
        if (!$this->spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::LIFE_SUPPORT)) {
281
            $information->addInformationf('Die %s hat keine Lebenserhaltungssysteme', $this->spacecraft->getName());
282
283
            return false;
284
        }
285
286
        $needsTroopQuarters = $this->spacecraft->getCrewCount() + $amount > $this->shipCrewCalculator->getMaxCrewCountByRump($this->spacecraft->getRump());
287
        if (
288
            $needsTroopQuarters
289
            && $this->spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)
290
            && $this->spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)->getMode() === SpacecraftSystemModeEnum::MODE_OFF
291
            && !$this->activatorDeactivatorHelper->activate($this->spacecraftWrapper, SpacecraftSystemTypeEnum::TROOP_QUARTERS, $information)
292
        ) {
293
            return false;
294
        }
295
296
        if ($this->spacecraft->getUser() === $user) {
297
            return true;
298
        }
299
        if (!$this->spacecraft->hasUplink()) {
300
            return false;
301
        }
302
303
        if (!$this->dockPrivilegeUtility->checkPrivilegeFor($this->spacecraft->getId(), $user)) {
304
            $information->addInformation("Benötigte Andockerlaubnis wurde verweigert");
305
            return false;
306
        }
307
        if (!$this->spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::UPLINK)) {
308
            $information->addInformation("Das Ziel verfügt über keinen intakten Uplink");
309
            return false;
310
        }
311
312
        if ($this->troopTransferUtility->foreignerCount($this->spacecraft) >= UplinkShipSystem::MAX_FOREIGNERS) {
313
            $information->addInformation("Maximale Anzahl an fremden Crewman ist bereits erreicht");
314
            return false;
315
        }
316
317
        return true;
318
    }
319
320
    #[Override]
321
    public function postCrewTransfer(int $foreignCrewChangeAmount, StorageEntityWrapperInterface $other, InformationInterface $information): void
322
    {
323
        // no crew left, so shut down
324
        if ($this->spacecraft->getCrewCount() === 0) {
325
            $this->shipShutdown->shutdown($this->spacecraftWrapper);
326
            return;
327
        }
328
329
        if ($foreignCrewChangeAmount !== 0) {
330
331
            $isOn = $this->troopTransferUtility->foreignerCount($this->spacecraft) > 0;
332
            if (
333
                !$isOn
334
                && $this->spacecraft->getSystemState(SpacecraftSystemTypeEnum::UPLINK)
335
            ) {
336
                $this->spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::UPLINK)->setMode(SpacecraftSystemModeEnum::MODE_OFF);
337
            }
338
339
            $this->sendUplinkMessage($foreignCrewChangeAmount, $isOn, $other);
340
        }
341
342
        if (
343
            $this->spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::TROOP_QUARTERS)
344
            && $this->spacecraft->getSystemState(SpacecraftSystemTypeEnum::TROOP_QUARTERS)
345
            && $this->spacecraft->getBuildplan() !== null
346
            && $this->spacecraft->getCrewCount() <= $this->shipCrewCalculator->getMaxCrewCountByRump($this->spacecraft->getRump())
347
        ) {
348
            $this->activatorDeactivatorHelper->deactivate($this->spacecraftWrapper, SpacecraftSystemTypeEnum::TROOP_QUARTERS, $information);
349
        }
350
351
        if (!$this->spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::LIFE_SUPPORT)) {
352
            return;
353
        }
354
355
        if ($this->spacecraft->getCrewCount() === 0) {
356
            $this->spacecraftSystemManager->deactivate($this->spacecraftWrapper, SpacecraftSystemTypeEnum::LIFE_SUPPORT, true);
357
            return;
358
        }
359
360
        if (
361
            $this->spacecraft->getCrewCount() > 0
362
            && !$this->spacecraft->getSystemState(SpacecraftSystemTypeEnum::LIFE_SUPPORT)
363
        ) {
364
            $this->spacecraftSystemManager->activate($this->spacecraftWrapper, SpacecraftSystemTypeEnum::LIFE_SUPPORT, true);
365
        }
366
    }
367
368
    private function sendUplinkMessage(int $foreignCrewChangeAmount, bool $isOn, StorageEntityWrapperInterface $other): void
369
    {
370
        $msg = sprintf(
371
            _('Die %s von Spieler %s hat 1 Crewman %s deiner Station %s gebeamt. Der Uplink ist %s'),
372
            $other->getName(),
373
            $other->getUser()->getName(),
374
            $foreignCrewChangeAmount > 0 ? 'zu' : 'von',
375
            $this->spacecraft->getName(),
376
            $isOn ? 'aktiviert' : 'deaktiviert'
377
        );
378
379
        $this->privateMessageSender->send(
380
            $other->getUser()->getId(),
381
            $this->spacecraft->getUser()->getId(),
382
            $msg,
383
            PrivateMessageFolderTypeEnum::SPECIAL_STATION,
384
            $this->spacecraft->getHref()
385
        );
386
    }
387
388
    // TORPEDOS
389
390
    #[Override]
391
    public function getTorpedo(): ?TorpedoTypeInterface
392
    {
393
        return $this->spacecraft->getTorpedo();
394
    }
395
396
    #[Override]
397
    public function getTorpedoCount(): int
398
    {
399
        return $this->spacecraft->getTorpedoCount();
400
    }
401
402
    #[Override]
403
    public function getMaxTorpedos(): int
404
    {
405
        return $this->spacecraft->getMaxTorpedos();
406
    }
407
408
    #[Override]
409
    public function canTransferTorpedos(InformationInterface $information): bool
410
    {
411
        if (!$this->spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::TORPEDO_STORAGE)) {
412
            $information->addInformation("Das Torpedolager ist zerstört");
413
            return false;
414
        }
415
416
        return true;
417
    }
418
419
    #[Override]
420
    public function canStoreTorpedoType(TorpedoTypeInterface $torpedoType, InformationInterface $information): bool
421
    {
422
        if (
423
            !$this->spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::TORPEDO_STORAGE)
424
            && $this->spacecraft->getRump()->getTorpedoLevel() !== $torpedoType->getLevel()
425
        ) {
426
            $information->addInformationf('Die %s kann den Torpedotyp nicht ausrüsten', $this->spacecraft->getName());
427
            return false;
428
        }
429
430
        if (
431
            !$this->spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::TORPEDO_STORAGE)
432
            && $torpedoType->getLevel() > $this->spacecraft->getRump()->getTorpedoLevel()
433
        ) {
434
            $information->addInformationf("Die %s kann den Torpedotyp nicht ausrüsten", $this->spacecraft->getName());
435
            return false;
436
        }
437
438
        if (
439
            $this->spacecraft->getTorpedo() !== null
440
            && $this->spacecraft->getTorpedo() !== $torpedoType
441
        ) {
442
            $information->addInformation("Es ist bereits ein anderer Torpedotyp geladen");
443
            return false;
444
        }
445
446
        return true;
447
    }
448
449
    #[Override]
450
    public function changeTorpedo(int $changeAmount, TorpedoTypeInterface $type): void
451
    {
452
        $this->shipTorpedoManager->changeTorpedo(
453
            $this->spacecraftWrapper,
454
            $changeAmount,
455
            $type
456
        );
457
    }
458
}
459