Completed
Pull Request — master (#139)
by De Cramer
02:36
created

ChatCommandDataProvider::onPlayerChat()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 11.6096

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 41
ccs 15
cts 22
cp 0.6818
rs 4.909
cc 9
eloc 22
nc 13
nop 4
crap 11.6096
1
<?php
2
3
namespace eXpansion\Framework\Core\DataProviders;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\ChatCommandInterface;
6
use eXpansion\Framework\Core\Exceptions\PlayerException;
7
use eXpansion\Framework\Core\Helpers\ChatOutput;
8
use eXpansion\Framework\Core\Model\ChatCommand\AbstractChatCommand;
9
use eXpansion\Framework\Core\Model\Helpers\ChatNotificationInterface;
10
use eXpansion\Framework\Core\Services\ChatCommands;
11
12
use /** @noinspection PhpUndefinedClassInspection */
13
    Symfony\Component\Console\Exception\RuntimeException;
14
15
/**
16
 * Class ChatCommandDataProvider, provides execution instructions for chat commands.
17
 *
18
 * @package eXpansion\Framework\Core\DataProviders
19
 */
20
class ChatCommandDataProvider extends AbstractDataProvider
21
{
22
    /** @var ChatCommands */
23
    protected $chatCommands;
24
25
    /** @var ChatNotificationInterface */
26
    protected $chatNotification;
27
28
    /** @var ChatOutput */
29
    protected $chatOutput;
30
31
    /**
32
     * ChatCommandDataProvider constructor.
33
     * @param ChatCommands $chatCommands
34
     * @param ChatNotificationInterface $chatNotification
35
     * @param ChatOutput $chatOutput
36 6
     */
37
    public function __construct(
38
        ChatCommands $chatCommands,
39
        ChatNotificationInterface $chatNotification,
40
        ChatOutput $chatOutput
41 6
    ) {
42 6
        $this->chatCommands = $chatCommands;
43 6
        $this->chatNotification = $chatNotification;
44 6
        $this->chatOutput = $chatOutput;
45
    }
46
47
    /**
48
     * @inheritdoc
49 1
     */
50
    public function registerPlugin($pluginId, $pluginService)
51 1
    {
52
        parent::registerPlugin($pluginId, $pluginService);
53
54 1
        /** @var ChatCommandInterface|object $pluginService */
55 1
        $this->chatCommands->registerPlugin($pluginId, $pluginService);
56
    }
57
58
    /**
59
     * @inheritdoc
60 1
     */
61
    public function deletePlugin($pluginId)
62 1
    {
63
        parent::deletePlugin($pluginId);
64 1
65 1
        $this->chatCommands->deletePlugin($pluginId);
66
    }
67
68
    /**
69
     * Called when a player chats on the server.
70
     *
71
     * @param int $playerUid
72
     * @param string $login
73
     * @param string $text
74
     * @param bool $isRegisteredCmd
75 4
     */
76
    public function onPlayerChat($playerUid, $login, $text, $isRegisteredCmd = false)
77
    {
78 4
        // disable for server
79
        if ($playerUid === 0) {
80
            return;
81
        }
82 4
83 2
        if (!$isRegisteredCmd) {
84
            return;
85
        }
86 3
87 3
        $text = substr($text, 1);
88
        $cmdAndArgs = explode(' ', $text);
89
90 3
        // Internal dedicated server commands to ignore.
91 1
        if ($cmdAndArgs[0] === 'version' || $cmdAndArgs[0] === 'serverlogin') {
92
            return;
93
        }
94 2
95
        $message = 'expansion_core.chat_commands.wrong_chat';
96 2
97
        list($command, $parameter) = $this->chatCommands->getChatCommand($cmdAndArgs);
98 2
        /** @var AbstractChatCommand $command */
99 1
        if ($command) {
100 1
            $parameter = implode(" ", $parameter);
101 1
            $message = $command->validate($login, $parameter);
102
            if (empty($message)) {
103 1
                try {
104 1
                    $this->chatOutput->setLogin($login);
105
                    $message = $command->run($login, $this->chatOutput, $parameter);
106
                } catch (PlayerException $e) {
107
                    // Player exceptions are meant to be sent to players, and not crash or even be logged.
108
                    $this->chatNotification->sendMessage($e->getTranslatableMessage(), $login);
109
                }
110
            }
111
        }
112
113
        if (!empty($message)) {
114
            $this->chatNotification->sendMessage($message, $login);
115
        }
116
    }
117
}
118