CommandHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 24
ccs 8
cts 9
cp 0.8889
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerCommand() 0 4 1
A hasCommand() 0 4 1
A execute() 0 8 2
1
<?php declare(strict_types=1);
2
3
namespace Bigwhoop\Trumpet\Commands;
4
5
final class CommandHandler
6
{
7
    /** @var Command[] */
8
    private $commands = [];
9
10 1
    public function registerCommand(Command $command)
11
    {
12 1
        $this->commands[$command->getToken()] = $command;
13 1
    }
14
    
15 1
    public function hasCommand(string $commandName): bool
16
    {
17 1
        return array_key_exists($commandName, $this->commands);
18
    }
19
    
20 1
    public function execute(string $commandName, CommandParams $params, CommandExecutionContext $executionContext): string
21
    {
22 1
        if (!array_key_exists($commandName, $this->commands)) {
23
            throw new UnknownCommandException("Command '$commandName' must be registered before it can be used.");
24
        }
25
26 1
        return $this->commands[$commandName]->execute($params, $executionContext);
27
    }
28
}
29