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

ArrayAccessLoader::execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 3
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
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