Failed Conditions
Push — dev ( 56c6e5...f96e05 )
by Janko
17:26
created

SpacecraftStorageCommodityLogic   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 2.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 122
ccs 2
cts 73
cp 0.0274
rs 10
wmc 28

3 Methods

Rating   Name   Duplication   Size   Complexity  
B transfer() 0 38 9
D transferPerSpacecraft() 0 73 18
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Transfer\Wrapper;
6
7
use request;
8
use Stu\Lib\Information\InformationInterface;
9
use Stu\Lib\Pirate\PirateReactionInterface;
10
use Stu\Lib\Pirate\PirateReactionTriggerEnum;
11
use Stu\Lib\Transfer\CommodityTransferInterface;
12
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
13
use Stu\Orm\Entity\ShipInterface;
14
15
class SpacecraftStorageCommodityLogic
16
{
17 1
    public function __construct(
18
        private PirateReactionInterface $pirateReaction,
19
        private CommodityTransferInterface $commodityTransfer
20 1
    ) {}
21
22
    public function transfer(
23
        bool $isUnload,
24
        SpacecraftWrapperInterface $wrapper,
25
        StorageEntityWrapperInterface $target,
26
        InformationInterface $information
27
    ): void {
28
29
        $hasTransfered = false;
30
31
        // check for fleet option
32
        $fleetWrapper = $wrapper->getFleetWrapper();
33
        if (request::postInt('isfleet') && $fleetWrapper !== null) {
34
            foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
35
                if ($this->transferPerSpacecraft(
36
                    $isUnload,
37
                    $wrapper,
38
                    $target,
39
                    $information
40
                )) {
41
                    $hasTransfered = true;
42
                }
43
            }
44
        } else {
45
            $hasTransfered =  $this->transferPerSpacecraft($isUnload, $wrapper, $target, $information);
46
        }
47
48
        $spacecraft = $wrapper->get();
49
        $targetEntity = $target->get();
50
        if (
51
            !$isUnload
52
            && $hasTransfered
53
            && $spacecraft instanceof ShipInterface
54
            && $targetEntity instanceof ShipInterface
55
        ) {
56
            $this->pirateReaction->checkForPirateReaction(
57
                $targetEntity,
58
                PirateReactionTriggerEnum::ON_BEAM,
59
                $spacecraft
60
            );
61
        }
62
    }
63
64
    private function transferPerSpacecraft(
65
        bool $isUnload,
66
        SpacecraftWrapperInterface $wrapper,
67
        StorageEntityWrapperInterface $target,
68
        InformationInterface $information
69
    ): bool {
70
71
        $ship = $wrapper->get();
72
        $epsSystem = $wrapper->getEpsSystemData();
73
74
        //sanity checks
75
        $isDockTransfer = $this->commodityTransfer->isDockTransfer($ship, $target->get());
76
        if (!$isDockTransfer && ($epsSystem === null || $epsSystem->getEps() === 0)) {
77
            $information->addInformation("Keine Energie vorhanden");
78
            return false;
79
        }
80
        if ($ship->isCloaked()) {
81
            $information->addInformation("Die Tarnung ist aktiviert");
82
            return false;
83
        }
84
        if ($ship->isWarped()) {
85
            $information->addInformation("Schiff befindet sich im Warp");
86
            return false;
87
        }
88
89
        $transferTarget = $isUnload ? $target->get() : $ship;
90
        if ($transferTarget->getMaxStorage() <= $transferTarget->getStorageSum()) {
91
            $information->addInformationf('%s: Der Lagerraum ist voll', $isUnload ? $target->getName() : $ship->getName());
92
            return false;
93
        }
94
95
        $commodities = request::postArray('commodities');
96
        $gcount = request::postArray('count');
97
98
        $storage = $isUnload ? $ship->getBeamableStorage() : $target->get()->getBeamableStorage();
99
100
        if ($storage->isEmpty()) {
101
            $information->addInformation("Keine Waren zum Beamen vorhanden");
102
            return false;
103
        }
104
        if (count($commodities) == 0 || count($gcount) == 0) {
105
            $information->addInformation("Es wurden keine Waren zum Beamen ausgewählt");
106
            return false;
107
        }
108
        $information->addInformationf(
109
            'Die %s hat folgende Waren %s %s %s transferiert',
110
            $ship->getName(),
111
            $isUnload ? 'zur' : 'von der',
112
            $target->get()->getTransferEntityType()->getName(),
113
            $target->getName()
114
        );
115
116
        $hasTransfered = false;
117
        foreach ($commodities as $key => $value) {
118
            $commodityId = (int) $value;
119
120
            if (!array_key_exists($key, $gcount)) {
121
                continue;
122
            }
123
124
            if ($this->commodityTransfer->transferCommodity(
125
                $commodityId,
126
                $gcount[$key],
127
                $wrapper,
128
                $isUnload ? $ship : $target->get(),
129
                $transferTarget,
130
                $information
131
            )) {
132
                $hasTransfered = true;
133
            }
134
        }
135
136
        return $hasTransfered;
137
    }
138
}
139