SpacecraftStorageCommodityLogic   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 2.6%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 72
c 1
b 0
f 0
dl 0
loc 140
ccs 2
cts 77
cp 0.026
rs 10
wmc 29

4 Methods

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