NativeNamespaceResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A resolve() 0 23 4
1
<?php
2
3
namespace Depot\Resolution;
4
5
use Depot\Command;
6
use Depot\Container;
7
use Depot\Container\NativeContainer;
8
use Depot\HandlerResolver;
9
use Depot\Exception\UnresolvableHandlerException;
10
11
final class NativeNamespaceResolver implements HandlerResolver
12
{
13
    /**
14
     * @var NativeContainer
15
     */
16
    private $container;
17
18
    /**
19
     * NativeNamespaceResolver constructor
20
     * @param Container|null $container
21
     */
22
    public function __construct(Container $container = null)
23
    {
24
        $this->container = $container ?: new NativeContainer;
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function resolve(Command $command)
31
    {
32
        $command = get_class($command);
33
        $name = substr($command, strrpos($command, '\\') + 1);
34
        $namespace = substr($command, 0, strrpos($command, '\\'));
35
36
        $handler = "{$command}Handler";
37
        if (class_exists($handler)) {
38
            return $this->container->make($handler);
39
        }
40
41
        $handler = "$namespace\\Handlers\\{$name}Handler";
42
        if (class_exists($handler)) {
43
            return $this->container->make($handler);
44
        }
45
46
        $handler = "$namespace\\Handler\\{$name}Handler";
47
        if (class_exists($handler)) {
48
            return $this->container->make($handler);
49
        }
50
51
        throw new UnresolvableHandlerException("Unable to resolve handler for $command");
52
    }
53
}
54