Passed
Push — dev ( d85471...98511f )
by Janko
18:01
created

RenameBuildplan::createLogEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 5
dl 0
loc 16
rs 9.9
c 1
b 0
f 0
ccs 0
cts 13
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\NPC\Action;
6
7
use Override;
8
use request;
9
use Stu\Exception\AccessViolationException;
10
use Stu\Lib\CleanTextUtils;
11
use Stu\Module\Control\ActionControllerInterface;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Orm\Repository\SpacecraftBuildplanRepositoryInterface;
14
use Stu\Orm\Repository\NPCLogRepositoryInterface;
15
16
final class RenameBuildplan implements ActionControllerInterface
17
{
18
    public const string ACTION_IDENTIFIER = 'B_CHANGE_BUILDPLAN_NAME';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 18 at column 24
Loading history...
19
20 1
    public function __construct(
21
        private SpacecraftBuildplanRepositoryInterface $spacecraftBuildplanRepository,
22
        private NPCLogRepositoryInterface $npcLogRepository
23 1
    ) {}
24
25
    #[Override]
26
    public function handle(GameControllerInterface $game): void
27
    {
28
        $userId = $game->getUser()->getId();
29
30
        if (!$game->isAdmin() && !$game->isNpc()) {
31
            $game->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin/NPC![/color][/b]'));
32
            return;
33
        }
34
35
        $buildplanId = request::postIntFatal('planid');
36
        $newName = CleanTextUtils::clearEmojis(request::postStringFatal('newName'));
37
38
        if (mb_strlen($newName) === 0) {
39
            return;
40
        }
41
42
        $nameWithoutUnicode = CleanTextUtils::clearUnicode($newName);
43
        if ($newName !== $nameWithoutUnicode) {
44
            $game->addInformation(_('Der Name enthält ungültigen Unicode'));
45
            return;
46
        }
47
48
        if (mb_strlen($newName) > 255) {
49
            $game->addInformation(_('Der Name ist zu lang (Maximum: 255 Zeichen)'));
50
            return;
51
        }
52
53
        $plan = $this->spacecraftBuildplanRepository->find($buildplanId);
54
        if ($plan === null || $plan->getUserId() !== $userId) {
55
            throw new AccessViolationException();
56
        }
57
58
        $oldName = $plan->getName();
59
        $plan->setName($newName);
60
61
        $this->spacecraftBuildplanRepository->save($plan);
62
63
        if ($game->getUser()->isNpc()) {
64
            $this->createLogEntry($oldName, $newName, $userId, $game->getUser()->getName(), $plan->getUser()->getName());
65
        }
66
67
        $game->addInformation(_('Der Name des Bauplans wurde geändert'));
68
    }
69
70
    private function createLogEntry(string $oldName, string $newName, int $userId, string $userName, string $planuser): void
71
    {
72
        $logText = sprintf(
73
            '%s hat den Bauplan %s des Spielers %s zu %s umbenannt.',
74
            $userName,
75
            $oldName,
76
            $planuser,
77
            $newName
78
        );
79
80
        $entry = $this->npcLogRepository->prototype();
81
        $entry->setText($logText);
82
        $entry->setSourceUserId($userId);
83
        $entry->setDate(time());
84
85
        $this->npcLogRepository->save($entry);
86
    }
87
88
    #[Override]
89
    public function performSessionCheck(): bool
90
    {
91
        return true;
92
    }
93
}
94