Completed
Pull Request — master (#111)
by Robin
02:26
created

ContainerLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace League\Tactician\Handler\Locator;
4
5
use League\Tactician\Exception\MissingHandlerException;
6
use Psr\Container\ContainerInterface;
7
8
/**
9
 * Lazily loads Command Handlers from a PSR-11 container.
10
 */
11
class ContainerLocator implements HandlerLocator
12
{
13
    /**
14
     * @var ContainerInterface
15
     */
16
    private $container;
17
18
    /**
19
     * @param ContainerInterface $container A PSR-11 container holding command handlers
20
     *                                      mapped by command names
21
     */
22 2
    public function __construct(ContainerInterface $container)
23
    {
24 2
        $this->container = $container;
25 2
    }
26
27
    /**
28
     * Gets the handler for a specified command.
29
     *
30
     * @param string $commandName
31
     *
32
     * @return object The command handler service
33
     *
34
     * @throws MissingHandlerException
35
     */
36 2
    public function getHandlerForCommand($commandName)
37
    {
38 2
        if (!$this->container->has($commandName)) {
39 1
            throw MissingHandlerException::forCommand($commandName);
40
        }
41
42 1
        return $this->container->get($commandName);
43
    }
44
}
45