Passed
Push — master ( 5ae195...c1faee )
by Nícollas
02:35 queued 56s
created

MiddlewareCollection   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 56
dl 0
loc 215
rs 10
c 2
b 0
f 0
wmc 29

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A storeMiddleware() 0 7 2
A resolveLaterMiddleware() 0 15 3
A setRequest() 0 3 1
A next() 0 5 1
A execute() 0 3 1
A get() 0 7 4
A callMiddlewares() 0 16 4
A resolveNestedMiddleware() 0 7 1
A getByAlias() 0 13 3
A setMiddlewares() 0 3 1
A executeMiddleware() 0 11 2
A instanceMiddleware() 0 13 4
A reset() 0 3 1
1
<?php
2
3
namespace MinasRouter\Router\Middlewares;
4
5
use MinasRouter\Http\Request;
6
use MinasRouter\Router\Middlewares\MiddlewareRoute;
7
8
class MiddlewareCollection
9
{
10
    /** @var array */
11
    protected $middlewares = [];
12
13
    /** @var array */
14
    protected $currentRequest;
15
16
    /** @var array */
17
    protected $queue;
18
19
    /** @var int = 0 */
20
    protected $currentQueueNumber = 0;
21
22
    public function __construct($middlewares)
23
    {
24
        $this->middlewares = $middlewares;
25
    }
26
27
    public function get(String $middleware = null)
28
    {
29
        if ($middleware && is_array($this->middlewares) && isset($this->middlewares[$middleware])) {
30
            return $this->middlewares[$middleware];
31
        }
32
33
        return $this->middlewares;
34
    }
35
36
    /**
37
     * Method responsible for setting the Route's Request
38
     * 
39
     * @param MinasRouter\Http\Request $request
0 ignored issues
show
Bug introduced by
The type MinasRouter\Router\Middl...inasRouter\Http\Request was not found. Did you mean MinasRouter\Http\Request? If so, make sure to prefix the type with \.
Loading history...
40
     * 
41
     * @return void
42
     */
43
    public function setRequest(Request $request)
44
    {
45
        $this->currentRequest = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request of type MinasRouter\Http\Request is incompatible with the declared type array of property $currentRequest.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
    }
47
48
    /**
49
     * Method responsible for setting the
50
     * middlewares of the route.
51
     * 
52
     * @var array $middlewares
53
     * 
54
     * @return void
55
     */
56
    public function setMiddlewares(array $middlewares)
57
    {
58
        $this->middlewares = $middlewares;
59
    }
60
61
    public function storeMiddleware($middleware)
62
    {
63
        if (is_string($middleware)) {
64
            $middleware = explode(",", $middleware);
65
        }
66
67
        return $this->resolveLaterMiddleware($middleware);
68
    }
69
70
    private function resolveLaterMiddleware(array $middleware)
71
    {
72
        $currentMiddleware = $this->middlewares;
73
74
        foreach ($middleware as $mid) {
75
            $mid = trim(rtrim($mid));
76
            
77
            if (is_string($currentMiddleware)) {
78
                $currentMiddleware .= ", {$mid}";
79
            } else {
80
                $currentMiddleware[] = $mid;
81
            }
82
        }
83
84
        return $currentMiddleware;
85
    }
86
87
    /**
88
     * Alias for execute the middlewares.
89
     * 
90
     * @return mixed|bool
91
     */
92
    public function execute()
93
    {
94
        return $this->executeMiddleware();
95
    }
96
97
    /**
98
     * Execute the middlewares.
99
     * 
100
     * @return mixed|bool
101
     */
102
    protected function executeMiddleware()
103
    {
104
        $middlewares = $this->middlewares;
105
106
        if (is_string($middlewares)) {
0 ignored issues
show
introduced by
The condition is_string($middlewares) is always false.
Loading history...
107
            $middlewares = explode(',', $middlewares);
108
        }
109
110
        $this->resolveNestedMiddleware($middlewares);
111
112
        return $this->callMiddlewares();
113
    }
114
115
    /**
116
     * Method responsible for identifying
117
     * middlewares and instantiating them.
118
     * 
119
     * @param array $middlewares
120
     * 
121
     * @return void
122
     */
123
    protected function resolveNestedMiddleware(array $middlewares): void
124
    {
125
        $this->queue = array_map(function ($middleware) {
126
            $middleware = trim(rtrim($middleware));
127
128
            return $this->instanceMiddleware($middleware);
129
        }, $middlewares);
130
    }
131
132
    /**
133
     * Method responsible for instantiating middlewares.
134
     * 
135
     * @param string
136
     * 
137
     * @return null|object
138
     */
139
    protected function instanceMiddleware($middleware)
140
    {
141
        if (!preg_match("/\\\/", $middleware)) {
142
            if (!$middlewareClass = $this->getByAlias($middleware)) return;
143
144
            return new $middlewareClass();
145
        }
146
147
        if (class_exists($middleware)) {
148
            return new $middleware();
149
        }
150
151
        return;
152
    }
153
154
    /**
155
     * Method responsible for identifying 
156
     * a middleware by alias.
157
     * 
158
     * @param string $alias
159
     * 
160
     * @return null|string
161
     */
162
    protected function getByAlias(String $alias)
163
    {
164
        $middlewares = MiddlewareRoute::getGlobalMiddlewares();
165
166
        if (!array_key_exists($alias, $middlewares)) {
167
            return;
168
        }
169
170
        if (class_exists($middlewares[$alias])) {
171
            return $middlewares[$alias];
172
        }
173
174
        return;
175
    }
176
177
    /**
178
     * Method responsible for 
179
     * calling the next middleware.
180
     * 
181
     * @return mixed|bool
182
     */
183
    protected function next()
184
    {
185
        $this->currentQueueNumber++;
186
187
        return $this->callMiddlewares();
188
    }
189
190
    /**
191
     * Method responsible for resetting 
192
     * the middleware queue.
193
     * 
194
     * @return void
195
     */
196
    protected function reset()
197
    {
198
        $this->currentQueueNumber = 0;
199
    }
200
201
    /**
202
     * Method responsible for calling
203
     * middlewares and class handle.
204
     * 
205
     * @return mixed|bool
206
     */
207
    protected function callMiddlewares()
208
    {
209
        if (!isset($this->queue[$this->currentQueueNumber])) {
210
            $this->reset();
211
            return true;
212
        }
213
214
        $currentMiddleware = $this->queue[$this->currentQueueNumber];
215
216
        if (is_null($currentMiddleware) || empty($currentMiddleware)) {
217
            return $this->next();
218
        }
219
220
        return $currentMiddleware->handle(
221
            $this->currentRequest,
222
            fn () => $this->next()
223
        );
224
    }
225
}
226