Completed
Push — dev ( 214409...8e757d )
by
unknown
03:22
created

ChatCommandDataProvider::onPlayerChat()   C

Complexity

Conditions 10
Paths 18

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 11.2294

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 48
rs 5.3454
ccs 20
cts 26
cp 0.7692
cc 10
eloc 26
nc 18
nop 4
crap 11.2294

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 ChatCommands */
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $chatNotification of type object<eXpansion\Framewo...tNotificationInterface> is incompatible with the declared type object<eXpansion\Framewo...\Services\ChatCommands> of property $chatNotification.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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 command to ignore.
90 3
        if ($cmdAndArgs[0] === 'version') {
91 1
            return;
92
        }
93
94
        // Internal dedicated server command to ignore.
95 2
        if ($cmdAndArgs[0] === 'serverlogin') {
96
            return;
97
        }
98
99 2
        $message = 'expansion_core.chat_commands.wrong_chat';
100
101 2
        list($command, $parameter) = $this->chatCommands->getChatCommand($cmdAndArgs);
102
        /** @var AbstractChatCommand $command */
103 2
        if ($command) {
104 1
            $parameter = implode(" ", $parameter);
105 1
            $message = $command->validate($login, $parameter);
106 1
            if (empty($message)) {
107
                try {
108 1
                    $this->chatOutput->setLogin($login);
109 1
                    $message = $command->run($login, $this->chatOutput, $parameter);
110
                } /** @noinspection PhpUndefinedClassInspection */
111
                catch (RuntimeException $e) {
112
                    $this->chatNotification->sendMessage($e->getMessage(), $login);
0 ignored issues
show
Bug introduced by
The method sendMessage() does not seem to exist on object<eXpansion\Framewo...\Services\ChatCommands>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
                } catch (\Exception $e) {
114
                    $this->chatNotification->sendMessage($e->getMessage(), $login);
0 ignored issues
show
Bug introduced by
The method sendMessage() does not seem to exist on object<eXpansion\Framewo...\Services\ChatCommands>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
                }
116
            }
117
        }
118
119 2
        if (!empty($message)) {
120 1
            $this->chatNotification->sendMessage($message, $login);
0 ignored issues
show
Bug introduced by
The method sendMessage() does not seem to exist on object<eXpansion\Framewo...\Services\ChatCommands>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
        }
122 2
    }
123
}
124