Completed
Push — dev ( 04be10...0e3c94 )
by
unknown
11:22
created

ChatCommandDataProvider::onPlayerChat()   D

Complexity

Conditions 9
Paths 17

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9.732

Importance

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