Passed
Push — master ( 26f240...a6e052 )
by Nico
64:14 queued 29:51
created

CreateShip::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 6
dl 0
loc 14
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\NPC\Action;
6
7
use request;
8
use Stu\Module\Control\ActionControllerInterface;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\Ship\Lib\ShipCreatorInterface;
11
use Stu\Orm\Repository\MapRepositoryInterface;
12
use Stu\Orm\Repository\ShipBuildplanRepositoryInterface;
13
use Stu\Module\NPC\View\ShowShipCreator\ShowShipCreator;
14
use Stu\Orm\Repository\NPCLogRepositoryInterface;
15
use Stu\Orm\Repository\LayerRepositoryInterface;
16
use Stu\Orm\Repository\UserRepositoryInterface;
17
18
final class CreateShip implements ActionControllerInterface
19
{
20
    public const ACTION_IDENTIFIER = 'B_CREATE_SHIP';
21
22
    private ShipCreatorInterface $shipCreator;
23
    private MapRepositoryInterface $mapRepository;
24
    private ShipBuildplanRepositoryInterface $buildplanRepository;
25
    private NPCLogRepositoryInterface $npcLogRepository;
26
    private LayerRepositoryInterface $layerRepository;
27
    private UserRepositoryInterface $userRepository;
28
29
    public function __construct(
30
        ShipCreatorInterface $shipCreator,
31
        MapRepositoryInterface $mapRepository,
32
        ShipBuildplanRepositoryInterface $buildplanRepository,
33
        NPCLogRepositoryInterface $npcLogRepository,
34
        LayerRepositoryInterface $layerRepository,
35
        UserRepositoryInterface $userRepository
36
    ) {
37
        $this->shipCreator = $shipCreator;
38
        $this->mapRepository = $mapRepository;
39
        $this->buildplanRepository = $buildplanRepository;
40
        $this->npcLogRepository = $npcLogRepository;
41
        $this->layerRepository = $layerRepository;
42
        $this->userRepository = $userRepository;
43
    }
44
45
    public function handle(GameControllerInterface $game): void
46
    {
47
        $game->setView(ShowShipCreator::VIEW_IDENTIFIER);
48
49
        if (!$game->isAdmin() && !$game->isNpc()) {
50
            $game->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin/NPC![/color][/b]'));
51
            return;
52
        }
53
54
        $userId = request::postIntFatal('userId');
55
        $buildplanId = request::postIntFatal('buildplanId');
56
        $shipCount = request::postIntFatal('shipcount');
57
        $layerId = request::postIntFatal('layer');
58
        $cx = request::postIntFatal('cx');
59
        $cy = request::postIntFatal('cy');
60
        $reason = request::postString('reason');
61
62
        if ($reason === '') {
0 ignored issues
show
introduced by
The condition $reason === '' is always false.
Loading history...
63
            $game->addInformation("Grund fehlt");
64
            return;
65
        }
66
67
        $user = $this->userRepository->find($userId);
68
        $buildplan = $this->buildplanRepository->find($buildplanId);
69
        $layer = $this->layerRepository->find($layerId);
70
71
        $field = $this->mapRepository->getByCoordinates($layer, $cx, $cy);
72
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
            $ship = $this->shipCreator
0 ignored issues
show
Unused Code introduced by
The assignment to $ship is dead and can be removed.
Loading history...
90
                ->createBy($userId, $buildplan->getRump()->getId(), $buildplan->getId())
91
                ->setLocation($this->mapRepository->getByCoordinates($layer, $cx, $cy))
0 ignored issues
show
Bug introduced by
It seems like $this->mapRepository->ge...nates($layer, $cx, $cy) can also be of type null; however, parameter $location of Stu\Module\Ship\Lib\Ship...nterface::setLocation() does only seem to accept Stu\Orm\Entity\LocationInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

91
                ->setLocation(/** @scrutinizer ignore-type */ $this->mapRepository->getByCoordinates($layer, $cx, $cy))
Loading history...
92
                ->maxOutSystems()
93
                ->createCrew()
94
                ->finishConfiguration();
95
        }
96
97
        $logText = sprintf(
98
            '%s hat für Spieler %s (%d) %dx %s erstellt. Module: %s, Position: %s %d|%d, Grund: %s',
99
            $game->getUser()->getName(),
100
            $user->getName(),
101
            $userId,
102
            $shipCount,
103
            $buildplan->getName(),
104
            implode(', ', $moduleNames),
105
            $layer->getName(),
106
            $cx,
107
            $cy,
108
            $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

108
            /** @scrutinizer ignore-type */ $reason
Loading history...
109
        );
110
111
112
        $this->createLogEntry($logText, $game->getUser()->getId());
113
114
        $game->addInformation(sprintf('%d Schiff(e) wurden ohne Marderschaden erstellt', $shipCount));
115
    }
116
117
    private function createLogEntry(string $text, int $userId): void
118
    {
119
        $entry = $this->npcLogRepository->prototype();
120
        $entry->setText($text);
121
        $entry->setSourceUserId($userId);
122
        $entry->setDate(time());
123
124
        $this->npcLogRepository->save($entry);
125
    }
126
127
    public function performSessionCheck(): bool
128
    {
129
        return true;
130
    }
131
}