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

CodeIgniter4   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 22
c 1
b 0
f 0
dl 0
loc 89
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A after() 0 2 1
A before() 0 26 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 Shieldon\Firewall\Firewall;
17
use Shieldon\Firewall\HttpResolver;
18
use CodeIgniter\HTTP\RequestInterface as Request;
0 ignored issues
show
Bug introduced by
The type CodeIgniter\HTTP\RequestInterface 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...
19
use CodeIgniter\HTTP\ResponseInterface as Response;
0 ignored issues
show
Bug introduced by
The type CodeIgniter\HTTP\ResponseInterface 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...
20
use CodeIgniter\Filters\FilterInterface;
0 ignored issues
show
Bug introduced by
The type CodeIgniter\Filters\FilterInterface 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...
21
use function dirname;
22
23
/**
24
 * CodeIgniter 4 Middleware of Shieldon Firewall.
25
 */
26
class CodeIgniter4 implements FilterInterface
27
{
28
    /**
29
     * The absolute path of the storage where stores Shieldon generated data.
30
     *
31
     * @var string
32
     */
33
    protected $storage;
34
35
    /**
36
     * The entry point of Shieldon Firewall's control panel.
37
     *
38
     * For example: https://yoursite.com/firewall/panel/
39
     * Just use the path component of a URI.
40
     *
41
     * @var string
42
     */
43
    protected $panelUri;
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param string $storage  See property `storage` explanation.
49
     * @param string $panelUri See property `panelUri` explanation.
50
     *
51
     * @return void
52
     */
53
    public function __construct(string $storage = '', string $panelUri = '')
54
    {
55
        $dir = dirname($_SERVER['SCRIPT_FILENAME']);
56
57
        $this->storage = $dir . '/../writable/shieldon_firewall';
58
        $this->panelUri = '/firewall/panel/';
59
60
        if ('' !== $storage) {
61
            $this->storage = $storage;
62
        }
63
64
        if ('' !== $panelUri) {
65
            $this->panelUri = $panelUri;
66
        }
67
    }
68
69
	/**
70
     * Shieldon middleware invokable class.
71
     *
72
	 * @param Request $request
73
	 *
74
	 * @return mixed
75
	 */
76
	public function before(Request $request)
77
	{
78
		if ($request->isCLI()) {
79
			return;
80
		}
81
82
        // CodeIgniter 4 is not a PSR-7 compatible framework, therefore we don't 
83
        // pass the Reqest and Reposne to Firewall instance.
84
        // Shieldon will create them by its HTTP factory.
85
        $firewall = new Firewall();
86
        $firewall->configure($this->storage);
87
        $firewall->controlPanel($this->panelUri);
88
89
        // Pass CodeIgniter CSRF Token to Captcha form.
90
        $firewall->getKernel()->setCaptcha(
91
            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...
92
                'name' => 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

92
                'name' => /** @scrutinizer ignore-call */ csrf_token(),
Loading history...
93
                'value' => csrf_hash(),
0 ignored issues
show
Bug introduced by
The function csrf_hash 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

93
                'value' => /** @scrutinizer ignore-call */ csrf_hash(),
Loading history...
94
            ])
95
        );
96
97
        $response = $firewall->run();
98
99
        if ($response->getStatusCode() !== 200) {
100
            $httpResolver = new HttpResolver();
101
            $httpResolver($response);
102
        }
103
	}
104
105
	/**
106
	 * We don't have anything to do here.
107
	 *
108
	 * @param Response $request
109
	 * @param Response $response
110
	 *
111
	 * @return mixed
112
	 */
113
	public function after(Request $request, Response $response)
114
	{
115
116
    }
117
}