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

Laravel::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
rs 9.8666
cc 2
nc 2
nop 2
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\ServerRequestInterface as Request;
17
use Shieldon\Firewall\Firewall;
18
use Shieldon\Firewall\HttpResolver;
19
use function storage_path;
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
21
/**
22
 * Middleware for Laravel framework (5.x - 6.x)
23
 */
24
class Laravel
25
{
26
    /**
27
     * The absolute path of the storage where stores Shieldon generated data.
28
     *
29
     * @var string
30
     */
31
    protected $storage;
32
33
    /**
34
     * The entry point of Shieldon Firewall's control panel.
35
     *
36
     * For example: https://yoursite.com/firewall/panel/
37
     * Just use the path component of a URI.
38
     *
39
     * @var string
40
     */
41
    protected $panelUri;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param string $storage  See property `storage` explanation.
47
     * @param string $panelUri See property `panelUri` explanation.
48
     *
49
     * @return void
50
     */
51
    public function __construct(string $storage = '', string $panelUri = '')
52
    {
53
        // The Shieldon generated data is stored at that place.
54
        $this->storage = storage_path('shieldon');
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

54
        $this->storage = /** @scrutinizer ignore-call */ storage_path('shieldon');
Loading history...
55
        $this->panelUri = '/firewall/panel/';
56
57
        if ('' !== $storage) {
58
            $this->storage = $storage;
59
        }
60
61
        if ('' !== $panelUri) {
62
            $this->panelUri = $panelUri;
63
        }
64
    }
65
66
    /**
67
     * Handle an incoming request.
68
     *
69
     * @param Request $request
70
     * @param Closure $next
71
     * @return mixed
72
     */
73
    public function handle(Request $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...
74
    {
75
        $firewall = new Firewall($request);
76
        $firewall->configure($this->storage);
77
        $firewall->controlPanel($this->panelUri);
78
79
        // Pass Laravel CSRF Token to Captcha form.
80
        $firewall->getKernel()->setCaptcha(
81
            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...
82
                'name' => '_token',
83
                '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

83
                'value' => /** @scrutinizer ignore-call */ csrf_token(),
Loading history...
84
            ])
85
        );
86
87
        $response = $firewall->run();
88
89
        if ($response->getStatusCode() !== 200) {
90
            $httpResolver = new HttpResolver();
91
            $httpResolver($response);
92
        }
93
94
        return $next($request);
95
    }
96
}
97