Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
20.00% |
1 / 5 |
CRAP | |
12.50% |
2 / 16 |
MiddlewareStack | |
0.00% |
0 / 1 |
|
20.00% |
1 / 5 |
21.75 | |
12.50% |
2 / 16 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
addMiddleware | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
process | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 6 |
|||
createMiddleware | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
createCoreFunction | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\CommandsLaravel\Middleware\Core; |
4 | |
5 | use Closure; |
6 | use Qmp\Laravel\CommandsLaravel\Console\Commands\Command; |
7 | |
8 | class MiddlewareStack |
9 | { |
10 | |
11 | /** |
12 | * Undocumented variable |
13 | * |
14 | * @var array |
15 | */ |
16 | protected $middleware = []; |
17 | |
18 | /** |
19 | * Undocumented variable |
20 | * |
21 | * @var Command |
22 | */ |
23 | protected $command; |
24 | |
25 | /** |
26 | * Undocumented variable |
27 | * |
28 | * @var MiddlewareData |
29 | */ |
30 | protected $middlewareData; |
31 | |
32 | /** |
33 | * Undocumented function |
34 | * |
35 | * @param Command $command |
36 | */ |
37 | public function __construct(Command $command) |
38 | { |
39 | $this->command = $command; |
40 | } |
41 | |
42 | /** |
43 | * Undocumented function |
44 | * |
45 | * @param array $middleware |
46 | * @return self |
47 | */ |
48 | public function addMiddleware(array $middleware) |
49 | { |
50 | $this->middleware = array_merge($this->middleware, $middleware); |
51 | |
52 | return $this; |
53 | } |
54 | |
55 | /** |
56 | * Undocumented function |
57 | * |
58 | * @param Closure $core |
59 | * @return void |
60 | */ |
61 | public function process(Closure $core) |
62 | { |
63 | $coreFunction = $this->createCoreFunction($core); |
64 | $middleware = array_reverse($this->middleware); |
65 | $completeMiddleware = array_reduce($middleware, function ($nextMiddleware, $middleware) { |
66 | return $this->createMiddleware($nextMiddleware, $middleware); |
67 | }, $coreFunction); |
68 | |
69 | return $completeMiddleware(); |
70 | } |
71 | |
72 | /** |
73 | * Undocumented function |
74 | * |
75 | * @param [type] $nextLayer |
76 | * @param [type] $layer |
77 | * @return void |
78 | */ |
79 | private function createMiddleware($nextMiddleware, $middleware) |
80 | { |
81 | return function () use ($nextMiddleware, $middleware) { |
82 | return (new $middleware())->handle($this->command, $nextMiddleware); |
83 | }; |
84 | } |
85 | |
86 | /** |
87 | * Undocumented function |
88 | * |
89 | * @param Closure $core |
90 | * @return void |
91 | */ |
92 | private function createCoreFunction(Closure $core) |
93 | { |
94 | return function () use ($core) { |
95 | return $core(); |
96 | }; |
97 | } |
98 | } |