Completed
Pull Request — master (#148)
by De Cramer
02:37
created

ChatCommandDataProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
3
namespace eXpansion\Framework\GameManiaplanet\DataProviders;
4
5
use eXpansion\Framework\Core\DataProviders\AbstractDataProvider;
6
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ChatCommandInterface;
7
use eXpansion\Framework\Core\Exceptions\PlayerException;
8
use eXpansion\Framework\Core\Helpers\ChatOutput;
9
use eXpansion\Framework\Core\Model\ChatCommand\AbstractChatCommand;
10
use eXpansion\Framework\Core\Model\Helpers\ChatNotificationInterface;
11
use eXpansion\Framework\Core\Services\ChatCommands;
12
13
use /** @noinspection PhpUndefinedClassInspection */
14
    Symfony\Component\Console\Exception\RuntimeException;
15
16
/**
17
 * Class ChatCommandDataProvider, provides execution instructions for chat commands.
18
 *
19
 * @package eXpansion\Framework\Core\DataProviders
20
 */
21
class ChatCommandDataProvider extends AbstractDataProvider
22
{
23
    /** @var ChatCommands */
24
    protected $chatCommands;
25
26
    /** @var ChatNotificationInterface */
27
    protected $chatNotification;
28
29
    /** @var ChatOutput */
30
    protected $chatOutput;
31
32
    /**
33
     * ChatCommandDataProvider constructor.
34
     * @param ChatCommands $chatCommands
35
     * @param ChatNotificationInterface $chatNotification
36
     * @param ChatOutput $chatOutput
37
     */
38 8
    public function __construct(
39
        ChatCommands $chatCommands,
40
        ChatNotificationInterface $chatNotification,
41
        ChatOutput $chatOutput
42
    ) {
43 8
        $this->chatCommands = $chatCommands;
44 8
        $this->chatNotification = $chatNotification;
45 8
        $this->chatOutput = $chatOutput;
46 8
    }
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 6
    public function onPlayerChat($playerUid, $login, $text, $isRegisteredCmd = false)
78
    {
79
        // disable for server
80 6
        if ($playerUid === 0) {
81
            return;
82
        }
83
84 6
        if (!$isRegisteredCmd) {
85 2
            return;
86
        }
87
88 5
        $text = substr($text, 1);
89 5
        $cmdAndArgs = explode(' ', $text);
90
91
        // Internal dedicated server commands to ignore.
92 5
        if ($cmdAndArgs[0] === 'version' || $cmdAndArgs[0] === 'serverlogin') {
93 1
            return;
94
        }
95
96 4
        $message = 'expansion_core.chat_commands.wrong_chat';
97
98 4
        list($command, $parameter) = $this->chatCommands->getChatCommand($cmdAndArgs);
99
        /** @var AbstractChatCommand $command */
100 4
        if ($command) {
101 3
            $parameter = implode(" ", $parameter);
102 3
            $message = $command->validate($login, $parameter);
103 3
            if (empty($message)) {
104
                try {
105 3
                    $this->chatOutput->setLogin($login);
106 3
                    $message = $command->run($login, $this->chatOutput, $parameter);
107 2
                } catch (PlayerException $e) {
108
                    // Player exceptions are meant to be sent to players, and not crash or even be logged.
109 1
                    $this->chatNotification->sendMessage($e->getTranslatableMessage(), $login);
110
                }
111
            }
112
        }
113
114 3
        if (!empty($message)) {
115 1
            $this->chatNotification->sendMessage($message, $login);
116
        }
117 3
    }
118
}
119