TraitRoute   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 12
Bugs 4 Features 0
Metric Value
eloc 90
c 12
b 4
f 0
dl 0
loc 220
rs 9.84
wmc 32

16 Methods

Rating   Name   Duplication   Size   Complexity  
A duplicates() 0 17 4
A pregMatch() 0 6 2
A setClass() 0 10 2
A keyRouter() 0 9 6
A patterns() 0 8 2
A controller() 0 8 2
A getQuery() 0 3 1
A getRequest() 0 3 1
A isNotFound() 0 3 1
A getMethod() 0 3 1
A getResponse() 0 3 1
A setResponse() 0 10 1
A classMethod() 0 43 5
A getPatterns() 0 3 1
A getNamespace() 0 3 1
A getRoute() 0 3 1
1
<?php
2
3
namespace Erykai\Routes;
4
5
use RuntimeException;
6
7
/**
8
 * Trait Controller Router
9
 */
10
trait TraitRoute
11
{
12
    /**
13
     * @var object
14
     */
15
    private object $response;
16
    /**
17
     * @param array $array
18
     * @return object
19
     */
20
    private function duplicates(array $array): object
21
    {
22
        if ($this->getRequest() !== "") {
23
            foreach ($array as $key => $item) {
24
                if ($this->verb[$key] !== $this->getMethod()) {
25
                    unset(
26
                        $this->controller[$key],
27
                        $this->middleware[$key],
28
                        $this->type[$key],
29
                        $this->verb[$key],
30
                        $this->route[$key],
31
                        $this->patterns[$key]
32
                    );
33
                }
34
            }
35
        }
36
        return $this;
37
    }
38
    /**
39
     * @return bool
40
     */
41
    private function patterns(): bool
42
    {
43
        $patterns = array_unique($this->getPatterns());
44
        if ($this->getRequest() === "") {
45
            $patterns = (array)$patterns[0];
46
        }
47
        $this->keyRouter($patterns);
48
        return true;
49
    }
50
    /**
51
     * @param $pattern
52
     * @return mixed
53
     */
54
    private function pregMatch($pattern): mixed
55
    {
56
        if (preg_match('#' . $pattern . '#', $this->getRequest(), $router) === false) {
57
            throw new RuntimeException('Error ' . $pattern . ' ' . $this->getRequest());
58
        }
59
        return $router;
60
    }
61
    /**
62
     * @param $patterns
63
     */
64
    private function keyRouter($patterns): void
65
    {
66
        foreach ($patterns as $key => $pattern) {
67
            if ($this->getMethod() === $this->verb[$key]) {
68
                $router = $this->pregMatch($pattern);
69
                if (isset($router[0]) && $router[0] === $this->getRequest()) {
70
                    $this->setClass($key, $router);
71
                } else if ($this->getRequest() === "") {
72
                    $this->setClass($key, $router);
73
                }
74
            }
75
76
        }
77
    }
78
    /**
79
     * @param $key
80
     * @param $router
81
     */
82
    private function setClass($key, $router): void
83
    {
84
        $classMethod = explode("@", $this->controller[$key]);
85
        [$class, $method] = $classMethod;
86
        array_shift($router);
87
        if (preg_match_all('~{([^}]*)}~', $this->route[$key], $keys) === false) {
88
            throw new RuntimeException('The route ' . $this->route[$key] . ' not exist.');
89
        }
90
        $data = array_combine($keys[1], $router);
91
        $this->classMethod($class, $method, $data, $key);
92
    }
93
    /**
94
     * @param $class
95
     * @param $method
96
     * @param $data
97
     * @param $key
98
     * @return bool|void
99
     */
100
    private function classMethod($class, $method, $data, $key)
101
    {
102
        $argument = $data;
103
        unset($data);
104
        $data['argument'] = $argument;
105
        $class = $this->namespaceArray[$key] . "\\" . $class;
106
        if (class_exists($class)) {
107
            $Class = $class::getInstance();
108
            if (method_exists($Class, $method)) {
109
                $this->setNotFound(false);
0 ignored issues
show
Bug introduced by
It seems like setNotFound() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

109
                $this->/** @scrutinizer ignore-call */ 
110
                       setNotFound(false);
Loading history...
110
                $data['query'] = $this->getQuery();
111
                if ($this->middleware[$key]) {
112
                    $Middleware = new Middleware();
113
                    if (!$Middleware->validate()) {
114
                        $this->setResponse(
115
                            401,
116
                            "error",
117
                            "this access is mandatory to inform the correct Baren Token",
118
                            "mandatory"
119
                        );
120
                        return false;
121
                    }
122
                }
123
                $Class->$method($data, $this->type[$key]);
124
                return true;
125
            }
126
            $this->setResponse(
127
                405,
128
                "error",
129
                "the {$this->controller[$key]} method does not exist",
130
                "controller",
131
                dynamic: $this->controller[$key]
132
            );
133
            return false;
134
        }
135
        $this->setResponse(
136
            405,
137
            "error",
138
            "the {$class} class does not exist",
139
            "controller",
140
            dynamic: $this->controller[$key]
141
        );
142
        return false;
143
    }
144
    /**
145
     * @return bool
146
     */
147
    protected function controller(): bool
148
    {
149
        $this->duplicates($this->getPatterns());
150
        $this->patterns();
151
        if (empty($this->getPatterns())) {
152
            return false;
153
        }
154
        return true;
155
    }
156
    /**
157
     * @return array
158
     */
159
    protected function getPatterns(): array
160
    {
161
        return $this->patterns;
162
    }
163
    /**
164
     * @return object
165
     */
166
    protected function getResponse(): object
167
    {
168
        return $this->response;
169
    }
170
    /**
171
     * @return string
172
     */
173
    protected function getMethod(): string
174
    {
175
        return $this->method;
176
    }
177
    /**
178
     * @return string
179
     */
180
    protected function getRequest(): string
181
    {
182
        return $this->request;
183
    }
184
    /**
185
     * @return array|null
186
     */
187
    protected function getQuery(): ?array
188
    {
189
        return $this->query;
190
    }
191
    /**
192
     * @return string
193
     */
194
    protected function getNamespace(): string
195
    {
196
        return $this->namespace;
197
    }
198
    /**
199
     * @return array
200
     */
201
    protected function getRoute(): array
202
    {
203
        return $this->route;
204
    }
205
    /**
206
     * @return bool
207
     */
208
    protected function isNotFound(): bool
209
    {
210
        return $this->notFound;
211
    }
212
    /**
213
     * @param int $code
214
     * @param string $type
215
     * @param string $text
216
     * @param string $model
217
     * @param object|null $data
218
     * @param string|null $dynamic
219
     */
220
    protected function setResponse(int $code, string $type, string $text, string $model, ?object $data = null, ?string $dynamic = null): void
221
    {
222
        http_response_code($code);
223
        $this->response = (object)[
224
            "code" => $code,
225
            "type" => $type,
226
            "text" => $text,
227
            "model" => $model,
228
            "data" => $data,
229
            "dynamic" => $dynamic
230
        ];
231
    }
232
}