Completed
Push — master ( 3cf2a3...b96e1a )
by Pierre
02:31 queued 12s
created

Kernel::setAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
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 PATH_CONFIG = '/../config/';
12
13
    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...
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 26
    public function setNameSpace(string $ctrlNamespace): Kernel
31
    {
32 26
        $this->nameSpace = $ctrlNamespace;
33 26
        return $this;
34
    }
35
36
    /**
37
     * run app
38
     *
39
     * @param array $routerGroups
40
     * @return Kernel
41
     */
42 3
    public function run(array $groups = []): Kernel
43
    {
44 3
        $routerGroups = ($groups)
45 2
            ? $groups
46 3
            : $this->router->compile();
47 3
        if ($routerGroups) {
48 2
            $this->setClassname($routerGroups);
49 2
            if (class_exists($this->className)) {
50 1
                $this->setController();
51 1
                $this->setReflector();
52 1
                $this->setActions();
53 1
                $this->setAction(
54 1
                    $routerGroups,
55 1
                    $this->getRequest()->getMethod()
0 ignored issues
show
Unused Code introduced by
The call to App\Kernel::setAction() has too many arguments starting with $this->getRequest()->getMethod(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
                $this->/** @scrutinizer ignore-call */ 
56
                       setAction(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
56
                );
57
                //->setActionAnnotations();
58 1
                $this->setMiddleware();
59
            } else {
60 1
                $this->error = true;
61 1
                $this->errorCode = Response::HTTP_SERVICE_UNAVAILABLE;
62
            }
63
        }
64 3
        return $this;
65
    }
66
67
    /**
68
     * set controller action from router groups
69
     *
70
     * @param array $routerGrps
71
     * @return void
72
     */
73 10
    public function setAction(array $routerGrps)
74
    {
75 10
        $this->action = isset($routerGrps[1]) ? strtolower($routerGrps[1]) : '';
76
    }
77
78
    /**
79
     * dispatch response
80
     *
81
     * @return Kernel
82
     */
83 1
    public function send(): Kernel
84
    {
85 1
        if ($this->getError()) {
86 1
            $this->res
87 1
                ->setCode($this->errorCode)
88 1
                ->setContent([
89 1
                    Response::_ERROR => $this->error,
90 1
                    Response::_ERROR_CODE => $this->errorCode,
91 1
                    Response::_ERROR_MSG => $this->errorMsg,
92
                ])
93 1
                ->getHeaderManager()
94 1
                ->add(
95 1
                    Headers::CONTENT_TYPE,
96 1
                    'application/json; charset=utf-8'
97
                );
98 1
            $this->getLogger()->warning($this->errorMsg);
99
        } else {
100 1
            $this->getLogger()->debug('Response sent');
101
        }
102 1
        $this->getResponse()->send();
103 1
        return $this;
104
    }
105
106
    /**
107
     * shutdown kernel
108
     *
109
     * @return void
110
     */
111 1
    public function shutdown(int $code = 0)
112
    {
113 1
        throw new \Exception('shutdown', $code);
114
    }
115
}
116