Passed
Push — dev ( c940fd...619386 )
by Janko
09:40
created

ColonyStorageEntityWrapper::transfer()   C

Complexity

Conditions 13
Paths 46

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

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

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 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\Lib\Information\InformationInterface;
10
use Stu\Lib\Information\InformationWrapper;
11
use Stu\Lib\Transfer\CommodityTransferInterface;
12
use Stu\Lib\Transfer\EntityWithStorageInterface;
13
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
14
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
15
use Stu\Module\Spacecraft\Lib\Crew\TroopTransferUtilityInterface;
16
use Stu\Orm\Entity\ColonyInterface;
17
use Stu\Orm\Entity\LocationInterface;
18
use Stu\Orm\Entity\TorpedoTypeInterface;
19
use Stu\Orm\Entity\UserInterface;
20
21
class ColonyStorageEntityWrapper implements StorageEntityWrapperInterface
22
{
23 4
    public function __construct(
24
        private ColonyLibFactoryInterface $colonyLibFactory,
25
        private CommodityTransferInterface $commodityTransfer,
26
        private StorageManagerInterface $storageManager,
27
        private TroopTransferUtilityInterface $troopTransferUtility,
28
        private ColonyInterface $colony
29 4
    ) {}
30
31
    // GENERAL
32 4
    #[Override]
33
    public function get(): EntityWithStorageInterface
34
    {
35 4
        return $this->colony;
36
    }
37
38 4
    #[Override]
39
    public function getUser(): UserInterface
40
    {
41 4
        return $this->colony->getUser();
42
    }
43
44 2
    #[Override]
45
    public function getName(): string
46
    {
47 2
        return sprintf('Kolonie %s', $this->colony->getName());
48
    }
49
50
    #[Override]
51
    public function canTransfer(InformationInterface $information): bool
52
    {
53
        if ($this->colony->getWorkers() + $this->colony->getWorkless() === 0) {
54
            $information->addInformation("Es lebt niemand auf dieser Kolonie");
55
            return false;
56
        }
57
58
        return true;
59
    }
60
61
    #[Override]
62
    public function getLocation(): LocationInterface
63
    {
64
        return $this->colony->getLocation();
65
    }
66
67
    #[Override]
68
    public function canPenetrateShields(UserInterface $user, InformationInterface $information): bool
69
    {
70
        if (
71
            $this->colony->getUser() !== $user
72
            && $this->colonyLibFactory->createColonyShieldingManager($this->colony)->isShieldingEnabled()
73
            && $this->colony->getShieldFrequency()
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->colony->getShieldFrequency() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
74
        ) {
75
            $frequency = request::postInt('frequency');
76
            if ($frequency !== $this->colony->getShieldFrequency()) {
77
                $information->addInformation("Die Schildfrequenz ist nicht korrekt");
78
                return false;
79
            }
80
        }
81
82
        return true;
83
    }
84
85
    // COMMODITIES
86 2
    public function getBeamFactor(): int
87
    {
88 2
        return $this->colony->getBeamFactor();
89
    }
90
91
    #[Override]
92
    public function transfer(
93
        bool $isUnload,
94
        StorageEntityWrapperInterface $target,
95
        InformationInterface $information
96
    ): void {
97
98
        $transferTarget = $isUnload ? $target->get() : $this->colony;
99
        if ($transferTarget->getMaxStorage() <= $transferTarget->getStorageSum()) {
100
            $information->addInformationf('%s: Der Lagerraum ist voll', $target->getName());
101
            return;
102
        }
103
104
        $commodities = request::postArray('commodities');
105
        $gcount = request::postArray('count');
106
107
        $storage = $isUnload ? $this->colony->getStorage() : $target->get()->getBeamableStorage();
108
        if ($storage->isEmpty()) {
109
            $information->addInformation("Keine Waren zum Beamen vorhanden");
110
            return;
111
        }
112
        if (count($commodities) == 0 || count($gcount) == 0) {
113
            $information->addInformation("Es wurden keine Waren zum Beamen ausgewählt");
114
            return;
115
        }
116
117
        $informations = new InformationWrapper();
118
        $informations->addInformationf(
119
            'Die Kolonie %s hat folgende Waren %s %s gebeamt',
120
            $this->colony->getName(),
121
            $isUnload ? 'zur' : 'von der',
122
            $target->getName()
123
        );
124
125
        foreach ($commodities as $key => $value) {
126
            $commodityId = (int) $value;
127
128
            if (!array_key_exists($key, $gcount)) {
129
                continue;
130
            }
131
132
            $this->commodityTransfer->transferCommodity(
133
                $commodityId,
134
                $gcount[$key],
135
                $this->colony,
136
                $isUnload ? $this->colony : $target->get(),
137
                $transferTarget,
138
                $informations
139
            );
140
        }
141
142
        $informationArray = $informations->getInformations();
143
        if (count($informationArray) > 1) {
144
145
            foreach ($informationArray as $info) {
146
                $information->addInformation($info);
147
            }
148
        }
149
    }
150
151
    // CREW
152
    #[Override]
153
    public function getMaxTransferrableCrew(bool $isTarget, UserInterface $user): int
154
    {
155
        return $this->troopTransferUtility->ownCrewOnTarget($user, $this->colony);
156
    }
157
158
    #[Override]
159
    public function getFreeCrewSpace(UserInterface $user): int
160
    {
161
        if ($user !== $this->colony->getUser()) {
162
            return 0;
163
        }
164
165
        return $this->colonyLibFactory
166
            ->createColonyPopulationCalculator($this->colony)
167
            ->getFreeAssignmentCount();
168
    }
169
170
    #[Override]
171
    public function checkCrewStorage(int $amount, bool $isUnload, InformationInterface $information): bool
172
    {
173
        return true;
174
    }
175
176
    #[Override]
177
    public function acceptsCrewFrom(int $amount, UserInterface $user, InformationInterface $information): bool
178
    {
179
        return $this->colony->getUser() === $user;
180
    }
181
182
    #[Override]
183
    public function postCrewTransfer(int $foreignCrewChangeAmount, StorageEntityWrapperInterface $other, InformationInterface $information): void {}
184
185
    // TORPEDOS
186
187
    #[Override]
188
    public function getTorpedo(): ?TorpedoTypeInterface
189
    {
190
        return null;
191
    }
192
193
    #[Override]
194
    public function getTorpedoCount(): int
195
    {
196
        return 0;
197
    }
198
199
    #[Override]
200
    public function getMaxTorpedos(): int
201
    {
202
        return $this->colony->getMaxStorage() - $this->colony->getStorageSum();
203
    }
204
205
    #[Override]
206
    public function canTransferTorpedos(InformationInterface $information): bool
207
    {
208
        return true;
209
    }
210
211
    #[Override]
212
    public function canStoreTorpedoType(TorpedoTypeInterface $torpedoType, InformationInterface $information): bool
213
    {
214
        return true;
215
    }
216
217
    #[Override]
218
    public function changeTorpedo(int $changeAmount, TorpedoTypeInterface $type): void
219
    {
220
        if ($changeAmount > 0) {
221
            $this->storageManager->upperStorage(
222
                $this->colony,
223
                $type->getCommodity(),
224
                $changeAmount
225
            );
226
        } else {
227
            $this->storageManager->lowerStorage(
228
                $this->colony,
229
                $type->getCommodity(),
230
                $changeAmount
231
            );
232
        }
233
    }
234
}
235