Passed
Branch master (f36b3c)
by Matthew
08:01
created

Kernel::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.9332
cc 3
nc 3
nop 2
crap 3
1
<?php
2
namespace Fyuze\Http;
3
4
use Closure;
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use ReflectionClass;
8
use ReflectionParameter;
9
use Fyuze\Http\Exception\NotFoundException;
10
use Fyuze\Kernel\Registry;
11
use Fyuze\Routing\Router;
12
13
class Kernel
14
{
15
    /**
16
     * @var Registry
17
     */
18
    protected $registry;
19
20
    /**
21
     *
22
     * @var Router
23
     */
24
    protected $router;
25
26
    /**
27
     * @param Registry $registry
28
     * @param Router $router
29
     */
30 5
    public function __construct(Registry $registry, Router $router)
31
    {
32 5
        $this->registry = $registry;
33 5
        $this->router = $router;
34
    }
35
36
    /**
37
     * @param ServerRequestInterface $request
38
     * @return Response|mixed
39
     */
40 5
    public function handle(ServerRequestInterface $request)
41
    {
42
        try {
43
44 5
            $route = $this->router->resolve($request);
45
46 2
            $response = $this->resolve($route->getAction(), $route->getQueryParams());
47 1
            if(false === $response instanceof ResponseInterface) {
48 1
                $response = Response::create($response);
49
            }
50
51
52 4
        } catch (NotFoundException $e) {
53
54 3
            $response = Response::create('<body>Not Found</body>', 404);
55
56 1
        } catch (\Exception $e) {
57
58 1
            $response = Response::create(
59 1
                sprintf('<body>An unknown error has occurred: %s</body>', $e->getMessage()),
60 1
                500
61 1
            );
62
        }
63
64 5
        $this->registry->add('response', $response);
65
66 5
        return $this->registry->make('response');
67
    }
68
69
    /**
70
     * Method injection resolver
71
     *
72
     * @param $action
73
     * @param $params
74
     * @return mixed
75
     */
76 2
    protected function resolve($action, $params)
77
    {
78 2
        if ($action instanceof Closure) {
79
80 1
            return $action($params);
81
        }
82
83 1
        list($controller, $method) = $action;
84
85 1
        $reflect = new ReflectionClass($controller);
86
87 1
        foreach (array_filter($reflect->getMethod($method)->getParameters(), $this->getParams()) as $param) {
88 1
            array_unshift($params, $this->registry->make(
89 1
                (string) $param->getType()
90 1
            ));
91
        }
92
93 1
        return $reflect->getMethod($method)->invokeArgs(
94 1
            $this->registry->make($controller),
95 1
            $params
96 1
        );
97
    }
98
99
    /**
100
     * @return Closure
101
     */
102 1
    protected function getParams()
103
    {
104 1
        return function (ReflectionParameter $param) {
105 1
            return (string) $param->getType();
106 1
        };
107
    }
108
}
109