Completed
Pull Request — master (#20)
by
unknown
02:09
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
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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 6
    public function __construct(ContainerInterface $container, $commandToServiceIdMapping)
28
    {
29 6
        if (empty($commandToServiceIdMapping)) {
30
            throw new \InvalidArgumentException('commandToServiceIdMapping cannot be empty');
31
        }
32
33 6
        $this->container = $container;
34 6
        $this->commandToServiceId = $commandToServiceIdMapping;
35 6
    }
36
37
    /**
38
     * Retrieves the handler for a specified command
39
     *
40
     * @param string $commandName
41
     * @return mixed
42
     */
43 6
    public function getHandlerForCommand($commandName)
44
    {
45 6
        if (!isset($this->commandToServiceId[$commandName])) {
46 3
            throw MissingHandlerException::forCommand($commandName);
47
        }
48
49 3
        return $this->container->get($this->commandToServiceId[$commandName]);
50
    }
51
}
52