Passed
Push — 4.1 ( 6d2665...02ad0f )
by
unknown
32:36 queued 24:09
created

ErrorDirector::handleRequestWithTokenChain()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 1
nop 3
dl 0
loc 20
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\Startup;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Control\HTTPResponse;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Core\Kernel;
10
use SilverStripe\Security\Permission;
11
use SilverStripe\Security\Security;
12
13
/**
14
 * Specialised Director class used by ErrorControlChain to handle error and redirect conditions
15
 *
16
 * @internal This class is experimental API and may change without warning
17
 */
18
class ErrorDirector extends Director
19
{
20
    /**
21
     * Redirect with token if allowed, or null if not allowed
22
     *
23
     * @param HTTPRequest $request
24
     * @param ConfirmationTokenChain $confirmationTokenChain
25
     * @param Kernel $kernel
26
     * @return null|HTTPResponse Redirection response, or null if not able to redirect
27
     */
28
    public function handleRequestWithTokenChain(
29
        HTTPRequest $request,
30
        ConfirmationTokenChain $confirmationTokenChain,
31
        Kernel $kernel
32
    ) {
33
        Injector::inst()->registerService($request, HTTPRequest::class);
34
35
        // Next, check if we're in dev mode, or the database doesn't have any security data, or we are admin
36
        $reload = function (HTTPRequest $request) use ($confirmationTokenChain, $kernel) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

36
        $reload = function (/** @scrutinizer ignore-unused */ HTTPRequest $request) use ($confirmationTokenChain, $kernel) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
            if ($kernel->getEnvironment() === Kernel::DEV || !Security::database_is_ready() || Permission::check('ADMIN')) {
38
                return $confirmationTokenChain->reloadWithTokens();
39
            }
40
            return null;
41
        };
42
43
        try {
44
            return $this->callMiddleware($request, $reload);
45
        } finally {
46
            // Ensure registered request is un-registered
47
            Injector::inst()->unregisterNamedObject(HTTPRequest::class);
48
        }
49
    }
50
}
51