Completed
Push — master ( 14c33c...acb33b )
by Pierre
03:53
created

Kernel::setNameSpace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Component\Http;
4
5
use App\Component\Http\Response;
6
use App\Component\Http\Headers;
7
8
class Kernel
9
{
10
11
    const PATH_CONFIG = '/../config/';
12
13
    use \App\Component\Http\Reuse\TKernel;
0 ignored issues
show
Bug introduced by
The trait App\Component\Http\Reuse\TKernel requires the property $name which is not provided by App\Component\Http\Kernel.
Loading history...
14
15
    /**
16
     * instanciate
17
     *
18
     */
19 26
    public function __construct(string $env, string $path)
20
    {
21 26
        $this->init($env, $path);
22
    }
23
24
    /**
25
     * set controller namespace
26
     *
27
     * @param string $ctrlNamespace
28
     * @return Kernel
29
     */
30 52
    public function setNameSpace(string $ctrlNamespace): Kernel
31
    {
32 52
        $this->spacename = $ctrlNamespace;
33 52
        return $this;
34
    }
35
36
    /**
37
     * retrieve kernel instance classname from container
38
     *
39
     * @return string
40
     */
41
    public function getBundleClassname():string
42
    {
43
        return get_called_class();
44
    }
45
46
    /**
47
     * run app
48
     *
49
     * @param array $routerGroups
50
     * @return Kernel
51
     */
52 6
    public function run(array $groups = []): Kernel
53
    {
54 6
        $routerGroups = ($groups)
55 4
            ? $groups
56 6
            : $this->router->compile();
57 6
        if ($routerGroups) {
58 4
            $this->setClassname($routerGroups);
59 4
            if (class_exists($this->className)) {
60 2
                $this->setController();
61 2
                $this->setReflector();
62 2
                $this->setActions();
63 2
                $this->setAction($routerGroups);
64
                //->setActionAnnotations();
65 2
                $this->setMiddleware();
66
            } else {
67 2
                $this->error = true;
68 2
                $this->errorCode = Response::HTTP_SERVICE_UNAVAILABLE;
69
            }
70
        }
71 6
        return $this;
72
    }
73
74
    /**
75
     * set controller action from router groups
76
     *
77
     * @param array $routerGrps
78
     * @return void
79
     */
80 20
    public function setAction(array $routerGrps)
81
    {
82 20
        $this->action = isset($routerGrps[1]) ? strtolower($routerGrps[1]) : '';
83
    }
84
85
    /**
86
     * dispatch response
87
     *
88
     * @return Kernel
89
     */
90 2
    public function send(): Kernel
91
    {
92 2
        if ($this->getError()) {
93 2
            $this->res
94 2
                ->setCode($this->errorCode)
95 2
                ->setContent([
96 2
                    Response::_ERROR => $this->error,
97 2
                    Response::_ERROR_CODE => $this->errorCode,
98 2
                    Response::_ERROR_MSG => $this->errorMsg,
99
                ])
100 2
                ->getHeaderManager()
101 2
                ->add(
102 2
                    Headers::CONTENT_TYPE,
103 2
                    'application/json; charset=utf-8'
104
                );
105 2
            $this->getLogger()->warning($this->errorMsg);
106
        } else {
107 2
            $this->getLogger()->debug('Response sent');
108
        }
109 2
        $this->getResponse()->send();
110 2
        return $this;
111
    }
112
113
    /**
114
     * shutdown kernel
115
     *
116
     * @return void
117
     */
118 2
    public function shutdown(int $code = 0)
119
    {
120 2
        throw new \Exception('shutdown', $code);
121
    }
122
}
123