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

SessionStorage::getSessionValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Stu\Lib\Session;
4
5
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...
6
use Stu\Exception\SessionInvalidException;
7
use Stu\Orm\Entity\UserInterface;
8
use Stu\Orm\Repository\UserRepositoryInterface;
9
10
final class SessionStorage implements SessionStorageInterface
11
{
12
    public function __construct(
13
        private readonly UserRepositoryInterface $userRepository,
14
        private readonly SessionInterface $session
15
    ) {}
16
17
    /**
18
     * @api
19
     */
20
    #[Override]
21
    public function storeSessionData(string|int $key, mixed $value, bool $isSingleValue = false): void
22
    {
23
        $stored = false;
24
        $user = $this->getUserMandatory();
25
26
        $data = $user->getSessionDataUnserialized();
27
        if (!array_key_exists($key, $data)) {
28
            if ($isSingleValue) {
29
                $data[$key] = $value;
30
                $stored = true;
31
            } else {
32
                $data[$key] = [];
33
            }
34
        }
35
        if (!$isSingleValue && !array_key_exists($value, $data[$key])) {
36
            $data[$key][$value] = 1;
37
            $stored = true;
38
        }
39
40
        if ($stored) {
41
            $user->setSessionData(serialize($data));
42
            $this->userRepository->save($user);
43
        }
44
    }
45
46
    /**
47
     * @api
48
     */
49
    #[Override]
50
    public function deleteSessionData(string $key, mixed $value = null): void
51
    {
52
        $user = $this->getUserMandatory();
53
54
        $data = $user->getSessionDataUnserialized();
55
        if (!array_key_exists($key, $data)) {
56
            return;
57
        }
58
        if ($value === null) {
59
            unset($data[$key]);
60
        } else {
61
            if (!array_key_exists($value, $data[$key])) {
62
                return;
63
            }
64
            unset($data[$key][$value]);
65
        }
66
        $user->setSessionData(serialize($data));
67
        $this->userRepository->save($user);
68
    }
69
70
    /**
71
     * @api
72
     */
73
    #[Override]
74
    public function hasSessionValue(string $key, mixed $value): bool
75
    {
76
        $data = $this->getUserMandatory()->getSessionDataUnserialized();
77
        if (!array_key_exists($key, $data)) {
78
            return false;
79
        }
80
        return array_key_exists($value, $data[$key]);
81
    }
82
83
    /**
84
     * @api
85
     */
86
    #[Override]
87
    public function getSessionValue(string $key): mixed
88
    {
89
        $data = $this->getUserMandatory()->getSessionDataUnserialized();
90
        if (!array_key_exists($key, $data)) {
91
            return false;
92
        }
93
        return $data[$key];
94
    }
95
96
    private function getUserMandatory(): UserInterface
97
    {
98
        $user = $this->session->getUser();
99
        if ($user === null) {
100
            throw new SessionInvalidException("No user logged in");
101
        }
102
103
        return $user;
104
    }
105
}
106