Laravel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 20
c 2
b 0
f 0
dl 0
loc 72
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 22 2
A __construct() 0 12 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
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Integration;
14
15
use Psr\Http\Message\ServerRequestInterface as Request;
16
use Shieldon\Firewall\Firewall;
17
use Shieldon\Firewall\HttpResolver;
18
use Shieldon\Firewall\Captcha\Csrf;
19
use function storage_path; // Laravel
0 ignored issues
show
introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
use function csrf_token; // Laravel
0 ignored issues
show
introduced by
The function csrf_token was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
22
/**
23
 * Middleware for Laravel framework (5.x - 6.x)
24
 */
25
class Laravel
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
        // The Shieldon generated data is stored at that place.
55
        $this->storage = storage_path('shieldon_firewall');
0 ignored issues
show
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

55
        $this->storage = /** @scrutinizer ignore-call */ storage_path('shieldon_firewall');
Loading history...
56
        $this->panelUri = '/firewall/panel/';
57
58
        if ('' !== $storage) {
59
            $this->storage = $storage;
60
        }
61
62
        if ('' !== $panelUri) {
63
            $this->panelUri = $panelUri;
64
        }
65
    }
66
67
    /**
68
     * Handle an incoming request.
69
     *
70
     * @param Request $request The PSR-7 server request.
71
     * @param Closure $next    The next middleware.
72
     *
73
     * @return mixed
74
     */
75
    public function handle($request, Closure $next)
0 ignored issues
show
Bug introduced by
The type Shieldon\Firewall\Integration\Closure was not found. Did you mean Closure? If so, make sure to prefix the type with \.
Loading history...
76
    {
77
        $firewall = new Firewall($request);
78
        $firewall->configure($this->storage);
79
        $firewall->controlPanel($this->panelUri);
80
81
        // Pass Laravel CSRF Token to Captcha form.
82
        $firewall->getKernel()->setCaptcha(
83
            new Csrf([
84
                'name' => '_token',
85
                'value' => csrf_token(),
0 ignored issues
show
Bug introduced by
The function csrf_token was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

85
                'value' => /** @scrutinizer ignore-call */ csrf_token(),
Loading history...
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);
97
    }
98
}
99