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

ContainerBasedHandlerLocator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 37
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getHandlerForCommand() 0 8 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