Test Failed
Pull Request — master (#1775)
by Nico
26:52
created

CommodityCheat   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
c 1
b 0
f 0
dl 0
loc 109
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 66 8
A __construct() 0 10 1
A createEntry() 0 10 1
A performSessionCheck() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\NPC\Action;
6
7
use request;
8
use Stu\Exception\ShipDoesNotExistException;
9
use Stu\Module\NPC\View\ShowTools\ShowTools;
10
use Stu\Module\Control\ActionControllerInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Ship\Lib\ShipLoaderInterface;
13
use Stu\Component\Ship\Storage\ShipStorageManagerInterface;
14
use Stu\Orm\Repository\CommodityRepositoryInterface;
15
use Stu\Orm\Repository\NPCLogRepositoryInterface;
16
17
final class CommodityCheat implements ActionControllerInterface
18
{
19
    public const ACTION_IDENTIFIER = 'B_COMMODITY_CHEAT';
20
21
    private ShipLoaderInterface $shipLoader;
22
23
    private ShipStorageManagerInterface $shipStorageManager;
24
25
    private CommodityRepositoryInterface $commodityRepository;
26
27
    private NPCLogRepositoryInterface $npcLogRepository;
28
29
30
    public function __construct(
31
        ShipLoaderInterface $shipLoader,
32
        ShipStorageManagerInterface $shipStorageManager,
33
        CommodityRepositoryInterface $commodityRepository,
34
        NPCLogRepositoryInterface $npcLogRepository
35
    ) {
36
        $this->shipLoader = $shipLoader;
37
        $this->shipStorageManager = $shipStorageManager;
38
        $this->commodityRepository = $commodityRepository;
39
        $this->npcLogRepository = $npcLogRepository;
40
    }
41
42
    public function handle(GameControllerInterface $game): void
43
    {
44
        $game->setView(ShowTools::VIEW_IDENTIFIER);
45
        $user = $game->getUser();
46
47
        // only Admins or NPC can trigger
48
        if (!$game->isAdmin() && !$game->isNpc()) {
49
            $game->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin/NPC![/color][/b]'));
50
            return;
51
        }
52
53
        if (!request::getVarByMethod(request::postvars(), 'shipid')) {
54
            $game->addInformation("Kein Schiff ausgewählt");
55
            return;
56
        } else {
57
            $shipId = request::postInt('shipid');
58
            $commodityId = request::postInt('commodityid');
59
            $amount = request::postInt('amount');
60
            $reason = request::postString('reason');
61
62
63
            $wrapper = $this->shipLoader->find($shipId);
64
65
            if ($wrapper === null) {
66
                throw new ShipDoesNotExistException(_('Ship does not exist!'));
67
            } else {
68
                $ship = $wrapper->get();
69
            }
70
71
            if ($amount < 1) {
72
                $game->addInformation("Anzahl muss größer als 0 sein");
73
                return;
74
            }
75
76
            if ($reason === '') {
0 ignored issues
show
introduced by
The condition $reason === '' is always false.
Loading history...
77
                $game->addInformation("Grund fehlt");
78
                return;
79
            }
80
81
            $commodity = $this->commodityRepository->find($commodityId);
82
83
            if ($commodity === null) {
84
                $game->addInformation("Ungültige Ware");
85
                return;
86
            }
87
88
            $this->shipStorageManager->upperStorage(
89
                $ship,
90
                $commodity,
91
                $amount
92
            );
93
94
            $text = sprintf(
95
                '%s hat dem Schiff %s (%d) von Spieler %s (%d) %d %s hinzugefügt. Grund: %s',
96
                $user->getName(),
97
                $ship->getName(),
98
                $ship->getId(),
99
                $ship->getUser()->getName(),
100
                $ship->getUser()->getId(),
101
                $amount,
102
                $commodity->getName(),
103
                $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

103
                /** @scrutinizer ignore-type */ $reason
Loading history...
104
            );
105
106
            $this->createEntry($text, $user->getId());
107
            $game->addInformation("Waren hinzugefügt");
108
        }
109
    }
110
111
    private function createEntry(
112
        string $text,
113
        int $UserId
114
    ): void {
115
        $entry = $this->npcLogRepository->prototype();
116
        $entry->setText($text);
117
        $entry->setSourceUserId($UserId);
118
        $entry->setDate(time());
119
120
        $this->npcLogRepository->save($entry);
121
    }
122
123
    public function performSessionCheck(): bool
124
    {
125
        return true;
126
    }
127
}
128