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

CakePhp::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
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 23
rs 9.8666
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 const TMP;
0 ignored issues
show
Bug introduced by
The constant TMP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
22
/**
23
 * CakePHP Middleware
24
 *
25
 * This middleware has been tested succesfully with CakePHP 3.8
26
 */
27
class CakePhp
28
{
29
    /**
30
     * The absolute path of the storage where stores Shieldon generated data.
31
     *
32
     * @var string
33
     */
34
    protected $storage;
35
36
    /**
37
     * The entry point of Shieldon Firewall's control panel.
38
     *
39
     * For example: https://yoursite.com/firewall/panel/
40
     * Just use the path component of a URI.
41
     *
42
     * @var string
43
     */
44
    protected $panelUri;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param string $storage  See property `storage` explanation.
50
     * @param string $panelUri See property `panelUri` explanation.
51
     * 
52
     * @return void
53
     */
54
    public function __construct(string $storage = '', string $panelUri = '')
55
    {
56
        // The constant TMP is the path of CakePHP's tmp folder.
57
        // The Shieldon generated data is stored at that place.
58
        $this->storage = TMP . 'shieldon_firewall';
0 ignored issues
show
Bug introduced by
The constant TMP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
59
        $this->panelUri = '/firewall/panel/';
60
61
        if ('' !== $storage) {
62
            $this->storage = $storage;
63
        }
64
65
        if ('' !== $panelUri) {
66
            $this->panelUri = $panelUri;
67
        }
68
    }
69
70
    /**
71
     * Middleware invokable class.
72
     *
73
     * @param Request  $request  PSR7 request
74
     * @param Response $response PSR7 response
75
     * @param callable $next     Next middleware
76
     *
77
     * @return Response
78
     */
79
    public function __invoke(Request $request, Response $response, $next): Response
80
    {
81
        $firewall = new Firewall($request, $response);
82
        $firewall->configure($this->storage);
83
        $firewall->controlPanel($this->panelUri);
84
85
        // Pass CSRF token to the Captcha form.
86
        // Note: The CsrfProtectionMiddleware was added in 3.5.0
87
        $firewall->getKernel()->setCaptcha(
88
            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...
89
                'name' => '_csrfToken',
90
                'value' => $request->getParam('_csrfToken'),
0 ignored issues
show
Bug introduced by
The method getParam() does not exist on Psr\Http\Message\ServerRequestInterface. ( Ignorable by Annotation )

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

90
                'value' => $request->/** @scrutinizer ignore-call */ getParam('_csrfToken'),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
            ])
92
        );
93
94
        $response = $firewall->run();
95
96
        if ($response->getStatusCode() !== 200) {
97
            $httpResolver = new HttpResolver();
98
            $httpResolver($response);
99
        }
100
101
        return $next($request, $response);
102
    }
103
}
104