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

SessionManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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