ContainerBasedHandlerLocator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
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 39
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
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