Passed
Push — master ( 4ea0a9...ff5a87 )
by Janko
10:24
created

ColonyStorageEntityWrapper::canTransfer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
119
        foreach ($commodities as $key => $value) {
120
            $commodityId = (int) $value;
121
122
            if (!array_key_exists($key, $gcount)) {
123
                continue;
124
            }
125
126
            $this->commodityTransfer->transferCommodity(
127
                $commodityId,
128
                $gcount[$key],
129
                $this->colony,
130
                $isUnload ? $this->colony : $target->get(),
131
                $transferTarget,
132
                $information
133
            );
134
        }
135
136
        if (!$informations->isEmpty()) {
137
            $informations->addInformationArray(
138
                [sprintf(
139
                    _('Die Kolonie %s hat folgende Waren zur %s transferiert'),
140
                    $this->colony->getName(),
141
                    $target->getName()
142
                )],
143
                true
144
            );
145
        }
146
    }
147
148
    // CREW
149
    #[Override]
150
    public function getMaxTransferrableCrew(bool $isTarget, UserInterface $user): int
151
    {
152
        return $this->troopTransferUtility->ownCrewOnTarget($user, $this->colony);
153
    }
154
155
    #[Override]
156
    public function getFreeCrewSpace(UserInterface $user): int
157
    {
158
        if ($user !== $this->colony->getUser()) {
159
            return 0;
160
        }
161
162
        return $this->colonyLibFactory
163
            ->createColonyPopulationCalculator($this->colony)
164
            ->getFreeAssignmentCount();
165
    }
166
167
    #[Override]
168
    public function checkCrewStorage(int $amount, bool $isUnload, InformationInterface $information): bool
169
    {
170
        return true;
171
    }
172
173
    #[Override]
174
    public function acceptsCrewFrom(int $amount, UserInterface $user, InformationInterface $information): bool
175
    {
176
        return $this->colony->getUser() === $user;
177
    }
178
179
    #[Override]
180
    public function postCrewTransfer(int $foreignCrewChangeAmount, StorageEntityWrapperInterface $other, InformationInterface $information): void {}
181
182
    // TORPEDOS
183
184
    #[Override]
185
    public function getTorpedo(): ?TorpedoTypeInterface
186
    {
187
        return null;
188
    }
189
190
    #[Override]
191
    public function getTorpedoCount(): int
192
    {
193
        return 0;
194
    }
195
196
    #[Override]
197
    public function getMaxTorpedos(): int
198
    {
199
        return $this->colony->getMaxStorage() - $this->colony->getStorageSum();
200
    }
201
202
    #[Override]
203
    public function canTransferTorpedos(InformationInterface $information): bool
204
    {
205
        return true;
206
    }
207
208
    #[Override]
209
    public function canStoreTorpedoType(TorpedoTypeInterface $torpedoType, InformationInterface $information): bool
210
    {
211
        return true;
212
    }
213
214
    #[Override]
215
    public function changeTorpedo(int $changeAmount, TorpedoTypeInterface $type): void
216
    {
217
        if ($changeAmount > 0) {
218
            $this->storageManager->upperStorage(
219
                $this->colony,
220
                $type->getCommodity(),
221
                $changeAmount
222
            );
223
        } else {
224
            $this->storageManager->lowerStorage(
225
                $this->colony,
226
                $type->getCommodity(),
227
                $changeAmount
228
            );
229
        }
230
    }
231
}
232