Passed
Pull Request — master (#1928)
by Janko
12:21
created

CreateShip::handle()   C

Complexity

Conditions 11
Paths 12

Size

Total Lines 85
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 58
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 85
ccs 0
cts 61
cp 0
crap 132
rs 6.7696

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\NPC\Action;
6
7
use request;
8
use RuntimeException;
9
use Stu\Module\Control\ActionControllerInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Ship\Lib\ShipCreatorInterface;
12
use Stu\Orm\Repository\MapRepositoryInterface;
13
use Stu\Orm\Repository\ShipBuildplanRepositoryInterface;
14
use Stu\Module\NPC\View\ShowShipCreator\ShowShipCreator;
15
use Stu\Orm\Repository\NPCLogRepositoryInterface;
16
use Stu\Orm\Repository\LayerRepositoryInterface;
17
use Stu\Orm\Repository\UserRepositoryInterface;
18
19
final class CreateShip implements ActionControllerInterface
20
{
21
    public const ACTION_IDENTIFIER = 'B_CREATE_SHIP';
22
23
    private ShipCreatorInterface $shipCreator;
24
    private MapRepositoryInterface $mapRepository;
25
    private ShipBuildplanRepositoryInterface $buildplanRepository;
26
    private NPCLogRepositoryInterface $npcLogRepository;
27
    private LayerRepositoryInterface $layerRepository;
28
    private UserRepositoryInterface $userRepository;
29
30
    public function __construct(
31
        ShipCreatorInterface $shipCreator,
32
        MapRepositoryInterface $mapRepository,
33
        ShipBuildplanRepositoryInterface $buildplanRepository,
34
        NPCLogRepositoryInterface $npcLogRepository,
35
        LayerRepositoryInterface $layerRepository,
36
        UserRepositoryInterface $userRepository
37
    ) {
38
        $this->shipCreator = $shipCreator;
39
        $this->mapRepository = $mapRepository;
40
        $this->buildplanRepository = $buildplanRepository;
41
        $this->npcLogRepository = $npcLogRepository;
42
        $this->layerRepository = $layerRepository;
43
        $this->userRepository = $userRepository;
44
    }
45
46
    public function handle(GameControllerInterface $game): void
47
    {
48
        $game->setView(ShowShipCreator::VIEW_IDENTIFIER);
49
50
        if (!$game->isAdmin() && !$game->isNpc()) {
51
            $game->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin/NPC![/color][/b]'));
52
            return;
53
        }
54
55
        $userId = request::postIntFatal('userId');
56
        $buildplanId = request::postIntFatal('buildplanId');
57
        $shipCount = request::postIntFatal('shipcount');
58
        $layerId = request::postIntFatal('layer');
59
        $cx = request::postIntFatal('cx');
60
        $cy = request::postIntFatal('cy');
61
        $reason = request::postString('reason');
62
        $torpedoTypeId = request::postInt('torpedoTypeId');
63
64
        if ($reason === '') {
0 ignored issues
show
introduced by
The condition $reason === '' is always false.
Loading history...
65
            $game->addInformation("Grund fehlt");
66
            return;
67
        }
68
69
        $user = $this->userRepository->find($userId);
70
        if ($user === null) {
71
            throw new RuntimeException(sprintf('userId %d does not exist', $userId));
72
        }
73
74
        $buildplan = $this->buildplanRepository->find($buildplanId);
75
        if ($buildplan === null) {
76
            throw new RuntimeException(sprintf('buildplanId %d does not exist', $buildplanId));
77
        }
78
79
        $layer = $this->layerRepository->find($layerId);
80
        if ($layer === null) {
81
            throw new RuntimeException(sprintf('layerId %d does not exist', $layerId));
82
        }
83
84
        $field = $this->mapRepository->getByCoordinates($layer, $cx, $cy);
85
        if ($field === null) {
86
            $game->addInformation(sprintf(
87
                'Die Position %s|%d|%d existiert nicht!',
88
                $layer->getName(),
89
                $cx,
90
                $cy
91
            ));
92
            return;
93
        }
94
95
        $moduleNames = [];
96
        foreach ($buildplan->getModules() as $module) {
97
            $moduleNames[] = $module->getModule()->getName();
98
        }
99
100
        for ($i = 0; $i < $shipCount; $i++) {
101
            $creator = $this->shipCreator
102
                ->createBy($userId, $buildplan->getRump()->getId(), $buildplan->getId())
103
                ->setLocation($field)
104
                ->maxOutSystems()
105
                ->createCrew();
106
107
            if ($torpedoTypeId > 0) {
108
                $creator->setTorpedo($torpedoTypeId);
109
            }
110
111
            $creator->finishConfiguration();
112
        }
113
114
        $logText = sprintf(
115
            '%s hat für Spieler %s (%d) %dx %s erstellt. Module: %s, Position: %s|%d|%d, Grund: %s',
116
            $game->getUser()->getName(),
117
            $user->getName(),
118
            $userId,
119
            $shipCount,
120
            $buildplan->getName(),
121
            implode(', ', $moduleNames),
122
            $layer->getName(),
123
            $cx,
124
            $cy,
125
            $reason
0 ignored issues
show
Bug introduced by
$reason of type false is incompatible with the type double|integer|string expected by parameter $values of sprintf(). ( Ignorable by Annotation )

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

125
            /** @scrutinizer ignore-type */ $reason
Loading history...
126
        );
127
128
        $this->createLogEntry($logText, $game->getUser()->getId());
129
130
        $game->addInformation(sprintf('%d Schiff(e) wurden erstellt', $shipCount));
131
    }
132
133
    private function createLogEntry(string $text, int $userId): void
134
    {
135
        $entry = $this->npcLogRepository->prototype();
136
        $entry->setText($text);
137
        $entry->setSourceUserId($userId);
138
        $entry->setDate(time());
139
140
        $this->npcLogRepository->save($entry);
141
    }
142
143
    public function performSessionCheck(): bool
144
    {
145
        return true;
146
    }
147
}