NativeMapResolver::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Depot\Resolution;
4
5
use Depot\Command;
6
use Depot\Container;
7
use Depot\Container\NativeContainer;
8
use Depot\Exception\UnresolvableHandlerException;
9
use Depot\HandlerResolver;
10
11
final class NativeMapResolver implements HandlerResolver
12
{
13
    /**
14
     * @var string[]
15
     */
16
    private $commands;
17
    /**
18
     * @var Container
19
     */
20
    private $container;
21
22
    /**
23
     * NativeMapResolver constructor
24
     * @param string[] $commands
25
     * @param Container $container
26
     */
27
    public function __construct(array $commands, Container $container = null)
28
    {
29
        $this->commands = $commands;
30
        $this->container = $container ?: new NativeContainer;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function resolve(Command $command)
37
    {
38
        if (array_key_exists($command = get_class($command), $this->commands)) {
39
            return $this->container->make($this->commands[$command]);
40
        }
41
42
        throw new UnresolvableHandlerException("Unable to resolve handler for $command");
43
    }
44
}
45