Completed
Branch master (220ce5)
by De Cramer
16:11
created

ChatCommandDataProvider::onPlayerChat()   D

Complexity

Conditions 10
Paths 17

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 10.3248

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 23
cts 27
cp 0.8519
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 24
nc 17
nop 4
crap 10.3248

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