Completed
Push — master ( 413e31...6028d7 )
by Changwan
06:35
created

ArrayAccessLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A middleware() 0 7 2
A execute() 0 11 4
1
<?php
2
namespace Wandu\Router\Loader;
3
4
use ArrayAccess;
5
use Psr\Http\Message\ServerRequestInterface;
6
use Wandu\Router\Contracts\LoaderInterface;
7
use Wandu\Router\Contracts\MiddlewareInterface;
8
use Wandu\Router\Exception\HandlerNotFoundException;
9
10
class ArrayAccessLoader implements LoaderInterface
11
{
12
    /** @var \ArrayAccess */
13
    protected $container;
14
15
    /**
16
     * @param \ArrayAccess $container
17
     */
18
    public function __construct(ArrayAccess $container)
19
    {
20
        $this->container = $container;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function middleware($className): MiddlewareInterface
27
    {
28
        if (!isset($this->container[$className])) {
29
            throw new HandlerNotFoundException($className);
30
        }
31
        return $this->container[$className];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function execute($className, $methodName, ServerRequestInterface $request)
38
    {
39
        if (!isset($this->container[$className])) {
40
            throw new HandlerNotFoundException($className, $methodName);
41
        }
42
        $object = $this->container[$className];
43
        if (!method_exists($object, $methodName) && !method_exists($object, '__call')) {
44
            throw new HandlerNotFoundException($className, $methodName);
45
        }
46
        return call_user_func([$object, $methodName], $request);
47
    }
48
}
49