Passed
Push — develop ( d334d9...84147f )
by Greg
06:22
created

ModuleMiddleware.php$0 ➔ __construct()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2019 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
declare(strict_types=1);
17
18
namespace Fisharebest\Webtrees\Http\Middleware;
19
20
use Fisharebest\Webtrees\Http\Response;
21
use Fisharebest\Webtrees\Services\ModuleService;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Psr\Http\Server\MiddlewareInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
27
/**
28
 * Middleware to apply all the module middleware.
29
 */
30
class ModuleMiddleware implements MiddlewareInterface
31
{
32
    /** @var ModuleService */
33
    private $module_service;
34
35
    /**
36
     * @param ModuleService $module_service
37
     */
38
    public function __construct(ModuleService $module_service)
39
    {
40
        $this->module_service = $module_service;
41
    }
42
43
    /**
44
     * Update the database schema, if necessary.
45
     *
46
     * @param ServerRequestInterface  $request
47
     * @param RequestHandlerInterface $handler
48
     *
49
     * @return Response
50
     */
51
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
52
    {
53
        $middlewares = $this->module_service->findByInterface(MiddlewareInterface::class);
54
55
        $pipeline = $handler;
56
57
        foreach ($middlewares as $middleware) {
58
            $pipeline = new class($pipeline, $middleware) implements RequestHandlerInterface
59
            {
60
                /** @var RequestHandlerInterface */
61
                private $handler;
62
63
                /** @var MiddlewareInterface */
64
                private $middleware;
65
66
                public function __construct(RequestHandlerInterface $handler, MiddlewareInterface $middleware)
67
                {
68
                    $this->handler    = $handler;
69
                    $this->middleware = $middleware;
70
                }
71
72
                public function handle(ServerRequestInterface $request): ResponseInterface
73
                {
74
                    return $this->middleware->process($request, $this->handler);
75
                }
76
            };
77
        }
78
79
        return $pipeline->handle($request);
80
    }
81
82
    private function createHandlerFromClosure(): RequestHandlerInterface
0 ignored issues
show
Unused Code introduced by
The method createHandlerFromClosure() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
83
    {
84
85
    }
86
}
87