Completed
Push — master ( 3309bb...059e5b )
by Changwan
02:58
created

PsrLoader::getArguments()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9.732

Importance

Changes 0
Metric Value
cc 9
eloc 25
nc 13
nop 2
dl 0
loc 36
ccs 19
cts 24
cp 0.7917
crap 9.732
rs 4.909
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Router\Loader;
3
4
use Psr\Container\ContainerInterface;
5
use Psr\Container\NotFoundExceptionInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use ReflectionClass;
8
use RuntimeException;
9
use ReflectionException;
10
use Wandu\Router\Contracts\LoaderInterface;
11
use Wandu\Router\Contracts\MiddlewareInterface;
12
use Wandu\Router\Exception\HandlerNotFoundException;
13
14
class PsrLoader implements LoaderInterface
15
{
16
    /** @var \Psr\Container\ContainerInterface */
17
    protected $container;
18
19
    /**
20
     * @param \Psr\Container\ContainerInterface $container
21
     */
22 5
    public function __construct(ContainerInterface $container)
23
    {
24 5
        $this->container = $container;
25 5
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 2
    public function middleware(string $className): MiddlewareInterface
31
    {
32 2
        if ($this->container->has($className)) {
33 1
            return $this->container->get($className);
34
        }
35 1
        throw new HandlerNotFoundException($className);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 3
    public function execute(string $className, string $methodName, ServerRequestInterface $request)
42
    {
43 3
        if ($this->container->has($className)) {
44 3
            $object = $this->container->get($className);
45
            try {
46 3
                $reflMethod = (new ReflectionClass($className))->getMethod($methodName);
47 1
            } catch (ReflectionException $e) {
48 1
                $reflMethod = null;
49
            }
50 3
            if ($reflMethod) {
51 2
                $arguments = $this->getArguments($reflMethod, $request);
52 2
                return call_user_func_array([$object, $methodName], $arguments);
53
            }
54 1
            if (method_exists($object, '__call')) {
55 1
                return $object->__call($methodName, [$request]);
56
            }
57
        }
58
        throw new HandlerNotFoundException($className, $methodName);
59
    }
60
    
61 2
    public function getArguments(\ReflectionFunctionAbstract $refl, ServerRequestInterface $request)
62
    {
63 2
        $arguments = [];
64
        // it from container Resolver..
65 2
        foreach ($refl->getParameters() as $param) {
66 1
            $paramClass = $param->getClass();
67 1
            if ($paramClass) { // #2.
68 1
                if ($paramClass->isInstance($request)) {
69 1
                    $arguments[] = $request;
70 1
                    continue;
71
                }
72 1
                $paramClassName = $paramClass->getName();
0 ignored issues
show
Bug introduced by
Consider using $paramClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
73 1
                if ($attribute = $request->getAttribute($paramClassName)) {
74 1
                    $arguments[] = $attribute;
75 1
                    continue;
76
                }
77 1
                if ($this->container->has($paramClassName)) {
78
                    try {
79 1
                        $arguments[] = $this->container->get($paramClassName);
80
                        continue;
81 1
                    } catch (NotFoundExceptionInterface $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
82
                    }
83
                }
84
            }
85 1
            if ($param->isDefaultValueAvailable()) {
86
                $arguments[] = $param->getDefaultValue();
87
                continue;
88
            }
89 1
            if ($attribute = $request->getAttribute($param->getName())) {
0 ignored issues
show
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
90
                $arguments[] = $attribute;
91
                continue;
92
            }
93 1
            throw new RuntimeException("not found parameter named \"{$param->getName()}\".");
0 ignored issues
show
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
94
        }
95 2
        return $arguments;
96
    }
97
}
98