Passed
Push — dev ( 996fbb...25004f )
by Janko
16:40
created

ColonyStorageEntityWrapper   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Test Coverage

Coverage 11.24%

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 192
ccs 10
cts 89
cp 0.1124
rs 9.68
c 0
b 0
f 0
wmc 34

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getUser() 0 4 1
A getName() 0 4 1
A get() 0 4 1
A getLocation() 0 4 1
A canTransfer() 0 9 2
A getMaxTransferrableCrew() 0 4 1
A changeTorpedo() 0 14 2
A postCrewTransfer() 0 2 1
A getBeamFactor() 0 3 1
A canStoreTorpedoType() 0 4 1
A getTorpedoCount() 0 4 1
A getTorpedo() 0 4 1
A acceptsCrewFrom() 0 4 1
A canTransferTorpedos() 0 4 1
A getMaxTorpedos() 0 4 1
A checkCrewStorage() 0 4 1
C transfer() 0 56 13
A getFreeCrewSpace() 0 10 2
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
    // 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 || count($gcount) == 0) {
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, UserInterface $user): int
136
    {
137
        return $this->troopTransferUtility->ownCrewOnTarget($user, $this->colony);
138
    }
139
140
    #[Override]
141
    public function getFreeCrewSpace(UserInterface $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, UserInterface $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
    // TORPEDOS
168
169
    #[Override]
170
    public function getTorpedo(): ?TorpedoTypeInterface
171
    {
172
        return null;
173
    }
174
175
    #[Override]
176
    public function getTorpedoCount(): int
177
    {
178
        return 0;
179
    }
180
181
    #[Override]
182
    public function getMaxTorpedos(): int
183
    {
184
        return $this->colony->getMaxStorage() - $this->colony->getStorageSum();
185
    }
186
187
    #[Override]
188
    public function canTransferTorpedos(InformationInterface $information): bool
189
    {
190
        return true;
191
    }
192
193
    #[Override]
194
    public function canStoreTorpedoType(TorpedoTypeInterface $torpedoType, InformationInterface $information): bool
195
    {
196
        return true;
197
    }
198
199
    #[Override]
200
    public function changeTorpedo(int $changeAmount, TorpedoTypeInterface $type): void
201
    {
202
        if ($changeAmount > 0) {
203
            $this->storageManager->upperStorage(
204
                $this->colony,
205
                $type->getCommodity(),
206
                $changeAmount
207
            );
208
        } else {
209
            $this->storageManager->lowerStorage(
210
                $this->colony,
211
                $type->getCommodity(),
212
                $changeAmount
213
            );
214
        }
215
    }
216
}
217