CodeIgniter4::after()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
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
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Integration;
14
15
use Shieldon\Firewall\Firewall;
16
use Shieldon\Firewall\HttpResolver;
17
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...
18
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...
19
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...
20
use Shieldon\Firewall\Captcha\Csrf;
21
use function dirname;
22
use function csrf_token; // CodeIgniter 4
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...
23
use function csrf_hash; // CodeIgniter 4
0 ignored issues
show
introduced by
The function csrf_hash was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
25
/**
26
 * CodeIgniter 4 Middleware of Shieldon Firewall.
27
 */
28
class CodeIgniter4 implements FilterInterface
29
{
30
    /**
31
     * The absolute path of the storage where stores Shieldon generated data.
32
     *
33
     * @var string
34
     */
35
    protected $storage;
36
37
    /**
38
     * The entry point of Shieldon Firewall's control panel.
39
     *
40
     * For example: https://yoursite.com/firewall/panel/
41
     * Just use the path component of a URI.
42
     *
43
     * @var string
44
     */
45
    protected $panelUri;
46
47
    /**
48
     * Constructor.
49
     *
50
     * @param string $storage  See property `storage` explanation.
51
     * @param string $panelUri See property `panelUri` explanation.
52
     *
53
     * @return void
54
     */
55
    public function __construct(string $storage = '', string $panelUri = '')
56
    {
57
        $dir = dirname($_SERVER['SCRIPT_FILENAME']);
58
59
        $this->storage = $dir . '/../writable/shieldon_firewall';
60
        $this->panelUri = '/firewall/panel/';
61
62
        if ('' !== $storage) {
63
            $this->storage = $storage;
64
        }
65
66
        if ('' !== $panelUri) {
67
            $this->panelUri = $panelUri;
68
        }
69
    }
70
71
    /**
72
     * Shieldon middleware invokable class.
73
     *
74
     * @param Request $request
75
     *
76
     * @return mixed
77
     */
78
    public function before(Request $request)
79
    {
80
        if ($request->isCLI()) {
81
            return;
82
        }
83
84
        // CodeIgniter 4 is not a PSR-7 compatible framework, therefore we don't
85
        // pass the Reqest and Reposne to Firewall instance.
86
        // Shieldon will create them by its HTTP factory.
87
        $firewall = new Firewall();
88
        $firewall->configure($this->storage);
89
        $firewall->controlPanel($this->panelUri);
90
91
        // Pass CodeIgniter CSRF Token to Captcha form.
92
        $firewall->getKernel()->setCaptcha(
93
            new Csrf([
94
                '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

94
                'name' => /** @scrutinizer ignore-call */ csrf_token(),
Loading history...
95
                '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

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