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

ZendPsr7::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 20
rs 9.9332
cc 2
nc 2
nop 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 Shieldon\Firewall\Firewall;
19
use Shieldon\Firewall\HttpResolver;
20
use function dirname;
21
22
/**
23
 * PSR-7 Middleware for Zend Framework
24
 */
25
class ZendPsr7
26
{
27
    /**
28
     * The absolute path of the storage where stores Shieldon generated data.
29
     *
30
     * @var string
31
     */
32
    protected $storage;
33
34
    /**
35
     * The entry point of Shieldon Firewall's control panel.
36
     *
37
     * For example: https://yoursite.com/firewall/panel/
38
     * Just use the path component of a URI.
39
     *
40
     * @var string
41
     */
42
    protected $panelUri;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param string $storage  See property `storage` explanation.
48
     * @param string $panelUri See property `panelUri` explanation.
49
     *
50
     * @return void
51
     */
52
    public function __construct(string $storage = '', string $panelUri = '')
53
    {
54
        $dir = dirname($_SERVER['SCRIPT_FILENAME']);
55
56
        $this->storage = $dir . '/../data/shieldon_firewall';
57
        $this->panelUri = '/firewall/panel/';
58
59
        if ('' !== $storage) {
60
            $this->storage = $storage;
61
        }
62
63
        if ('' !== $panelUri) {
64
            $this->panelUri = $panelUri;
65
        }
66
    }
67
68
    /**
69
     * Shieldon middleware invokable class
70
     *
71
     * @param Request  $request  PSR7 request
72
     * @param Response $response PSR7 response
73
     * @param callable $next     Next middleware
74
     *
75
     * @return Response
76
     */
77
    public function __invoke(Request $request, Response $response, $next): Response
78
    {
79
        $firewall = new Firewall($request, $response);
80
81
        // Pass \Zend\Validator\Csrf CSRF Token to Captcha form.
82
        $firewall->getKernel()->setCaptcha(
83
            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...
84
                'name' => '_shieldon_csrf',
85
                'value' => $request->getAttribute('_shieldon_csrf'),
86
            ])
87
        );
88
89
        $response = $firewall->run();
90
91
        if ($response->getStatusCode() !== 200) {
92
            $httpResolver = new HttpResolver();
93
            $httpResolver($response);
94
        }
95
96
        return $next($request, $response);
97
    }
98
}
99