Passed
Push — master ( 77290b...f3fb78 )
by Marcel
09:09
created

LogoutHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 18
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A logout() 0 12 3
A __construct() 0 1 1
1
<?php
2
3
namespace App\Security\Session;
4
5
use App\Entity\User;
6
use Doctrine\DBAL\Connection;
7
8
/**
9
 * This class helps to logout a user completly by
10
 * (a) removing all active sessions and
11
 * (b) removing all remember me tokens
12
 *
13
 * Caveat: The user is not logged out from applications. This method only prevents the user
14
 * from logging in again after a session is expired.
15
 */
16
class LogoutHelper {
17
18
    public function __construct(private readonly Connection $connection, private readonly ActiveSessionsResolver $activeSessionsResolver) {
19
20
    }
21
22
    public function logout(User $user): void {
23
        // Delete active sessions
24
        $activeSessions = $this->activeSessionsResolver->getSessionsForUser($user);
25
26
        foreach($activeSessions as $activeSession) {
27
            if($activeSession->isCurrentSession === false) {
28
                $this->connection->delete('sessions', ['sess_id' => $activeSession->sessionId]);
29
            }
30
        }
31
32
        // Delete rememberme tokens
33
        $this->connection->delete('rememberme_token', ['username' => $user->getUserIdentifier()]);
34
    }
35
}