| Total Complexity | 7 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | final readonly class SessionManager implements SessionManagerInterface |
||
|
|
|||
| 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 | } |
||
| 65 |