Registry::findCommand()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the LineMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LineMob\Core;
13
14
use LineMob\Core\Command\AbstractCommand;
15
16
/**
17
 * @author Ishmael Doss <[email protected]>
18
 */
19
class Registry implements RegistryInterface
20
{
21
    /**
22
     * @var array
23
     */
24
    private $commands = [];
25
26
    /**
27
     * @var string
28
     */
29
    private $defaultCommand;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function add($commandClass, CommandHandlerInterface $handler, $default = false)
35
    {
36
        $this->commands[$commandClass] = $handler;
37
38
        if ($default) {
39
            $this->defaultCommand = $commandClass;
40
        }
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getCommandList()
47
    {
48
        return $this->commands;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function findCommand(Input $input)
55
    {
56
        foreach (array_keys($this->commands) as $command) {
57
            /** @var AbstractCommand $cmd */
58
            $cmd = new $command();
59
60
            if ($cmd->supported($input->text)) {
61
                return $cmd;
62
            }
63
        }
64
65
        $cmd = new $this->defaultCommand();
66
67
        return $cmd;
68
    }
69
}
70