HandlerLocator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Buttress\Concrete\CommandBus\Command;
4
5
use League\Tactician\Exception\MissingHandlerException;
6
use Psr\Container\ContainerInterface;
7
8
/**
9
 * Custom handler locator that pulls handlers from the container
10
 */
11
class HandlerLocator implements \League\Tactician\Handler\Locator\HandlerLocator
12
{
13
14
    protected $container;
15
16
    protected $handlers = [];
17
18 9
    public function __construct(ContainerInterface $container)
19
    {
20 9
        $this->container = $container;
21 9
    }
22
23
    /**
24
     * Retrieves the handler for a specified command
25
     *
26
     * @param string $commandName
27
     * @return object
28
     * @throws MissingHandlerException
29
     */
30 9
    public function getHandlerForCommand($commandName)
31
    {
32 9
        if (isset($this->handlers[$commandName])) {
33 6
            $handler = $this->handlers[$commandName];
34 6
            if ($this->container->has($handler)) {
35 3
                return $this->container->get($handler);
36
            }
37 2
        }
38
39 6
        throw new MissingHandlerException(sprintf('No handler found for "%s"', $commandName));
40
    }
41
42
    /**
43
     * Add a handler for a command
44
     *
45
     * @param string $command
46
     * @param string $handler
47
     */
48 6
    public function pushHandler($command, $handler)
49
    {
50 6
        $this->handlers[$command] = $handler;
51 6
    }
52
}
53