CommandHandler::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
crap 2.0625
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