Completed
Push — snyk-fix-a7ffc9795c22fef729062... ( 82e92b )
by Greg
08:14
created

WrapHandler::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 19
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
declare(strict_types=1);
18
19
namespace Fisharebest\Webtrees\Http\Middleware;
20
21
use Illuminate\Support\Str;
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
use function app;
28
use function explode;
29
use function is_string;
30
use function substr_compare;
31
32
/**
33
 * Middleware to run a controller/action or a request-handler.
34
 */
35
class WrapHandler implements MiddlewareInterface
36
{
37
    private const CONTROLLER_NAMESPACE = '\\Fisharebest\\Webtrees\\Http\\Controllers\\';
38
39
    // To parse Controller::action
40
    private const SCOPE_OPERATOR = '::';
41
42
    /** @var string|MiddlewareInterface */
43
    private $handler;
44
45
    /**
46
     * WrapController constructor.
47
     *
48
     * @param string $handler
49
     */
50
    public function __construct(string $handler)
51
    {
52
        $this->handler = $handler;
53
    }
54
55
    /**
56
     * @param ServerRequestInterface  $request
57
     * @param RequestHandlerInterface $handler
58
     *
59
     * @return ResponseInterface
60
     */
61
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
62
    {
63
        if (is_string($this->handler)) {
64
            if (Str::contains($this->handler, self::SCOPE_OPERATOR)) {
65
                [$class, $method] = explode(self::SCOPE_OPERATOR, $this->handler);
66
67
                if (substr_compare($class, '\\', 0, 1) !== 0) {
68
                    $class = self::CONTROLLER_NAMESPACE . $class;
69
                }
70
71
                $controller = app($class);
72
73
                return $controller->$method($request);
74
            }
75
76
            return app($this->handler)->handle($request);
77
        }
78
79
        return $this->handler->handle($request);
0 ignored issues
show
Bug introduced by
The method handle() does not exist on Psr\Http\Server\MiddlewareInterface. It seems like you code against a sub-type of Psr\Http\Server\MiddlewareInterface such as Middleland\Dispatcher. ( Ignorable by Annotation )

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

79
        return $this->handler->/** @scrutinizer ignore-call */ handle($request);
Loading history...
80
    }
81
}
82