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

ContainerBasedHandlerLocator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 41
ccs 9
cts 10
cp 0.9
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
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 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