Completed
Pull Request — master (#7007)
by Simon
08:19
created

LogoutHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A logout() 0 6 1
A doLogOut() 0 8 2
1
<?php
2
3
namespace SilverStripe\Security\MemberAuthenticator;
4
5
use SilverStripe\Control\RequestHandler;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Security\IdentityStore;
8
use SilverStripe\Security\Member;
9
use SilverStripe\Security\Security;
10
11
/**
12
 * Class LogoutHandler handles logging out Members from their session and/or cookie.
13
 * The logout process destroys all traces of the member on the server (not the actual computer user
14
 * at the other end of the line, don't worry)
15
 *
16
 */
17
class LogoutHandler extends RequestHandler
18
{
19
    /**
20
     * @var array
21
     */
22
    private static $url_handlers = [
23
        '' => 'logout'
24
    ];
25
26
    /**
27
     * @var array
28
     */
29
    private static $allowed_actions = [
30
        'logout'
31
    ];
32
33
34
    /**
35
     * Log out form handler method
36
     *
37
     * This method is called when the user clicks on "logout" on the form
38
     * created when the parameter <i>$checkCurrentUser</i> of the
39
     * {@link __construct constructor} was set to TRUE and the user was
40
     * currently logged in.
41
     *
42
     * @return bool|Member
43
     */
44
    public function logout()
45
    {
46
        $member = Security::getCurrentUser();
47
48
        return $this->doLogOut($member);
0 ignored issues
show
Bug introduced by
It seems like $member defined by \SilverStripe\Security\Security::getCurrentUser() on line 46 can be null; however, SilverStripe\Security\Me...goutHandler::doLogOut() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
49
    }
50
51
    /**
52
     *
53
     * @param Member $member
54
     * @return bool|Member Return a member if something goes wrong
55
     */
56
    public function doLogOut($member)
57
    {
58
        if ($member instanceof Member) {
59
            Injector::inst()->get(IdentityStore::class)->logOut($this->getRequest());
60
        }
61
62
        return true;
63
    }
64
}
65