Passed
Push — main ( 035e7e...219221 )
by Daniel
03:43
created

SessionManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 47
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A login() 0 24 3
A lookup() 0 3 1
A logout() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Session;
6
7
use Uxmp\Core\Orm\Model\SessionInterface;
8
use Uxmp\Core\Orm\Repository\SessionRepositoryInterface;
9
use Uxmp\Core\Orm\Repository\UserRepositoryInterface;
10
11
final class SessionManager implements SessionManagerInterface
12
{
13 6
    public function __construct(
14
        private SessionRepositoryInterface $sessionRepository,
15
        private UserRepositoryInterface $userRepository
16
    ) {
17 6
    }
18
19 3
    public function lookup(int $sessionId): ?SessionInterface
20
    {
21 3
        return $this->sessionRepository->find($sessionId);
22
    }
23
24 2
    public function logout(int $sessionId): void
25
    {
26 2
        $session = $this->lookup($sessionId);
27 2
        if ($session !== null) {
28 1
            $session->setActive(false);
29
30 1
            $this->sessionRepository->save($session);
31
        }
32 2
    }
33
34 3
    public function login(
35
        string $username,
36
        string $password
37
    ): ?SessionInterface {
38 3
        $user = $this->userRepository->findOneBy([
39 3
            'name' => $username,
40
        ]);
41
42 3
        if ($user === null) {
43 1
            return null;
44
        }
45
46 2
        if (password_verify($password, $user->getPassword()) === false) {
47 1
            return null;
48
        }
49
50 1
        $session = $this->sessionRepository
51 1
            ->prototype()
52 1
            ->setActive(true)
53 1
            ->setUser($user);
54
55 1
        $this->sessionRepository->save($session);
56
57 1
        return $session;
58
    }
59
}
60