Completed
Push — master ( 1f1d6f...d17677 )
by De Cramer
9s
created

ChatCommands   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 78
c 0
b 0
f 0
rs 10
ccs 23
cts 23
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerPlugin() 0 13 3
A deletePlugin() 0 11 3
A getChatCommand() 0 4 2
A addCommand() 0 11 2
1
<?php
2
3
namespace eXpansion\Core\Services;
4
5
use eXpansion\Core\DataProviders\Listener\ChatCommandInterface as ChatCommandPluginInterface;
6
use eXpansion\Core\Exceptions\ChatCommand\CommandExistException;
7
use eXpansion\Core\Model\ChatCommand\ChatCommandInterface;
8
9
/**
10
 * Class ChatCommands
11
 *
12
 * @package eXpansion\Core\Services;
13
 * @author oliver de Cramer <[email protected]>
14
 */
15
class ChatCommands
16
{
17
    /** @var ChatCommandInterface[] */
18
    protected $commands;
19
20
    /** @var ChatCommandInterface[] */
21
    protected $commandPlugin;
22
23
    /**
24
     * Register chat commands of a plugin.
25
     *
26
     * @param string $pluginId
27
     * @param ChatCommandPluginInterface $pluginService
28
     *
29
     * @throws CommandExistException
30
     */
31 2
    public function registerPlugin($pluginId, ChatCommandPluginInterface $pluginService)
32
    {
33 2
        $commands = $pluginService->getChatCommands();
34
35 2
        foreach($commands as $command)
36
        {
37 2
            $this->addCommand($pluginId, $command->getCommand(), $command);
38
39 2
            foreach ($command->getAliases() as $alias) {
40 2
                $this->addCommand($pluginId, $alias, $command);
41
            }
42
        }
43 2
    }
44
45
    /**
46
     * Remove all chat commands registered for a plugin.
47
     *
48
     * @param $pluginId
49
     */
50 2
    public function deletePlugin($pluginId)
51
    {
52 2
        if(!isset($this->commandPlugin[$pluginId])) {
53 1
            return;
54
        }
55
56 1
        foreach ($this->commandPlugin[$pluginId] as $cmdTxt => $command) {
0 ignored issues
show
Bug introduced by
The expression $this->commandPlugin[$pluginId] of type object<eXpansion\Core\Mo...d\ChatCommandInterface> is not traversable.
Loading history...
57 1
            unset($this->commands[$cmdTxt]);
58
        }
59 1
        unset($this->commandPlugin[$pluginId]);
60 1
    }
61
62
    /**
63
     * Get a chat command.
64
     *
65
     * @param $command
66
     * @return ChatCommandInterface|null
67
     */
68 1
    public function getChatCommand($command)
69
    {
70 1
        return isset($this->commands[$command]) ? $this->commands[$command] : null;
71
    }
72
73
    /**
74
     *
75
     * @param $pluginId
76
     * @param $cmdTxt
77
     * @param ChatCommandInterface $command
78
     *
79
     * @throws CommandExistException
80
     */
81 2
    protected function addCommand($pluginId, $cmdTxt, ChatCommandInterface $command)
82
    {
83 2
        if (isset($this->commands[$cmdTxt])) {
84 1
            throw new CommandExistException(
85 1
                "$pluginId tries to register command '$cmdTxt' already registered"
86
            );
87
        }
88
89 2
        $this->commands[$cmdTxt] = $command;
90 2
        $this->commandPlugin[$pluginId][$cmdTxt]  = $command;
91
    }
92
}