ChatCommandDataProvider::onPlayerChat()   B
last analyzed

Complexity

Conditions 10
Paths 17

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10.0578

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 22
cts 24
cp 0.9167
rs 7.3333
c 0
b 0
f 0
cc 10
nc 17
nop 4
crap 10.0578

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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