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

BeamFrom::handle()   C

Complexity

Conditions 16
Paths 15

Size

Total Lines 101
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
cc 16
eloc 68
nc 15
nop 1
dl 0
loc 101
ccs 0
cts 70
cp 0
crap 272
rs 5.5666
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\Colony\Action\BeamFrom;
6
7
use request;
8
use Stu\Component\Ship\System\ShipSystemTypeEnum;
9
use Stu\Lib\BeamUtil\BeamUtilInterface;
10
use Stu\Module\Colony\Lib\ColonyLoaderInterface;
11
use Stu\Module\Colony\View\ShowColony\ShowColony;
12
use Stu\Module\Control\ActionControllerInterface;
13
use Stu\Module\Control\GameControllerInterface;
14
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
15
use Stu\Module\Ship\Lib\Interaction\InteractionCheckerInterface;
16
use Stu\Module\Ship\Lib\ShipLoaderInterface;
17
use Stu\Module\Ship\View\ShowShip\ShowShip;
18
19
final class BeamFrom implements ActionControllerInterface
20
{
21
    public const ACTION_IDENTIFIER = 'B_BEAMFROM';
22
23
    private ColonyLoaderInterface $colonyLoader;
24
25
    private BeamUtilInterface $beamUtil;
26
27
    private ShipLoaderInterface $shipLoader;
28
29
    private InteractionCheckerInterface $interactionChecker;
30
31
    public function __construct(
32
        ColonyLoaderInterface $colonyLoader,
33
        BeamUtilInterface $beamUtil,
34
        ShipLoaderInterface $shipLoader,
35
        InteractionCheckerInterface $interactionChecker
36
    ) {
37
        $this->colonyLoader = $colonyLoader;
38
        $this->beamUtil = $beamUtil;
39
        $this->shipLoader = $shipLoader;
40
        $this->interactionChecker = $interactionChecker;
41
    }
42
43
    public function handle(GameControllerInterface $game): void
44
    {
45
        $game->setView(ShowColony::VIEW_IDENTIFIER);
46
47
        $userId = $game->getUser()->getId();
48
49
        $colony = $this->colonyLoader->byIdAndUser(
50
            request::indInt('id'),
51
            $userId
52
        );
53
54
        if ($colony->getEps() == 0) {
55
            $game->addInformation(_('Keine Energie vorhanden'));
56
            return;
57
        }
58
        $wrapper = $this->shipLoader->find(request::postIntFatal('target'));
59
        if ($wrapper === null) {
60
            return;
61
        }
62
63
        $ship = $wrapper->get();
64
65
        if (!$this->interactionChecker->checkColonyPosition($colony, $ship)) {
66
            return;
67
        }
68
69
        if ($ship->getUser()->isVacationRequestOldEnough()) {
70
            $game->addInformation(_('Aktion nicht möglich, der Spieler befindet sich im Urlaubsmodus!'));
71
            return;
72
        }
73
74
        if (
75
            $ship->getShieldState()
76
            && $ship->getUser()->getId() !== $userId
77
        ) {
78
            $game->addInformationf(_('Die %s hat die Schilde aktiviert'), $ship->getName());
79
            return;
80
        }
81
        if (
82
            $ship->isSystemHealthy(ShipSystemTypeEnum::SYSTEM_BEAM_BLOCKER)
83
            && $ship->getUser()->getId() !== $userId
84
        ) {
85
            $game->addInformation(sprintf(_('Die %s hat einen Beamblocker aktiviert. Transfer nicht möglich.'), $ship->getName()));
86
            return;
87
        }
88
        if (!$colony->storagePlaceLeft()) {
89
            $game->addInformationf(_('Der Lagerraum der %s ist voll'), $colony->getName());
90
            return;
91
        }
92
        $commodities = request::postArray('commodities');
93
        $gcount = request::postArray('count');
94
95
        $shipStorage = $ship->getStorage();
96
97
        if ($shipStorage->isEmpty()) {
98
            $game->addInformation(_('Keine Waren zum Beamen vorhanden'));
99
            return;
100
        }
101
        if (count($commodities) == 0 || count($gcount) == 0) {
102
            $game->addInformation(_('Es wurden keine Waren zum Beamen ausgewählt'));
103
            return;
104
        }
105
        $isOwnedByCurrentUser = $game->getUser() === $ship->getUser();
106
        if ($isOwnedByCurrentUser) {
107
            $link = sprintf("ship.php?%s=1&id=%d", ShowShip::VIEW_IDENTIFIER, $ship->getId());
108
109
            $game->addInformationfWithLink(
110
                _('Die Kolonie %s hat folgende Waren von der %s transferiert'),
111
                $link,
112
                $colony->getName(),
113
                $ship->getName()
114
            );
115
        } else {
116
            $game->addInformationf(
117
                _('Die Kolonie %s hat folgende Waren von der %s transferiert'),
118
                $colony->getName(),
119
                $ship->getName()
120
            );
121
        }
122
        foreach ($commodities as $key => $value) {
123
            $commodityId = (int) $value;
124
125
            if (!array_key_exists($key, $gcount)) {
126
                continue;
127
            }
128
129
            $this->beamUtil->transferCommodity(
130
                $commodityId,
131
                $gcount[$key],
132
                $colony,
133
                $ship,
134
                $colony,
135
                $game
136
            );
137
        }
138
139
        $game->sendInformation(
140
            $ship->getUser()->getId(),
141
            $userId,
142
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_TRADE,
143
            sprintf('ship.php?%s=1&id=%d', ShowShip::VIEW_IDENTIFIER, $ship->getId())
144
        );
145
    }
146
147
    public function performSessionCheck(): bool
148
    {
149
        return true;
150
    }
151
}
152