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

ManageTorpedo   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 99.04%

Importance

Changes 0
Metric Value
eloc 95
dl 0
loc 190
ccs 103
cts 104
cp 0.9904
rs 10
c 0
b 0
f 0
wmc 24

7 Methods

Rating   Name   Duplication   Size   Complexity  
A determineCount() 0 12 3
A loadTorpedo() 0 49 5
B manage() 0 33 6
A unloadTorpedo() 0 23 3
A __construct() 0 4 1
A sendMessage() 0 19 1
A determineTorpedoType() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\SpacecraftManagement\Manager;
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 RuntimeException;
9
use Stu\Lib\SpacecraftManagement\Provider\ManagerProviderInterface;
10
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
11
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
12
use Stu\Module\Spacecraft\Lib\Torpedo\ShipTorpedoManagerInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Orm\Entity\SpacecraftInterface;
15
use Stu\Orm\Entity\TorpedoTypeInterface;
16
17
class ManageTorpedo implements ManagerInterface
18
{
19 14
    public function __construct(
20
        private readonly ShipTorpedoManagerInterface $shipTorpedoManager,
21
        private readonly PrivateMessageSenderInterface $privateMessageSender
22 14
    ) {}
23
24 13
    #[Override]
25
    public function manage(SpacecraftWrapperInterface $wrapper, array $values, ManagerProviderInterface $managerProvider): array
26
    {
27 13
        $torp = $values['torp'] ?? null;
28 13
        if ($torp === null) {
29 1
            throw new RuntimeException('value array not existent');
30
        }
31
32 12
        $ship = $wrapper->get();
33
34 12
        if (!array_key_exists($ship->getId(), $torp)) {
35 1
            return [];
36
        }
37
38 11
        $count = $this->determineCount($torp[$ship->getId()], $ship);
39
40 11
        if ($count < 0) {
41 1
            return [];
42
        }
43 10
        if ($count === $ship->getTorpedoCount()) {
44 1
            return [];
45
        }
46
47 9
        $load = $count - $ship->getTorpedoCount();
48 9
        $isUnload = $load < 0;
49
50 9
        if ($isUnload) {
51 3
            return $this->unloadTorpedo(abs($load), $wrapper, $managerProvider);
0 ignored issues
show
Bug introduced by
It seems like abs($load) can also be of type double; however, parameter $unload of Stu\Lib\SpacecraftManage...orpedo::unloadTorpedo() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            return $this->unloadTorpedo(/** @scrutinizer ignore-type */ abs($load), $wrapper, $managerProvider);
Loading history...
52
        } else {
53 6
            $selectedTorpedoTypeArray = $values['torp_type'] ?? null;
54 6
            $torpedoType = $this->determineTorpedoType($wrapper, $selectedTorpedoTypeArray);
55
56 6
            return $this->loadTorpedo($load, $torpedoType, $wrapper, $managerProvider);
57
        }
58
    }
59
60 11
    private function determineCount(mixed $value, SpacecraftInterface $spacecraft): int
61
    {
62 11
        if ($value == 'm') {
63 1
            return $spacecraft->getMaxTorpedos();
64
        } else {
65 10
            $count = (int) $value;
66
67 10
            if ($count > $spacecraft->getMaxTorpedos()) {
68
                $count = $spacecraft->getMaxTorpedos();
69
            }
70
71 10
            return $count;
72
        }
73
    }
74
75
    /**
76
     * @return array<string>
77
     */
78 3
    private function unloadTorpedo(int $unload, SpacecraftWrapperInterface $wrapper, ManagerProviderInterface $managerProvider): array
79
    {
80 3
        $user = $managerProvider->getUser();
81 3
        $ship = $wrapper->get();
82
83 3
        if ($ship->getUser() !== $user) {
84 1
            return [];
85
        }
86
87 2
        $torpedoType = $ship->getTorpedo();
88
89 2
        if ($torpedoType === null) {
90 1
            return [];
91
        }
92
93 1
        $managerProvider->upperStorage($torpedoType->getCommodity(), $unload);
94 1
        $this->shipTorpedoManager->changeTorpedo($wrapper, -$unload);
95
96 1
        return [sprintf(
97 1
            _('%s: Es wurden %d Torpedos des Typs %s vom Schiff transferiert'),
98 1
            $ship->getName(),
99 1
            $unload,
100 1
            $torpedoType->getName()
101 1
        )];
102
    }
103
104
    /**
105
     * @param array<int|string, mixed>|null $selectedTorpedoTypeArray
106
     */
107 6
    private function determineTorpedoType(SpacecraftWrapperInterface $wrapper, ?array $selectedTorpedoTypeArray): ?TorpedoTypeInterface
108
    {
109 6
        $spacecraft = $wrapper->get();
110
111 6
        if ($spacecraft->getTorpedoCount() > 0) {
112 1
            return $spacecraft->getTorpedo();
113
        }
114
115 5
        if ($selectedTorpedoTypeArray === null) {
0 ignored issues
show
introduced by
The condition $selectedTorpedoTypeArray === null is always false.
Loading history...
116 1
            return null;
117
        }
118
119 4
        if (!array_key_exists($spacecraft->getId(), $selectedTorpedoTypeArray)) {
120 1
            return null;
121
        }
122
123 3
        $selectedTorpedoTypeId = (int) $selectedTorpedoTypeArray[$spacecraft->getId()];
124 3
        $possibleTorpedoTypes = $wrapper->getPossibleTorpedoTypes();
125
126 3
        if (!array_key_exists($selectedTorpedoTypeId, $possibleTorpedoTypes)) {
127 1
            return null;
128
        }
129
130 2
        return $possibleTorpedoTypes[$selectedTorpedoTypeId];
131
    }
132
133
    /**
134
     * @return array<string>
135
     */
136 6
    private function loadTorpedo(
137
        int $load,
138
        ?TorpedoTypeInterface $torpedoType,
139
        SpacecraftWrapperInterface $wrapper,
140
        ManagerProviderInterface $managerProvider
141
    ): array {
142 6
        $ship = $wrapper->get();
143
144 6
        if ($torpedoType === null) {
145 3
            return [];
146
        }
147
148 3
        $storageElement = $managerProvider->getStorage()->get($torpedoType->getCommodityId());
149 3
        if ($storageElement === null) {
150 1
            return [sprintf(
151 1
                _('%s: Es sind keine Torpedos des Typs %s auf der %s vorhanden'),
152 1
                $ship->getName(),
153 1
                $torpedoType->getName(),
154 1
                $managerProvider->getName()
155 1
            )];
156
        }
157
158 2
        if ($load > $storageElement->getAmount()) {
159 1
            $load = $storageElement->getAmount();
160
        }
161
162 2
        $managerProvider->lowerStorage(
163 2
            $torpedoType->getCommodity(),
164 2
            $load
165 2
        );
166
167 2
        if ($ship->getTorpedoCount() === 0) {
168 1
            $this->shipTorpedoManager->changeTorpedo($wrapper, $load, $torpedoType);
169
        } else {
170 1
            $this->shipTorpedoManager->changeTorpedo($wrapper, $load);
171
        }
172
173 2
        $this->sendMessage(
174 2
            $load,
175 2
            $torpedoType,
176 2
            $managerProvider,
177 2
            $ship
178 2
        );
179
180 2
        return  [sprintf(
181 2
            _('%s: Es wurden %d Torpedos des Typs %s zum Schiff transferiert'),
182 2
            $ship->getName(),
183 2
            $load,
184 2
            $torpedoType->getName()
185 2
        )];
186
    }
187
188 2
    private function sendMessage(
189
        int $load,
190
        TorpedoTypeInterface $torpedoType,
191
        ManagerProviderInterface $managerProvider,
192
        SpacecraftInterface $spacecraft
193
    ): void {
194 2
        $this->privateMessageSender->send(
195 2
            $managerProvider->getUser()->getId(),
196 2
            $spacecraft->getUser()->getId(),
197 2
            sprintf(
198 2
                _('Die %s hat in Sektor %s %d %s auf die %s transferiert'),
199 2
                $managerProvider->getName(),
200 2
                $spacecraft->getSectorString(),
201 2
                $load,
202 2
                $torpedoType->getName(),
203 2
                $spacecraft->getName()
204 2
            ),
205 2
            PrivateMessageFolderTypeEnum::SPECIAL_TRADE,
206 2
            $spacecraft
207 2
        );
208
    }
209
}
210