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