Completed
Pull Request — master (#194)
by De Cramer
24:15 queued 02:45
created

ChatCommandDataProvider::deletePlugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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 Psr\Log\LoggerInterface;
13
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
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
     * @var LoggerInterface
31
     */
32
    private $logger;
33
34
    /**
35
     * ChatCommandDataProvider constructor.
36
     * @param ChatCommands              $chatCommands
37
     * @param ChatNotificationInterface $chatNotification
38
     * @param ChatOutput                $chatOutput
39
     * @param LoggerInterface           $logger
40
     */
41
    public function __construct(
42
        ChatCommands $chatCommands,
43
        ChatNotificationInterface $chatNotification,
44
        ChatOutput $chatOutput,
45
        LoggerInterface $logger
46
47
    ) {
48
        $this->chatCommands = $chatCommands;
49
        $this->chatNotification = $chatNotification;
50
        $this->chatOutput = $chatOutput;
51
        $this->logger = $logger;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function registerPlugin($pluginId, $pluginService)
58
    {
59
        parent::registerPlugin($pluginId, $pluginService);
60
61
        /** @var ChatCommandInterface|object $pluginService */
62
        $this->chatCommands->registerPlugin($pluginId, $pluginService);
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function deletePlugin($pluginId)
69
    {
70
        parent::deletePlugin($pluginId);
71
72
        $this->chatCommands->deletePlugin($pluginId);
73
    }
74
75
    /**
76
     * Called when a player chats on the server.
77
     *
78
     * @param int    $playerUid
79
     * @param string $login
80
     * @param string $text
81
     * @param bool   $isRegisteredCmd
82
     */
83
    public function onPlayerChat($playerUid, $login, $text, $isRegisteredCmd = false)
84
    {
85
        // disable for server
86
        if ($playerUid === 0) {
87
            return;
88
        }
89
90
        if (!$isRegisteredCmd) {
91
            return;
92
        }
93
94
        $text = substr($text, 1);
95
        $cmdAndArgs = explode(' ', $text);
96
97
        // Internal dedicated server commands to ignore.
98
        if ($cmdAndArgs[0] === 'version' || $cmdAndArgs[0] === 'serverlogin') {
99
            return;
100
        }
101
102
        $message = 'expansion_core.chat_commands.wrong_chat';
103
104
        list($command, $parameter) = $this->chatCommands->getChatCommand($cmdAndArgs);
105
106
        /** @var AbstractChatCommand $command */
107
        if ($command) {
108
            $parameter = implode(" ", $parameter);
109
            $message = $command->validate($login, $parameter);
110
            if (empty($message)) {
111
                try {
112
                    $this->chatOutput->setLogin($login);
113
                    $message = $command->run($login, $this->chatOutput, $parameter);
114
                } catch (PlayerException $e) {
115
                    // Player exceptions are meant to be sent to players, and not crash or even be logged.
116
                    $this->chatNotification->sendMessage($e->getTranslatableMessage(), $login);
117
                } catch( UnknownPlayerException $e) {
118
                    // Player exceptions are meant to be sent to players, and not crash or even be logged.
119
                    $this->chatNotification->sendMessage($e->getMessage(), $login);
120
                }
121
            }
122
        }
123
124
        if (!empty($message)) {
125
            $this->chatNotification->sendMessage($message, $login);
126
        }
127
    }
128
}
129