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

BeamTo::beamToTarget()   C

Complexity

Conditions 14
Paths 20

Size

Total Lines 69
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

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