Completed
Pull Request — master (#161)
by De Cramer
02:59
created

ChatCommands   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 130
rs 10
c 0
b 0
f 0
ccs 0
cts 40
cp 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getChatCommands() 0 9 2
A __construct() 0 4 1
A getChatCommand() 0 4 1
A findChatCommand() 0 14 3
A registerPlugin() 0 12 3
A deletePlugin() 0 11 3
A addCommand() 0 11 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Services;
4
5
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ChatCommandInterface as ChatCommandPluginInterface;
6
use eXpansion\Framework\Core\Exceptions\ChatCommand\CommandExistException;
7
use eXpansion\Framework\Core\Model\ChatCommand\ChatCommandInterface;
8
9
/**
10
 * Class ChatCommands store all currently active chat commands.
11
 *
12
 * @package eXpansion\Framework\Core\Services;
13
 * @author oliver de Cramer <[email protected]>
14
 */
15
class ChatCommands
16
{
17
    /** @var ChatCommandInterface[] */
18
    protected $commands = array();
19
20
    /** @var ChatCommandInterface[][] */
21
    protected $commandPlugin;
22
23
    /** @var int */
24
    protected $depth = 3;
25
26
    /**
27
     * ChatCommands constructor.
28
     *
29
     * @param int $depth
30
     */
31
    public function __construct($depth)
32
    {
33
        $this->depth = $depth;
34
    }
35
36
37
    /**
38
     * Registers all chat commands of a plugin.
39
     *
40
     * @param string $pluginId
41
     * @param ChatCommandPluginInterface $pluginService
42
     *
43
     * @throws CommandExistException
44
     */
45
    public function registerPlugin($pluginId, ChatCommandPluginInterface $pluginService)
46
    {
47
        $commands = $pluginService->getChatCommands();
48
49
        foreach ($commands as $command) {
50
            $this->addCommand($pluginId, $command->getCommand(), $command);
51
52
            foreach ($command->getAliases() as $alias) {
53
                $this->addCommand($pluginId, $alias, $command);
54
            }
55
        }
56
    }
57
58
    /**
59
     * Remove all chat commands registered for a plugin.
60
     *
61
     * @param $pluginId
62
     */
63
    public function deletePlugin($pluginId)
64
    {
65
        if (!isset($this->commandPlugin[$pluginId])) {
66
            return;
67
        }
68
69
        foreach ($this->commandPlugin[$pluginId] as $cmdTxt => $command) {
70
            unset($this->commands[$cmdTxt]);
71
        }
72
        unset($this->commandPlugin[$pluginId]);
73
    }
74
75
    /**
76
     * Get a chat command.
77
     *
78
     * @param string[] $cmdAndArgs
79
     * @return array
80
     */
81
    public function getChatCommand($cmdAndArgs)
82
    {
83
        return $this->findChatCommand($cmdAndArgs, $this->depth);
84
    }
85
86
    /**
87
     * Get list of all chat commands.
88
     *
89
     * return array
90
     */
91
    public function getChatCommands()
92
    {
93
        $chatCommands = [];
94
        foreach ($this->commands as $chatCommand => $data) {
95
            $chatCommands[$data->getCommand()] = clone $data;
96
        }
97
98
        return $chatCommands;
99
    }
100
101
    /**
102
     * Find a chat command.
103
     *
104
     * @param string[] $cmdAndArgs
105
     * @param integer $depth
106
     *
107
     * @return mixed
108
     */
109
    protected function findChatCommand($cmdAndArgs, $depth)
110
    {
111
        if ($depth == 0) {
112
            return [null, []];
113
        }
114
115
        $parameters = array_splice($cmdAndArgs, $depth - 1, 100);
116
        $cmdAndArgs = array_splice($cmdAndArgs, 0, $depth);
117
        $command = implode(' ', $cmdAndArgs);
118
119
        return isset($this->commands[$command])
120
            ? [$this->commands[$command], $parameters]
121
            : $this->findChatCommand($cmdAndArgs, $depth - 1);
122
    }
123
124
    /**
125
     * Register a command.
126
     *
127
     * @param string $pluginId
128
     * @param string $cmdTxt
129
     * @param ChatCommandInterface $command
130
     *
131
     * @throws CommandExistException
132
     */
133
    protected function addCommand($pluginId, $cmdTxt, ChatCommandInterface $command)
134
    {
135
        if (isset($this->commands[$cmdTxt])) {
136
            throw new CommandExistException(
137
                "$pluginId tries to register command '$cmdTxt' already registered"
138
            );
139
        }
140
141
        $this->commands[$cmdTxt] = $command;
142
        $this->commandPlugin[$pluginId][$cmdTxt] = $command;
143
    }
144
}
145