Passed
Push — 2.x ( 3b7a15...f6a0a4 )
by Terry
02:08
created

Slim4   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 25
c 1
b 0
f 0
dl 0
loc 80
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 29 2
A __construct() 0 13 3
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 */
11
12
declare(strict_types=1);
13
14
namespace Shieldon\Firewall\Integration;
15
16
use Psr\Http\Message\ResponseInterface as Response;
17
use Psr\Http\Message\ServerRequestInterface as Request;
18
use Psr\Http\Server\MiddlewareInterface as Middleware;
19
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
20
use Shieldon\Firewall\Firewall;
21
use Shieldon\Firewall\HttpResolver;
22
use function dirname;
23
24
/**
25
 * Middleware for Slim 4 framework
26
 */
27
class Slim4 implements Middleware
28
{
29
    /**
30
     * The absolute path of the storage where stores Shieldon generated data.
31
     *
32
     * @var string
33
     */
34
    protected $storage;
35
36
    /**
37
     * The entry point of Shieldon Firewall's control panel.
38
     *
39
     * For example: https://yoursite.com/firewall/panel/
40
     * Just use the path component of a URI.
41
     *
42
     * @var string
43
     */
44
    protected $panelUri;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param string $storage  See property `storage` explanation.
50
     * @param string $panelUri See property `panelUri` explanation.
51
     *
52
     * @return void
53
     */
54
    public function __construct(string $storage = '', string $panelUri = '')
55
    {
56
        $dir = dirname($_SERVER['SCRIPT_FILENAME']);
57
58
        $this->storage = $dir . '/../shieldon_firewall';
59
        $this->panelUri = '/firewall/panel/';
60
61
        if ('' !== $storage) {
62
            $this->storage = $storage;
63
        }
64
65
        if ('' !== $panelUri) {
66
            $this->panelUri = $panelUri;
67
        }
68
    }
69
70
    /**
71
     * Shieldon middleware invokable class.
72
     *
73
     * @param ServerRequest  $request PSR-7 request
0 ignored issues
show
Bug introduced by
The type Shieldon\Firewall\Integration\ServerRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
     * @param RequestHandler $handler PSR-15 request handler
75
     *
76
     * @return Response
77
     */
78
    public function process(Request $request, RequestHandler $handler): Response
79
    {
80
        $firewall = new Firewall($request);
81
        $firewall->configure($this->storage);
82
        $firewall->controlPanel($this->panelUri);
83
        
84
        // Pass Slim CSRF Token to Captcha form.
85
        $firewall->getKernel()->setCaptcha(
86
            new \Shieldon\Captcha\Csrf([
0 ignored issues
show
Bug introduced by
The type Shieldon\Captcha\Csrf was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
                'name' => 'csrf_name',
88
                'value' => $request->getAttribute('csrf_name'),
89
            ])
90
        );
91
92
        $firewall->getKernel()->setCaptcha(
93
            new \Shieldon\Captcha\Csrf([
94
                'name' => 'csrf_value',
95
                'value' => $request->getAttribute('csrf_value'),
96
            ])
97
        );
98
99
        $response = $firewall->run();
100
101
        if ($response->getStatusCode() !== 200) {
102
            $httpResolver = new HttpResolver();
103
            $httpResolver($response);
104
        }
105
106
        return $handler->handle($request);
107
    }
108
}
109