Passed
Push — dev ( 5ebfc2...850c22 )
by Janko
12:27
created

RenameBuildplan::handle()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 25
nc 7
nop 1
dl 0
loc 43
rs 8.0555
c 1
b 0
f 0
ccs 0
cts 25
cp 0
crap 90
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\NPC\Action;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use request;
9
use Stu\Exception\AccessViolation;
10
use Stu\Lib\CleanTextUtils;
11
use Stu\Module\Control\ActionControllerInterface;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Orm\Repository\ShipBuildplanRepositoryInterface;
14
use Stu\Orm\Repository\NPCLogRepositoryInterface;
15
16
final class RenameBuildplan implements ActionControllerInterface
17
{
18
    public const ACTION_IDENTIFIER = 'B_CHANGE_BUILDPLAN_NAME';
19
20 1
    public function __construct(
21
        private ShipBuildplanRepositoryInterface $shipBuildplanRepository,
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->shipBuildplanRepository->find($buildplanId);
54
        if ($plan === null || $plan->getUserId() !== $userId) {
55
            throw new AccessViolation();
56
        }
57
58
        $oldName = $plan->getName();
59
        $plan->setName($newName);
60
61
        $this->shipBuildplanRepository->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