Passed
Push — main ( 7dcca1...e70bce )
by Daniel
04:22
created

SessionManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 18
c 0
b 0
f 0
dl 0
loc 48
ccs 23
cts 23
cp 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Authentication;
6
7
use Uxmp\Core\Component\User\PasswordVerificatorInterface;
8
use Uxmp\Core\Orm\Model\SessionInterface;
9
use Uxmp\Core\Orm\Repository\SessionRepositoryInterface;
10
use Uxmp\Core\Orm\Repository\UserRepositoryInterface;
11
12
/**
13
 * Handles session lookup and login/logout of users
14
 */
15
final readonly class SessionManager implements SessionManagerInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 15 at column 6
Loading history...
16
{
17 6
    public function __construct(
18
        private SessionRepositoryInterface $sessionRepository,
19
        private UserRepositoryInterface $userRepository,
20
        private PasswordVerificatorInterface $passwordVerificator,
21
    ) {
22 6
    }
23
24 3
    public function lookup(int $sessionId): ?SessionInterface
25
    {
26 3
        return $this->sessionRepository->find($sessionId);
27
    }
28
29 2
    public function logout(int $sessionId): void
30
    {
31 2
        $session = $this->lookup($sessionId);
32 2
        if ($session !== null) {
33 1
            $session->setActive(false);
34
35 1
            $this->sessionRepository->save($session);
36
        }
37
    }
38
39 3
    public function login(
40
        string $username,
41
        string $password
42
    ): ?SessionInterface {
43 3
        $user = $this->userRepository->findOneBy([
44 3
            'name' => $username,
45 3
        ]);
46
47 3
        if ($user === null) {
48 1
            return null;
49
        }
50
51 2
        if (!$this->passwordVerificator->verify($user, $password)) {
52 1
            return null;
53
        }
54
55 1
        $session = $this->sessionRepository
56 1
            ->prototype()
57 1
            ->setActive(true)
58 1
            ->setUser($user);
59
60 1
        $this->sessionRepository->save($session);
61
62 1
        return $session;
63
    }
64
}
65