Passed
Push — master ( 279881...b4f44a )
by Divine Niiquaye
10:48
created

LogoutHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 47
ccs 0
cts 15
cp 0
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B handle() 0 18 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 */
18
19
namespace Biurad\Security\Handler;
20
21
use Biurad\Http\Request;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Symfony\Component\HttpFoundation\Session\SessionInterface;
24
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
25
use Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface;
26
27
/**
28
 * The default logout handler.
29
 *
30
 * @author Divine Niiquaye Ibok <[email protected]>
31
 */
32
class LogoutHandler
33
{
34
    private TokenStorageInterface $tokenStorage;
35
    private ClearableTokenStorageInterface $csrfTokenStorage;
36
    private ?SessionInterface $session;
37
    private ?RememberMeHandler $rememberMeHandler;
38
39
    public function __construct(
40
        TokenStorageInterface $tokenStorage,
41
        ClearableTokenStorageInterface $csrfTokenStorage,
42
        RememberMeHandler $rememberMeHandler = null,
43
        SessionInterface $session = null
44
    )
45
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
46
        $this->session = $session;
47
        $this->tokenStorage = $tokenStorage;
48
        $this->csrfTokenStorage = $csrfTokenStorage;
49
        $this->rememberMeHandler = $rememberMeHandler;
50
    }
51
52
    /**
53
     * Handler for:
54
     * - clearing invalidating the current session
55
     * - clearing the token storage
56
     * - clearing the CSRF token storage
57
     * - clearing the remember me cookie if needed.
58
     * 
59
     * @return array<int,Cookie> The remember me clearing cookies if any.
60
     */
61
    public function handle(ServerRequestInterface $request): array
62
    {
63
        $this->tokenStorage->setToken();
64
        $this->csrfTokenStorage->clear();
65
66
        if (null === $this->session && $request instanceof Request && $request->getRequest()->hasSession()) {
67
            $this->session = $request->getRequest()->getSession();
68
        }
69
70
        if (null !== $this->session) {
71
            $this->session->invalidate();
72
        }
73
74
        if (null !== $this->rememberMeHandler) {
75
            return $this->rememberMeHandler->clearRememberMeCookies($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->rememberMe...mberMeCookies($request) returns an array which contains values of type Symfony\Component\HttpFoundation\Cookie which are incompatible with the documented value type Biurad\Security\Handler\Cookie.
Loading history...
76
        }
77
78
        return null !== $this->rememberMeHandler ? $this->rememberMeHandler->clearRememberMeCookies($request) : [];
79
    }
80
}
81