Completed
Pull Request — master (#20)
by
unknown
02:20
created

ContainerBasedHandlerLocator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2.0185
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