Completed
Push — dev ( 845ef5...8eacd8 )
by
unknown
02:50
created

ChatCommands::getChatCommands()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 6
1
<?php
2
3
namespace eXpansion\Framework\Core\Services;
4
5
use eXpansion\Framework\Core\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
     * @param int $depth
29
     */
30 4
    public function __construct($depth)
31
    {
32 4
        $this->depth = $depth;
33 4
    }
34
35
36
    /**
37
     * Register chat commands of a plugin.
38
     *
39
     * @param string $pluginId
40
     * @param ChatCommandPluginInterface $pluginService
41
     *
42
     * @throws CommandExistException
43
     */
44 2
    public function registerPlugin($pluginId, ChatCommandPluginInterface $pluginService)
45
    {
46 2
        $commands = $pluginService->getChatCommands();
47
48 2
        foreach ($commands as $command)
49
        {
50 2
            $this->addCommand($pluginId, $command->getCommand(), $command);
51
52 2
            foreach ($command->getAliases() as $alias) {
53 2
                $this->addCommand($pluginId, $alias, $command);
54
            }
55
        }
56 2
    }
57
58
    /**
59
     * Remove all chat commands registered for a plugin.
60
     *
61
     * @param $pluginId
62
     */
63 2
    public function deletePlugin($pluginId)
64
    {
65 2
        if (!isset($this->commandPlugin[$pluginId])) {
66 1
            return;
67
        }
68
69 1
        foreach ($this->commandPlugin[$pluginId] as $cmdTxt => $command) {
0 ignored issues
show
Bug introduced by
The expression $this->commandPlugin[$pluginId] of type object<eXpansion\Framewo...d\ChatCommandInterface> is not traversable.
Loading history...
70 1
            unset($this->commands[$cmdTxt]);
71
        }
72 1
        unset($this->commandPlugin[$pluginId]);
73 1
    }
74
75
    /**
76
     * Get a chat command.
77
     *
78
     * @param string[] $cmdAndArgs
79
     * @return array
80
     */
81 1
    public function getChatCommand($cmdAndArgs)
82
    {
83 1
        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
        {
96
            $chatCommands[] = [
97
                'command' => $chatCommand,
98
                'description' => $data->getDescription(),
99
                'help' => $data->getHelp(),
100
                'aliases' => $data->getAliases()
101
            ];
102
        }
103
104
        return $chatCommands;
105
    }
106
107
    /**
108
     * @param string[] $cmdAndArgs
109
     * @param integer $depth
110
     *
111
     * @return mixed
112
     */
113 1
    protected function findChatCommand($cmdAndArgs, $depth)
114
    {
115 1
        if ($depth == 0) {
116 1
            return [null, []];
117
        }
118
119 1
        $parameters = array_splice($cmdAndArgs, $depth - 1, 100);
120 1
        $cmdAndArgs = array_splice($cmdAndArgs, 0, $depth);
121 1
        $command = implode(' ', $cmdAndArgs);
122
123 1
        return isset($this->commands[$command])
124 1
            ? [$this->commands[$command], $parameters]
125 1
            : $this->findChatCommand($cmdAndArgs, $depth - 1);
126
    }
127
128
    /**
129
     *
130
     * @param string $pluginId
131
     * @param string $cmdTxt
132
     * @param ChatCommandInterface $command
133
     *
134
     * @throws CommandExistException
135
     */
136 2
    protected function addCommand($pluginId, $cmdTxt, ChatCommandInterface $command)
137
    {
138 2
        if (isset($this->commands[$cmdTxt])) {
139 1
            throw new CommandExistException(
140 1
                "$pluginId tries to register command '$cmdTxt' already registered"
141
            );
142
        }
143
144 2
        $this->commands[$cmdTxt] = $command;
145 2
        $this->commandPlugin[$pluginId][$cmdTxt] = $command;
146
    }
147
}