Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

ColonyStorageEntityWrapper::transfer()   B

Complexity

Conditions 11
Paths 34

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 33
nc 34
nop 3
dl 0
loc 53
ccs 0
cts 35
cp 0
crap 132
rs 7.3166
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 getLocation(): LocationInterface
52
    {
53
        return $this->colony->getLocation();
54
    }
55
56
    #[Override]
57
    public function canPenetrateShields(UserInterface $user, InformationInterface $information): bool
58
    {
59
        if (
60
            $this->colony->getUser() !== $user
61
            && $this->colonyLibFactory->createColonyShieldingManager($this->colony)->isShieldingEnabled()
62
            && $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...
63
        ) {
64
            $frequency = request::postInt('frequency');
65
            if ($frequency !== $this->colony->getShieldFrequency()) {
66
                $information->addInformation("Die Schildfrequenz ist nicht korrekt");
67
                return false;
68
            }
69
        }
70
71
        return true;
72
    }
73
74
    // COMMODITIES
75 2
    public function getBeamFactor(): int
76
    {
77 2
        return $this->colony->getBeamFactor();
78
    }
79
80
    #[Override]
81
    public function transfer(
82
        bool $isUnload,
83
        StorageEntityWrapperInterface $target,
84
        InformationInterface $information
85
    ): void {
86
87
        $transferTarget = $isUnload ? $target->get() : $this->colony;
88
        if ($transferTarget->getMaxStorage() <= $transferTarget->getStorageSum()) {
89
            $information->addInformationf('%s: Der Lagerraum ist voll', $target->getName());
90
            return;
91
        }
92
93
        $commodities = request::postArray('commodities');
94
        $gcount = request::postArray('count');
95
96
        $storage = $isUnload ? $this->colony->getStorage() : $target->get()->getBeamableStorage();
97
        if ($storage->isEmpty()) {
98
            $information->addInformation("Keine Waren zum Beamen vorhanden");
99
            return;
100
        }
101
        if (count($commodities) == 0 || count($gcount) == 0) {
102
            $information->addInformation("Es wurden keine Waren zum Beamen ausgewählt");
103
            return;
104
        }
105
106
        $informations = new InformationWrapper();
107
108
        foreach ($commodities as $key => $value) {
109
            $commodityId = (int) $value;
110
111
            if (!array_key_exists($key, $gcount)) {
112
                continue;
113
            }
114
115
            $this->commodityTransfer->transferCommodity(
116
                $commodityId,
117
                $gcount[$key],
118
                $this->colony,
119
                $isUnload ? $this->colony : $target->get(),
120
                $transferTarget,
121
                $information
122
            );
123
        }
124
125
        if (!$informations->isEmpty()) {
126
            $informations->addInformationArray(
127
                [sprintf(
128
                    _('Die Kolonie %s hat folgende Waren zur %s transferiert'),
129
                    $this->colony->getName(),
130
                    $target->getName()
131
                )],
132
                true
133
            );
134
        }
135
    }
136
137
    // CREW
138
    #[Override]
139
    public function getMaxTransferrableCrew(bool $isTarget, UserInterface $user): int
140
    {
141
        return $this->troopTransferUtility->ownCrewOnTarget($user, $this->colony);
142
    }
143
144
    #[Override]
145
    public function getFreeCrewSpace(UserInterface $user): int
146
    {
147
        if ($user !== $this->colony->getUser()) {
148
            return 0;
149
        }
150
151
        return $this->colonyLibFactory
152
            ->createColonyPopulationCalculator($this->colony)
153
            ->getFreeAssignmentCount();
154
    }
155
156
    #[Override]
157
    public function checkCrewStorage(int $amount, bool $isUnload, InformationInterface $information): bool
158
    {
159
        return true;
160
    }
161
162
    #[Override]
163
    public function acceptsCrewFrom(int $amount, UserInterface $user, InformationInterface $information): bool
164
    {
165
        return $this->colony->getUser() === $user;
166
    }
167
168
    #[Override]
169
    public function postCrewTransfer(int $foreignCrewChangeAmount, StorageEntityWrapperInterface $other, InformationInterface $information): void {}
170
171
    // TORPEDOS
172
173
    #[Override]
174
    public function getTorpedo(): ?TorpedoTypeInterface
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(TorpedoTypeInterface $torpedoType, InformationInterface $information): bool
199
    {
200
        return true;
201
    }
202
203
    #[Override]
204
    public function changeTorpedo(int $changeAmount, TorpedoTypeInterface $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