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

DeleteBuildplan   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 5.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 60
rs 10
c 1
b 0
f 0
ccs 2
cts 34
cp 0.0588
wmc 8
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\Module\Control\ActionControllerInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Orm\Repository\SpacecraftBuildplanRepositoryInterface;
12
use Stu\Orm\Repository\NPCLogRepositoryInterface;
13
14
final class DeleteBuildplan implements ActionControllerInterface
15
{
16
    public const string ACTION_IDENTIFIER = 'B_DELETE_BUILDPLAN';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 16 at column 24
Loading history...
17
18 1
    public function __construct(
19
        private SpacecraftBuildplanRepositoryInterface $spacecraftBuildplanRepository,
20
        private NPCLogRepositoryInterface $npcLogRepository
21 1
    ) {}
22
23
    #[Override]
24
    public function handle(GameControllerInterface $game): void
25
    {
26
        $userId = $game->getUser()->getId();
27
        $buildplanId = request::postIntFatal('planid');
28
        if ($buildplanId == null) {
29
            $game->addInformation('Es wurde kein Bauplan ausgewählt');
30
            return;
31
        }
32
33
        $buildplan = $this->spacecraftBuildplanRepository->find($buildplanId);
34
        if ($buildplan === null || $buildplan->getUserId() !== $userId) {
35
            $game->addInformation('Der Bauplan konnte nicht gelöscht werden');
36
            return;
37
        }
38
39
        $crewCount = $buildplan->getCrew();
40
41
42
        $this->spacecraftBuildplanRepository->delete($buildplan);
43
44
        $logText = sprintf(
45
            '%s hat den Bauplan %s (%d) von Benutzer %s (%d) gelöscht. Crew: %d',
46
            $game->getUser()->getName(),
47
            $buildplan->getName(),
48
            $buildplan->getId(),
49
            $buildplan->getUser()->getName(),
50
            $buildplan->getUserId(),
51
            $crewCount
52
        );
53
        if ($game->getUser()->isNpc()) {
54
            $this->createLogEntry($logText, $userId);
55
        }
56
57
        $game->addInformation('Der Bauplan wurde gelöscht');
58
    }
59
60
    private function createLogEntry(string $text, int $userId): void
61
    {
62
        $entry = $this->npcLogRepository->prototype();
63
        $entry->setText($text);
64
        $entry->setSourceUserId($userId);
65
        $entry->setDate(time());
66
67
        $this->npcLogRepository->save($entry);
68
    }
69
70
    #[Override]
71
    public function performSessionCheck(): bool
72
    {
73
        return true;
74
    }
75
}
76