ChatCommandDataProvider   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.59%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 103
ccs 35
cts 37
cp 0.9459
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A registerPlugin() 0 7 1
A deletePlugin() 0 6 1
B onPlayerChat() 0 45 10
1
<?php
2
3
namespace eXpansion\Framework\GameManiaplanet\DataProviders;
4
5
use eXpansion\Framework\Core\DataProviders\AbstractDataProvider;
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
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ChatCommandInterface;
12
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
13
use Psr\Log\LoggerInterface;
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 9
    public function __construct(
38
        ChatCommands $chatCommands,
39
        ChatNotificationInterface $chatNotification,
40
        ChatOutput $chatOutput
41
42
    ) {
43 9
        $this->chatCommands = $chatCommands;
44 9
        $this->chatNotification = $chatNotification;
45 9
        $this->chatOutput = $chatOutput;
46 9
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 1
    public function registerPlugin($pluginId, $pluginService)
52
    {
53 1
        parent::registerPlugin($pluginId, $pluginService);
54
55
        /** @var ChatCommandInterface|object $pluginService */
56 1
        $this->chatCommands->registerPlugin($pluginId, $pluginService);
57 1
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 1
    public function deletePlugin($pluginId)
63
    {
64 1
        parent::deletePlugin($pluginId);
65
66 1
        $this->chatCommands->deletePlugin($pluginId);
67 1
    }
68
69
    /**
70
     * Called when a player chats on the server.
71
     *
72
     * @param int    $playerUid
73
     * @param string $login
74
     * @param string $text
75
     * @param bool   $isRegisteredCmd
76
     */
77 7
    public function onPlayerChat($playerUid, $login, $text, $isRegisteredCmd = false)
78
    {
79
        // disable for server
80 7
        if ($playerUid === 0) {
81
            return;
82
        }
83
84 7
        if (!$isRegisteredCmd) {
85 2
            return;
86
        }
87
88 6
        $text = substr($text, 1);
89 6
        $cmdAndArgs = explode(' ', $text);
90
91
        // Internal dedicated server commands to ignore.
92 6
        if ($cmdAndArgs[0] === 'version' || $cmdAndArgs[0] === 'serverlogin') {
93 1
            return;
94
        }
95
96 5
        $message = 'expansion_core.chat_commands.wrong_chat';
97
98 5
        list($command, $parameter) = $this->chatCommands->getChatCommand($cmdAndArgs);
99
100
        /** @var AbstractChatCommand $command */
101 5
        if ($command) {
102 4
            $parameter = implode(" ", $parameter);
103 4
            $message = $command->validate($login, $parameter);
104 4
            if (empty($message)) {
105
                try {
106 4
                    $this->chatOutput->setLogin($login);
107 4
                    $message = $command->run($login, $this->chatOutput, $parameter);
108 2
                } catch (PlayerException $e) {
109
                    // Player exceptions are meant to be sent to players, and not crash or even be logged.
110 1
                    $this->chatNotification->sendMessage($e->getTranslatableMessage(), $login);
111 1
                } catch (UnknownPlayerException $e) {
112
                    // Player exceptions are meant to be sent to players, and not crash or even be logged.
113
                    $this->chatNotification->sendMessage($e->getMessage(), $login);
114
                }
115
            }
116
        }
117
118 4
        if (!empty($message)) {
119 1
            $this->chatNotification->sendMessage($message, $login);
120
        }
121 4
    }
122
}
123