LogoutHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 4
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
rs 10
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 = null,
42
        RememberMeHandler $rememberMeHandler = null,
43
        SessionInterface $session = null
44
    ) {
45
        $this->session = $session;
46
        $this->tokenStorage = $tokenStorage;
47
        $this->csrfTokenStorage = $csrfTokenStorage;
48
        $this->rememberMeHandler = $rememberMeHandler;
49
    }
50
51
    /**
52
     * Handler for:
53
     * - clearing invalidating the current session
54
     * - clearing the token storage
55
     * - clearing the CSRF token storage
56
     * - clearing the remember me cookie if needed.
57
     *
58
     * @return array<int,Cookie> The remember me clearing cookies if any.
59
     */
60
    public function handle(ServerRequestInterface $request): array
61
    {
62
        $this->tokenStorage->setToken();
0 ignored issues
show
Bug introduced by
The call to Symfony\Component\Securi...geInterface::setToken() has too few arguments starting with token. ( Ignorable by Annotation )

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

62
        $this->tokenStorage->/** @scrutinizer ignore-call */ 
63
                             setToken();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
63
64
        if (null !== $this->csrfTokenStorage) {
65
            $this->csrfTokenStorage->clear();
66
        }
67
68
        if (null === $this->session && $request instanceof Request && $request->getRequest()->hasSession()) {
69
            $this->session = $request->getRequest()->getSession();
70
        }
71
72
        if (null !== $this->session) {
73
            $this->session->invalidate();
74
        }
75
76
        if (null !== $this->rememberMeHandler) {
77
            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...
78
        }
79
80
        return null !== $this->rememberMeHandler ? $this->rememberMeHandler->clearRememberMeCookies($request) : [];
81
    }
82
}
83