Completed
Push — master ( 579110...b52e58 )
by Pierre
02:10
created

Kernel   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 112
rs 10
wmc 9

5 Methods

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