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

ChatCommands::addCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
cc 2
eloc 6
nc 2
nop 3
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Services;
4
5
use eXpansion\Framework\Core\Exceptions\ChatCommand\CommandExistException;
6
use eXpansion\Framework\Core\Model\ChatCommand\ChatCommandInterface;
7
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ChatCommandInterface as ChatCommandPluginInterface;
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 3
    public function __construct($depth)
32
    {
33 3
        $this->depth = $depth;
34 3
    }
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 2
    public function registerPlugin($pluginId, ChatCommandPluginInterface $pluginService)
46
    {
47 2
        $commands = $pluginService->getChatCommands();
48
49 2
        foreach ($commands as $command) {
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) {
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
            $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 1
    protected function findChatCommand($cmdAndArgs, $depth, $orig = null)
110
    {
111 1
        if ($depth == 0) {
112 1
            return [null, []];
113
        }
114
115 1
        if ($orig == null) {
116 1
            $orig = $cmdAndArgs;
117
        }
118
119 1
        $cmdAndArgs = array_splice($cmdAndArgs, 0, $depth);
120 1
        $parameters = array_diff($orig, $cmdAndArgs);
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, $orig);
126
    }
127
128
    /**
129
     * Register a command.
130
     *
131
     * @param string               $pluginId
132
     * @param string               $cmdTxt
133
     * @param ChatCommandInterface $command
134
     *
135
     * @throws CommandExistException
136
     */
137 2
    protected function addCommand($pluginId, $cmdTxt, ChatCommandInterface $command)
138
    {
139 2
        if (isset($this->commands[$cmdTxt])) {
140 1
            throw new CommandExistException(
141 1
                "$pluginId tries to register command '$cmdTxt' already registered"
142
            );
143
        }
144
145 2
        $this->commands[$cmdTxt] = $command;
146 2
        $this->commandPlugin[$pluginId][$cmdTxt] = $command;
147 2
    }
148
}
149