ColonyStorageEntityWrapper::transfer()   C
last analyzed

Complexity

Conditions 13
Paths 46

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 35
c 0
b 0
f 0
nc 46
nop 3
dl 0
loc 54
ccs 0
cts 37
cp 0
crap 182
rs 6.6166

How to fix   Long Method    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\Transfer\Wrapper;
6
7
use request;
8
use Stu\Lib\Information\InformationInterface;
9
use Stu\Lib\Information\InformationWrapper;
10
use Stu\Lib\Transfer\CommodityTransferInterface;
11
use Stu\Lib\Transfer\EntityWithStorageInterface;
12
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
13
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
14
use Stu\Module\Spacecraft\Lib\Crew\TroopTransferUtilityInterface;
15
use Stu\Orm\Entity\Colony;
16
use Stu\Orm\Entity\Location;
17
use Stu\Orm\Entity\TorpedoType;
18
use Stu\Orm\Entity\User;
19
20
class ColonyStorageEntityWrapper implements StorageEntityWrapperInterface
21
{
22 4
    public function __construct(
23
        private ColonyLibFactoryInterface $colonyLibFactory,
24
        private CommodityTransferInterface $commodityTransfer,
25
        private StorageManagerInterface $storageManager,
26
        private TroopTransferUtilityInterface $troopTransferUtility,
27
        private Colony $colony
28 4
    ) {}
29
30
    // GENERAL
31 4
    #[\Override]
32
    public function get(): EntityWithStorageInterface
33
    {
34 4
        return $this->colony;
35
    }
36
37 4
    #[\Override]
38
    public function getUser(): User
39
    {
40 4
        return $this->colony->getUser();
41
    }
42
43 2
    #[\Override]
44
    public function getName(): string
45
    {
46 2
        return sprintf('Kolonie %s', $this->colony->getName());
47
    }
48
49
    #[\Override]
50
    public function canTransfer(InformationInterface $information): bool
51
    {
52
        if (($this->colony->getWorkers() + $this->colony->getChangeable()->getWorkless()) === 0) {
53
            $information->addInformation('Es lebt niemand auf dieser Kolonie');
54
            return false;
55
        }
56
57
        return true;
58
    }
59
60
    #[\Override]
61
    public function getLocation(): Location
62
    {
63
        return $this->colony->getLocation();
64
    }
65
66
    // COMMODITIES
67 2
    #[\Override]
68
    public function getBeamFactor(): int
69
    {
70 2
        return $this->colony->getBeamFactor();
71
    }
72
73
    #[\Override]
74
    public function transfer(
75
        bool $isUnload,
76
        StorageEntityWrapperInterface $target,
77
        InformationInterface $information
78
    ): void {
79
        $transferTarget = $isUnload ? $target->get() : $this->colony;
80
        if ($transferTarget->getMaxStorage() <= $transferTarget->getStorageSum()) {
81
            $information->addInformationf('%s: Der Lagerraum ist voll', $target->getName());
82
            return;
83
        }
84
85
        $commodities = request::postArray('commodities');
86
        $gcount = request::postArray('count');
87
88
        $storage = $isUnload ? $this->colony->getStorage() : $target->get()->getBeamableStorage();
89
        if ($storage->isEmpty()) {
90
            $information->addInformation('Keine Waren zum Beamen vorhanden');
91
            return;
92
        }
93
        if (count($commodities) == 0 || $gcount === []) {
94
            $information->addInformation('Es wurden keine Waren zum Beamen ausgewählt');
95
            return;
96
        }
97
98
        $informations = new InformationWrapper();
99
        $informations->addInformationf(
100
            'Die Kolonie %s hat folgende Waren %s %s gebeamt',
101
            $this->colony->getName(),
102
            $isUnload ? 'zur' : 'von der',
103
            $target->getName()
104
        );
105
106
        foreach ($commodities as $key => $value) {
107
            $commodityId = (int) $value;
108
109
            if (!array_key_exists($key, $gcount)) {
110
                continue;
111
            }
112
113
            $this->commodityTransfer->transferCommodity(
114
                $commodityId,
115
                $gcount[$key],
116
                $this->colony,
117
                $isUnload ? $this->colony : $target->get(),
118
                $transferTarget,
119
                $informations
120
            );
121
        }
122
123
        $informationArray = $informations->getInformations();
124
        if (count($informationArray) > 1) {
125
            foreach ($informationArray as $info) {
126
                $information->addInformation($info);
127
            }
128
        }
129
    }
130
131
    // CREW
132
    #[\Override]
133
    public function getMaxTransferrableCrew(bool $isTarget, User $user): int
134
    {
135
        return $this->troopTransferUtility->ownCrewOnTarget($user, $this->colony);
136
    }
137
138
    #[\Override]
139
    public function getFreeCrewSpace(User $user): int
140
    {
141
        if ($user->getId() !== $this->colony->getUser()->getId()) {
142
            return 0;
143
        }
144
145
        return $this->colonyLibFactory
146
            ->createColonyPopulationCalculator($this->colony)
147
            ->getFreeAssignmentCount();
148
    }
149
150
    #[\Override]
151
    public function checkCrewStorage(int $amount, bool $isUnload, InformationInterface $information): bool
152
    {
153
        return true;
154
    }
155
156
    #[\Override]
157
    public function acceptsCrewFrom(int $amount, User $user, InformationInterface $information): bool
158
    {
159
        return $this->colony->getUser()->getId() === $user->getId();
160
    }
161
162
    #[\Override]
163
    public function postCrewTransfer(
164
        int $foreignCrewChangeAmount,
165
        StorageEntityWrapperInterface $other,
166
        InformationInterface $information
167
    ): void {
168
        // nothing to do here
169
    }
170
171
    // TORPEDOS
172
173
    #[\Override]
174
    public function getTorpedo(): ?TorpedoType
175
    {
176
        return null;
177
    }
178
179
    #[\Override]
180
    public function getTorpedoCount(): int
181
    {
182
        return 0;
183
    }
184
185
    #[\Override]
186
    public function getMaxTorpedos(): int
187
    {
188
        return $this->colony->getMaxStorage() - $this->colony->getStorageSum();
189
    }
190
191
    #[\Override]
192
    public function canTransferTorpedos(InformationInterface $information): bool
193
    {
194
        return true;
195
    }
196
197
    #[\Override]
198
    public function canStoreTorpedoType(TorpedoType $torpedoType, InformationInterface $information): bool
199
    {
200
        return true;
201
    }
202
203
    #[\Override]
204
    public function changeTorpedo(int $changeAmount, TorpedoType $type): void
205
    {
206
        if ($changeAmount > 0) {
207
            $this->storageManager->upperStorage(
208
                $this->colony,
209
                $type->getCommodity(),
210
                $changeAmount
211
            );
212
        } else {
213
            $this->storageManager->lowerStorage(
214
                $this->colony,
215
                $type->getCommodity(),
216
                $changeAmount
217
            );
218
        }
219
    }
220
}
221