Passed
Push — master ( 69410e...6e6431 )
by Nico
42:43 queued 11:08
created

BeamFrom::beamFromTarget()   C

Complexity

Conditions 17
Paths 22

Size

Total Lines 74
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
cc 17
eloc 51
nc 22
nop 3
dl 0
loc 74
ccs 0
cts 55
cp 0
crap 306
rs 5.2166
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\Module\Ship\Action\BeamFrom;
6
7
use request;
8
use Stu\Component\Ship\System\ShipSystemTypeEnum;
9
use Stu\Lib\BeamUtil\BeamUtilInterface;
10
use Stu\Module\Control\ActionControllerInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
13
use Stu\Module\Ship\Lib\Interaction\InteractionChecker;
14
use Stu\Module\Ship\Lib\ShipLoaderInterface;
15
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
16
use Stu\Module\Ship\Lib\ShipWrapperInterface;
17
use Stu\Module\Ship\View\ShowShip\ShowShip;
18
use Stu\Orm\Entity\ShipInterface;
19
20
final class BeamFrom implements ActionControllerInterface
21
{
22
    public const ACTION_IDENTIFIER = 'B_BEAMFROM';
23
24
    private ShipLoaderInterface $shipLoader;
25
26
    private BeamUtilInterface $beamUtil;
27
28
    private ShipWrapperFactoryInterface $shipWrapperFactory;
29
30
    public function __construct(
31
        ShipLoaderInterface $shipLoader,
32
        BeamUtilInterface $beamUtil,
33
        ShipWrapperFactoryInterface $shipWrapperFactory
34
    ) {
35
        $this->shipLoader = $shipLoader;
36
        $this->beamUtil = $beamUtil;
37
        $this->shipWrapperFactory = $shipWrapperFactory;
38
    }
39
40
    public function handle(GameControllerInterface $game): void
41
    {
42
        $game->setView(ShowShip::VIEW_IDENTIFIER);
43
44
        $userId = $game->getUser()->getId();
45
46
        $shipId = request::indInt('id');
47
        $targetId = request::postIntFatal('target');
48
49
        $wrappers = $this->shipLoader->getWrappersBySourceAndUserAndTarget(
50
            $shipId,
51
            $userId,
52
            $targetId
53
        );
54
55
        $wrapper = $wrappers->getSource();
56
        $ship = $wrapper->get();
57
58
59
        //bad request
60
        if (!$ship->hasEnoughCrew($game)) {
61
            return;
62
        }
63
64
        $targetWrapper = $wrappers->getTarget();
65
        if ($targetWrapper === null) {
66
            return;
67
        }
68
        $target = $targetWrapper->get();
69
70
        if (!InteractionChecker::canInteractWith($ship, $target, $game, true)) {
71
            return;
72
        }
73
74
        if ($target->getWarpState()) {
75
            $game->addInformation(sprintf(_('Die %s befindet sich im Warp'), $target->getName()));
76
            return;
77
        }
78
79
        // check for fleet option
80
        if (request::postInt('isfleet') && $ship->getFleet() !== null) {
81
            foreach ($ship->getFleet()->getShips() as $ship) {
82
                $this->beamFromTarget(
83
                    $this->shipWrapperFactory->wrapShip($ship),
84
                    $target,
85
                    $game
86
                );
87
            }
88
        } else {
89
            $this->beamFromTarget($wrapper, $target, $game);
90
        }
91
    }
92
93
    private function beamFromTarget(ShipWrapperInterface $wrapper, ShipInterface $target, GameControllerInterface $game): void
94
    {
95
        $ship = $wrapper->get();
96
        $epsSystem = $wrapper->getEpsSystemData();
97
98
        //sanity checks
99
        $isDockTransfer = $ship->getDockedTo() === $target || $target->getDockedTo() === $ship;
100
        if (!$isDockTransfer && ($epsSystem === null || $epsSystem->getEps() === 0)) {
101
            $game->addInformation(_("Keine Energie vorhanden"));
102
            return;
103
        }
104
        if ($ship->getCloakState()) {
105
            $game->addInformation(_("Die Tarnung ist aktiviert"));
106
            return;
107
        }
108
        if ($ship->getWarpState()) {
109
            $game->addInformation(_("Der Warpantrieb ist aktiviert"));
110
            return;
111
        }
112
        if (!$isDockTransfer && $ship->getShieldState()) {
113
            $game->addInformation(_("Die Schilde sind aktiviert"));
114
            return;
115
        }
116
        if ($ship->getDockedTo() !== $target && $target->isSystemHealthy(ShipSystemTypeEnum::SYSTEM_BEAM_BLOCKER)) {
117
            $game->addInformation(sprintf(_('Die %s hat einen Beamblocker aktiviert. Zum Warentausch andocken.'), $target->getName()));
118
            return;
119
        }
120
        if ($ship->getMaxStorage() <= $ship->getStorageSum()) {
121
            $game->addInformation(sprintf(_('Der Lagerraum der %s ist voll'), $ship->getName()));
122
            return;
123
        }
124
125
        $commodities = request::postArray('commodities');
126
        $gcount = request::postArray('count');
127
128
        $targetStorage = $target->getStorage();
129
130
        if ($targetStorage->isEmpty()) {
131
            $game->addInformation(_("Keine Waren zum Beamen vorhanden"));
132
            return;
133
        }
134
        if (count($commodities) == 0 || count($gcount) == 0) {
135
            $game->addInformation(_("Es wurden keine Waren zum Beamen ausgewählt"));
136
            return;
137
        }
138
        $game->addInformation(
139
            sprintf(
140
                _('Die %s hat folgende Waren von der %s transferiert'),
141
                $ship->getName(),
142
                $target->getName()
143
            )
144
        );
145
        foreach ($commodities as $key => $value) {
146
            $commodityId = (int) $value;
147
148
            if (!array_key_exists($key, $gcount)) {
149
                continue;
150
            }
151
152
            $this->beamUtil->transferCommodity(
153
                $commodityId,
154
                $gcount[$key],
155
                $wrapper,
156
                $target,
157
                $wrapper->get(),
158
                $game
159
            );
160
        }
161
162
        $game->sendInformation(
163
            $target->getUser()->getId(),
164
            $ship->getUser()->getId(),
165
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_TRADE,
166
            sprintf('ship.php?%s=1&id=%d', ShowShip::VIEW_IDENTIFIER, $target->getId())
167
        );
168
    }
169
170
    public function performSessionCheck(): bool
171
    {
172
        return true;
173
    }
174
}
175