Completed
Push — master ( 0e17e5...d2ba08 )
by Pierre
01:59
created

Kernel::shutdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Nymfonya\Component\Http;
4
5
use Nymfonya\Component\Http\Response;
6
use Nymfonya\Component\Http\Headers;
7
8
class Kernel
9
{
10
11
    const PATH_CONFIG = '/../config/';
12
13
    use \Nymfonya\Component\Http\Reuse\TKernel;
14
15
    /**
16
     * instanciate
17
     * @return Kernel
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
18
     */
19 26
    public function __construct(string $env, string $path)
20
    {
21 26
        $this->init($env, $path);
22 26
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
23
    }
24
25
    /**
26
     * set controller namespace
27
     *
28
     * @param string $ctrlNamespace
29
     * @return Kernel
30
     */
31 26
    public function setNameSpace(string $ctrlNamespace): Kernel
32
    {
33 26
        $this->spacename = $ctrlNamespace;
34 26
        return $this;
35
    }
36
37
    /**
38
     * retrieve kernel instance classname from container
39
     *
40
     * @return string
41
     */
42
    public function getBundleClassname():string
43
    {
44
        return get_called_class();
45
    }
46
47
    /**
48
     * run app
49
     *
50
     * @param array $groups
51
     * @return Kernel
52
     */
53 3
    public function run(array $groups = []): Kernel
54
    {
55 3
        $routerGroups = (empty($groups))
56 1
            ? $this->router->compile()
57 3
            : $groups;
58 3
        if ($routerGroups) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $routerGroups of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
59 2
            $this->setClassname($routerGroups);
60 2
            if (class_exists($this->className)) {
61 1
                $this->setController();
62 1
                $this->setReflector();
63 1
                $this->setActions();
64 1
                $this->setAction($routerGroups);
65
                //->setActionAnnotations();
66 1
                $this->setMiddleware();
67
            } else {
68 1
                $this->error = true;
69 1
                $this->errorCode = Response::HTTP_SERVICE_UNAVAILABLE;
70
            }
71
        }
72 3
        return $this;
73
    }
74
75
    /**
76
     * set controller action from router groups
77
     *
78
     * @param array $routerGrps
79
     * @return void
80
     */
81 10
    public function setAction(array $routerGrps)
82
    {
83 10
        $this->action = isset($routerGrps[1]) ? strtolower($routerGrps[1]) : '';
84
    }
85
86
    /**
87
     * dispatch response
88
     *
89
     * @return Kernel
90
     */
91 1
    public function send(): Kernel
92
    {
93 1
        if ($this->getError()) {
94 1
            $this->res
95 1
                ->setCode($this->errorCode)
96 1
                ->setContent([
97 1
                    Response::_ERROR => $this->error,
98 1
                    Response::_ERROR_CODE => $this->errorCode,
99 1
                    Response::_ERROR_MSG => $this->errorMsg,
100
                ])
101 1
                ->getHeaderManager()
102 1
                ->add(
103 1
                    Headers::CONTENT_TYPE,
104 1
                    'application/json; charset=utf-8'
105
                );
106 1
            $this->getLogger()->warning($this->errorMsg);
107
        } else {
108 1
            $this->getLogger()->debug('Response sent');
109
        }
110 1
        $this->getResponse()->send();
111 1
        return $this;
112
    }
113
114
    /**
115
     * shutdown kernel
116
     *
117
     * @return void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
118
     */
119 1
    public function shutdown(int $code = 0)
120
    {
121 1
        throw new \Exception('shutdown', $code);
122
    }
123
}
124