Completed
Push — master ( e4b07c...c935ac )
by De Cramer
10:27 queued 07:51
created

ChatCommandDataProvider::onPlayerChat()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 9.0076

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 21
cts 22
cp 0.9545
rs 4.909
c 0
b 0
f 0
cc 9
eloc 22
nc 13
nop 4
crap 9.0076
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
     */
37 8
    public function __construct(
38
        ChatCommands $chatCommands,
39
        ChatNotificationInterface $chatNotification,
40
        ChatOutput $chatOutput
41
    ) {
42 8
        $this->chatCommands = $chatCommands;
43 8
        $this->chatNotification = $chatNotification;
44 8
        $this->chatOutput = $chatOutput;
45 8
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 1
    public function registerPlugin($pluginId, $pluginService)
51
    {
52 1
        parent::registerPlugin($pluginId, $pluginService);
53
54
        /** @var ChatCommandInterface|object $pluginService */
55 1
        $this->chatCommands->registerPlugin($pluginId, $pluginService);
56 1
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 1
    public function deletePlugin($pluginId)
62
    {
63 1
        parent::deletePlugin($pluginId);
64
65 1
        $this->chatCommands->deletePlugin($pluginId);
66 1
    }
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
     */
76 6
    public function onPlayerChat($playerUid, $login, $text, $isRegisteredCmd = false)
77
    {
78
        // disable for server
79 6
        if ($playerUid === 0) {
80
            return;
81
        }
82
83 6
        if (!$isRegisteredCmd) {
84 2
            return;
85
        }
86
87 5
        $text = substr($text, 1);
88 5
        $cmdAndArgs = explode(' ', $text);
89
90
        // Internal dedicated server commands to ignore.
91 5
        if ($cmdAndArgs[0] === 'version' || $cmdAndArgs[0] === 'serverlogin') {
92 1
            return;
93
        }
94
95 4
        $message = 'expansion_core.chat_commands.wrong_chat';
96
97 4
        list($command, $parameter) = $this->chatCommands->getChatCommand($cmdAndArgs);
98
        /** @var AbstractChatCommand $command */
99 4
        if ($command) {
100 3
            $parameter = implode(" ", $parameter);
101 3
            $message = $command->validate($login, $parameter);
102 3
            if (empty($message)) {
103
                try {
104 3
                    $this->chatOutput->setLogin($login);
105 3
                    $message = $command->run($login, $this->chatOutput, $parameter);
106 2
                } catch (PlayerException $e) {
107
                    // Player exceptions are meant to be sent to players, and not crash or even be logged.
108 1
                    $this->chatNotification->sendMessage($e->getTranslatableMessage(), $login);
109
                }
110
            }
111
        }
112
113 3
        if (!empty($message)) {
114 1
            $this->chatNotification->sendMessage($message, $login);
115
        }
116 3
    }
117
}
118