Passed
Pull Request — master (#1801)
by Nico
23:50
created

CommodityTransferStrategy::transfer()   C

Complexity

Conditions 15
Paths 10

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 34
c 1
b 0
f 0
nc 10
nop 4
dl 0
loc 55
ccs 0
cts 32
cp 0
crap 240
rs 5.9166

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\Strategy;
6
7
use request;
8
use Stu\Lib\Information\InformationWrapper;
9
use Stu\Lib\Pirate\PirateReactionInterface;
10
use Stu\Lib\Pirate\PirateReactionTriggerEnum;
11
use Stu\Lib\Transfer\BeamUtilInterface;
12
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
13
use Stu\Module\Control\GameControllerInterface;
14
use Stu\Module\Ship\Lib\ShipWrapperInterface;
15
use Stu\Orm\Entity\ColonyInterface;
16
use Stu\Orm\Entity\ShipInterface;
17
18
class CommodityTransferStrategy implements TransferStrategyInterface
19
{
20
    public function __construct(
21
        private ColonyLibFactoryInterface $colonyLibFactory,
22
        private BeamUtilInterface $beamUtil,
23
        private PirateReactionInterface $pirateReaction
24
    ) {
25
    }
26
27
    public function setTemplateVariables(
28
        bool $isUnload,
29
        ShipInterface $ship,
30
        ShipInterface|ColonyInterface $target,
31
        GameControllerInterface $game
32
    ): void {
33
34
        $game->setTemplateVar(
35
            'BEAMABLE_STORAGE',
36
            $isUnload ? $ship->getBeamableStorage() : $target->getBeamableStorage()
37
        );
38
39
        if ($target instanceof ColonyInterface) {
40
            $game->setTemplateVar(
41
                'SHOW_SHIELD_FREQUENCY',
42
                $this->colonyLibFactory->createColonyShieldingManager($target)->isShieldingEnabled() && $target->getUser() !== $ship->getUser()
43
            );
44
        }
45
    }
46
47
    public function transfer(
48
        bool $isUnload,
49
        ShipWrapperInterface $wrapper,
50
        ShipInterface|ColonyInterface $target,
51
        InformationWrapper $informations
52
    ): void {
53
54
        $commodities = request::postArray('commodities');
55
        $gcount = request::postArray('count');
56
        if (count($commodities) == 0 || count($gcount) == 0) {
57
            $informations->addInformation(_("Es wurden keine Waren zum Beamen ausgewählt"));
58
            return;
59
        }
60
61
        $user = $wrapper->get()->getUser();
62
63
        if (
64
            $target instanceof ColonyInterface
65
            && $target->getUser() !== $user
66
            && $this->colonyLibFactory->createColonyShieldingManager($target)->isShieldingEnabled()
67
            && $target->getShieldFrequency() !== 0
68
        ) {
69
            $frequency = request::postInt('frequency');
70
            if ($frequency !== $target->getShieldFrequency()) {
71
                $informations->addInformation(_("Die Schildfrequenz ist nicht korrekt"));
72
                return;
73
            }
74
        }
75
76
        $hasTransfered = false;
77
78
        // check for fleet option
79
        $fleetWrapper = $wrapper->getFleetWrapper();
80
        if (request::postInt('isfleet') && $fleetWrapper !== null) {
81
            foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
82
                $hasTransfered = $hasTransfered ||  $this->transferPerShip(
83
                    $isUnload,
84
                    $wrapper,
85
                    $target,
86
                    $informations
87
                );
88
            }
89
        } else {
90
            $hasTransfered =  $this->transferPerShip($isUnload, $wrapper, $target, $informations);
91
        }
92
93
        if (
94
            !$isUnload
95
            && $hasTransfered
96
            && $target instanceof ShipInterface
97
        ) {
98
            $this->pirateReaction->checkForPirateReaction(
99
                $target,
100
                PirateReactionTriggerEnum::ON_BEAM,
101
                $wrapper->get()
102
            );
103
        }
104
    }
105
106
    private function transferPerShip(
107
        bool $isUnload,
108
        ShipWrapperInterface $wrapper,
109
        ShipInterface|ColonyInterface $target,
110
        InformationWrapper $informations
111
    ): bool {
112
113
        $ship = $wrapper->get();
114
        $epsSystem = $wrapper->getEpsSystemData();
115
116
        //sanity checks
117
        $isDockTransfer = $this->beamUtil->isDockTransfer($ship, $target);
118
        if (!$isDockTransfer && ($epsSystem === null || $epsSystem->getEps() === 0)) {
119
            $informations->addInformation(_("Keine Energie vorhanden"));
120
            return false;
121
        }
122
        if ($ship->getCloakState()) {
123
            $informations->addInformation(_("Die Tarnung ist aktiviert"));
124
            return false;
125
        }
126
        if ($ship->getWarpState()) {
127
            $informations->addInformation(_("Der Warpantrieb ist aktiviert"));
128
            return false;
129
        }
130
        if ($target instanceof ShipInterface && $target->getWarpState()) {
131
            $informations->addInformation(sprintf(_('Die %s befindet sich im Warp'), $target->getName()));
132
            return false;
133
        }
134
135
        $transferTarget = $isUnload ? $target : $ship;
136
        if ($transferTarget->getMaxStorage() <= $transferTarget->getStorageSum()) {
137
            $informations->addInformation(sprintf(_('%s: Der Lagerraum ist voll'), $transferTarget->getName()));
138
            return false;
139
        }
140
141
        $commodities = request::postArray('commodities');
142
        $gcount = request::postArray('count');
143
144
        $storage = $isUnload ? $ship->getStorage() : $target->getStorage();
145
146
        if ($storage->isEmpty()) {
147
            $informations->addInformation(_("Keine Waren zum Beamen vorhanden"));
148
            return false;
149
        }
150
        if (count($commodities) == 0 || count($gcount) == 0) {
151
            $informations->addInformation(_("Es wurden keine Waren zum Beamen ausgewählt"));
152
            return false;
153
        }
154
        $informations->addInformation(sprintf(
155
            _('Die %s hat folgende Waren %s %s %s transferiert'),
156
            $ship->getName(),
157
            $isUnload ? 'zur' : 'von der',
158
            $target instanceof ColonyInterface ? 'Kolonie' : '',
159
            $target->getName()
160
        ));
161
162
        $hasTransfered = false;
163
        foreach ($commodities as $key => $value) {
164
            $commodityId = (int) $value;
165
166
            if (!array_key_exists($key, $gcount)) {
167
                continue;
168
            }
169
170
            $hasTransfered = $hasTransfered || $this->beamUtil->transferCommodity(
171
                $commodityId,
172
                $gcount[$key],
173
                $wrapper,
174
                $isUnload ? $ship : $target,
175
                $transferTarget,
176
                $informations
177
            );
178
        }
179
180
        return $hasTransfered;
181
    }
182
}
183