ColonyStorageEntityWrapper::transfer()   C
last analyzed

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