DirectBindingCommandHandlerLocator::findHandler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2011, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Component\Command\Handler\Locator;
13
14
use GpsLab\Component\Command\Command;
15
use GpsLab\Component\Command\Handler\CommandSubscriber;
16
17
class DirectBindingCommandHandlerLocator implements CommandHandlerLocator
18
{
19
    /**
20
     * @var callable[]
21
     */
22
    private $handlers = [];
23
24
    /**
25
     * Bind command handler to concrete command by class name.
26
     *
27
     * @param string   $command_name
28
     * @param callable $handler
29
     */
30 3
    public function registerHandler(string $command_name, callable $handler): void
31
    {
32 3
        $this->handlers[$command_name] = $handler;
33 3
    }
34
35
    /**
36
     * @param CommandSubscriber $subscriber
37
     */
38 1
    public function registerSubscriber(CommandSubscriber $subscriber): void
39
    {
40 1
        foreach ($subscriber::getSubscribedCommands() as $command_name => $method) {
41 1
            $handler = [$subscriber, $method];
42 1
            if (is_callable($handler)) {
43 1
                $this->registerHandler($command_name, $handler);
44
            }
45
        }
46 1
    }
47
48
    /**
49
     * @param Command $command
50
     *
51
     * @return callable|null
52
     */
53 3
    public function findHandler(Command $command): ?callable
54
    {
55 3
        return $this->handlers[get_class($command)] ?? null;
56
    }
57
}
58