Test Failed
Push — dev ( fdf488...077c6d )
by Janko
14:47
created

Session   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 6.67%

Importance

Changes 0
Metric Value
eloc 38
c 0
b 0
f 0
dl 0
loc 88
rs 10
ccs 2
cts 30
cp 0.0667
wmc 20

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUser() 0 6 1
A __construct() 0 5 1
A chklogin() 0 33 5
A getUser() 0 4 1
B createSession() 0 12 8
A logout() 0 4 1
A isLoggedIn() 0 5 3
1
<?php
2
3
namespace Stu\Lib\Session;
4
5
use DateTime;
6
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Stu\Exception\SessionInvalidException;
8
use Stu\Orm\Entity\UserInterface;
9
use Stu\Orm\Repository\UserIpTableRepositoryInterface;
10
use Stu\Orm\Repository\UserRepositoryInterface;
11
12
final class Session implements SessionInterface
13
{
14
    private ?UserInterface $user = null;
15
16
    public function __construct(
17
        private readonly UserIpTableRepositoryInterface $userIpTableRepository,
18
        private readonly UserRepositoryInterface $userRepository,
19
        private readonly SessionDestructionInterface $sessionDestruction
20
    ) {}
21
22
    #[Override]
23
    public function createSession(bool $session_check = true): void
24
    {
25
        if (!$this->isLoggedIn() && $session_check) {
26
            throw new SessionInvalidException('Session abgelaufen');
27
        }
28 2
        if ($session_check && (!$_SESSION['uid'] || !$_SESSION['login'])) {
29
            $this->logout();
30
            return;
31
        }
32
        if ($this->isLoggedIn() && $session_check) {
33
            $this->chklogin();
34
        }
35 2
    }
36
37
    private function isLoggedIn(): bool
38
    {
39
        return array_key_exists('uid', $_SESSION)
40
            && array_key_exists('login', $_SESSION)
41
            && $_SESSION['login'] == 1;
42
    }
43
44
    /**
45
     * @api
46
     */
47
    #[Override]
48
    public function getUser(): ?UserInterface
49
    {
50
        return $this->user;
51
    }
52
53
    #[Override]
54
    public function setUser(?UserInterface $user): SessionInterface
55
    {
56
        $this->user = $user;
57
58
        return $this;
59
    }
60
61
    #[Override]
62
    public function logout(?UserInterface $user = null): void
63
    {
64
        $this->sessionDestruction->destroySession($this, $user);
65
    }
66
67
    private function chklogin(): void
68
    {
69
        if (!$this->isLoggedIn()) {
70
            throw new SessionInvalidException("Not logged in");
71
        }
72
73
        $userId = (int) $_SESSION['uid'];
74
75
        $user = $this->userRepository->find($userId);
76
77
        if ($user === null) {
78
            $this->logout();
79
            return;
80
        }
81
        $user->setLastaction(time());
82
83
        $this->userRepository->save($user);
84
85
        $sessionId = session_id();
86
        if (!$sessionId) {
87
            throw new SessionInvalidException("Session Id not set");
88
        }
89
90
        $ipTableEntry = $this->userIpTableRepository->findBySessionId($sessionId);
91
        if ($ipTableEntry !== null) {
92
            $ipTableEntry->setEndDate(new DateTime());
93
94
            $this->userIpTableRepository->save($ipTableEntry);
95
        }
96
97
        $_SESSION['login'] = 1;
98
99
        $this->user = $user;
100
    }
101
}
102