Completed
Pull Request — master (#33)
by Timothée
03:16
created

getHandlerForCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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