Passed
Pull Request — master (#2161)
by Janko
25:16 queued 15:11
created

SessionStorage::getSessionDataUnserialized()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
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
    /** @var array<array<mixed>> */
13
    private array $sessionDataPerUser = [];
14
15 1
    public function __construct(
16
        private readonly UserRepositoryInterface $userRepository,
17
        private readonly SessionInterface $session
18 1
    ) {}
19
20
    /**
21
     * @api
22
     */
23
    #[Override]
24
    public function storeSessionData(string|int $key, mixed $value, bool $isSingleValue = false): void
25
    {
26
        $stored = false;
27
        $user = $this->getUserMandatory();
28
29
        $data = $this->getSessionDataUnserialized($user);
30
        if (!array_key_exists($key, $data)) {
31
            if ($isSingleValue) {
32
                $data[$key] = $value;
33
                $stored = true;
34
            } else {
35
                $data[$key] = [];
36
            }
37
        }
38
        if (!$isSingleValue && !array_key_exists($value, $data[$key])) {
39
            $data[$key][$value] = 1;
40
            $stored = true;
41
        }
42
43
        if ($stored) {
44
            unset($this->sessionDataPerUser[$user->getId()]);
45
            $user->setSessionData(serialize($data));
46
            $this->userRepository->save($user);
47
        }
48
    }
49
50
    /** @return array<mixed> */
51 3
    private function getSessionDataUnserialized(UserInterface $user): array
52
    {
53 3
        if (!array_key_exists($user->getId(), $this->sessionDataPerUser)) {
54 1
            $sessiondataUnserialized = unserialize($user->getSessionData());
55 1
            if (!is_array($sessiondataUnserialized)) {
56 1
                $sessiondataUnserialized = [];
57
            }
58 1
            $this->sessionDataPerUser[$user->getId()] = $sessiondataUnserialized;
59
        }
60
61 3
        return $this->sessionDataPerUser[$user->getId()];
62
    }
63
64
    /**
65
     * @api
66
     */
67 1
    #[Override]
68
    public function deleteSessionData(string $key, mixed $value = null): void
69
    {
70 1
        $user = $this->getUserMandatory();
71
72 1
        $data = $this->getSessionDataUnserialized($user);
73 1
        if (!array_key_exists($key, $data)) {
74 1
            return;
75
        }
76
        if ($value === null) {
77
            unset($data[$key]);
78
        } else {
79
            if (!array_key_exists($value, $data[$key])) {
80
                return;
81
            }
82
            unset($data[$key][$value]);
83
        }
84
        $user->setSessionData(serialize($data));
85
        $this->userRepository->save($user);
86
    }
87
88
    /**
89
     * @api
90
     */
91 2
    #[Override]
92
    public function hasSessionValue(string $key, mixed $value): bool
93
    {
94 2
        $data = $this->getSessionDataUnserialized($this->getUserMandatory());
95 2
        if (!array_key_exists($key, $data)) {
96 2
            return false;
97
        }
98
        return array_key_exists($value, $data[$key]);
99
    }
100
101
    /**
102
     * @api
103
     */
104
    #[Override]
105
    public function getSessionValue(string $key): mixed
106
    {
107
        $data = $this->getSessionDataUnserialized($this->getUserMandatory());
108
        if (!array_key_exists($key, $data)) {
109
            return false;
110
        }
111
        return $data[$key];
112
    }
113
114 3
    private function getUserMandatory(): UserInterface
115
    {
116 3
        $user = $this->session->getUser();
117 3
        if ($user === null) {
118
            throw new SessionInvalidException("No user logged in");
119
        }
120
121 3
        return $user;
122
    }
123
}
124