ContainerBasedHandlerLocator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace League\Tactician\Bundle\Handler;
4
5
use League\Tactician\Exception\MissingHandlerException;
6
use League\Tactician\Handler\Locator\HandlerLocator;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
/**
10
 * Lazily loads Command Handlers from the Symfony DI container
11
 */
12
class ContainerBasedHandlerLocator implements HandlerLocator
13
{
14
    /**
15
     * @var ContainerInterface
16
     */
17
    private $container;
18
19
    /**
20
     * @var array
21
     */
22
    private $commandToServiceId = [];
23
24
    /**
25
     * @param ContainerInterface $container
26
     * @param array $commandToServiceIdMapping
27
     */
28 6
    public function __construct(ContainerInterface $container, array $commandToServiceIdMapping)
29
    {
30 6
        $this->container = $container;
31 6
        $this->commandToServiceId = $commandToServiceIdMapping;
32 6
    }
33
34
    /**
35
     * Retrieves the handler for a specified command
36
     *
37
     * @param string $commandName
38
     * @return object
39
     *
40
     * @throws MissingHandlerException
41
     */
42 6
    public function getHandlerForCommand($commandName)
43
    {
44 6
        if (!isset($this->commandToServiceId[$commandName])) {
45 3
            throw MissingHandlerException::forCommand($commandName);
46
        }
47
48 3
        return $this->container->get($this->commandToServiceId[$commandName]);
49
    }
50
}
51