Passed
Push — master ( 764db3...6a1819 )
by Marcio
03:46
created

RouteMiddleware::middlewareAfter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 *
5
 * KNUT7 K7F (https://marciozebedeu.com/)
6
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @link      https://github.com/knut7/framework/ for the canonical source repository
13
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
14
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
15
 * @author    Marcio Zebedeu - [email protected]
16
 * @version   1.0.14
17
 *
18
 *
19
 */
20
21
namespace Ballybran\Routing\Router;
22
23
class RouteMiddleware {
24
25
    /**
26
     * @var array $middlewares General middlewares for per request
27
     */
28
    protected $middlewares = [];
29
30
    /**
31
     * @var array $routeMiddlewares Route middlewares
32
     */
33
    protected $routeMiddlewares = [];
34
35
    /**
36
     * @var array $middlewareGroups Middleware Groups
37
     */
38
    protected $middlewareGroups = [];
39
40
    /**
41
     * [TODO] This method implementation not completed yet.
42
     *
43
     * Set route middleware
44
     *
45
     * @param string|array $middleware
46
     * @param string $type
47
     *
48
     * @return $this
49
     */
50
    public function middleware($middleware, $type = 'before')
51
    {
52
        if (!is_array($middleware) && !is_string($middleware)) {
0 ignored issues
show
introduced by
The condition is_string($middleware) is always true.
Loading history...
53
            return $this;
54
        }
55
56
        $currentRoute = end($this->routes);
57
        $currentRoute[$type] = $middleware;
58
        array_pop($this->routes);
59
        array_push($this->routes, $currentRoute);
60
61
        return $this;
62
    }
63
64
    /**
65
     * [TODO] This method implementation not completed yet.
66
     *
67
     * @param string|array $middleware
68
     *
69
     * @return $this
70
     */
71
    public function middlewareBefore($middleware)
72
    {
73
        $this->middleware($middleware, 'before');
74
75
        return $this;
76
    }
77
78
    /**
79
     * [TODO] This method implementation not completed yet.
80
     *
81
     * @param string|array $middleware
82
     *
83
     * @return $this
84
     */
85
    public function middlewareAfter($middleware)
86
    {
87
        $this->middleware($middleware, 'after');
88
89
        return $this;
90
    }
91
92
    /**
93
     * [TODO] This method implementation not completed yet.
94
     *
95
     * Set general middlewares
96
     *
97
     * @param array $middlewares
98
     *
99
     * @return void
100
     */
101
    public function setMiddleware(array $middlewares)
102
    {
103
        $this->middlewares = $middlewares;
104
    }
105
106
    /**
107
     * [TODO] This method implementation not completed yet.
108
     *
109
     * Set Route middlewares
110
     *
111
     * @param array $middlewares
112
     *
113
     * @return void
114
     */
115
    public function setRouteMiddleware(array $middlewares)
116
    {
117
        $this->routeMiddlewares = $middlewares;
118
    }
119
120
    /**
121
     * [TODO] This method implementation not completed yet.
122
     *
123
     * Set middleware groups
124
     *
125
     * @param array $middlewareGroup
126
     *
127
     * @return void
128
     */
129
    public function setMiddlewareGroup(array $middlewareGroup)
130
    {
131
        $this->middlewareGroups = $middlewareGroup;
132
    }
133
134
     /**
135
     * Detect Routes Middleware; before or after
136
     *
137
     * @param $middleware
138
     * @param $type
139
     *
140
     * @return void
141
     */
142
    public function runRouteMiddleware($middleware, $type)
143
    {
144
        if ($type === 'before') {
145
            if (!is_null($middleware['group'])) {
146
                $this->routerCommand()->beforeAfter($middleware['group'][$type]);
0 ignored issues
show
introduced by
The method routerCommand() does not exist on Ballybran\Routing\Router\RouteMiddleware. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

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

146
                $this->/** @scrutinizer ignore-call */ 
147
                       routerCommand()->beforeAfter($middleware['group'][$type]);
Loading history...
147
            }
148
            $this->routerCommand()->beforeAfter($middleware[$type]);
149
        } else {
150
            $this->routerCommand()->beforeAfter($middleware[$type]);
151
            if (!is_null($middleware['group'])) {
152
                $this->routerCommand()->beforeAfter($middleware['group'][$type]);
153
            }
154
        }
155
    }
156
157
}