Passed
Pull Request — master (#2129)
by Nico
23:37 queued 13:36
created

SessionStorage::hasSessionValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 1
b 0
f 0
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 1
    public function __construct(
13
        private readonly UserRepositoryInterface $userRepository,
14
        private readonly SessionInterface $session
15 1
    ) {}
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 1
    #[Override]
50
    public function deleteSessionData(string $key, mixed $value = null): void
51
    {
52 1
        $user = $this->getUserMandatory();
53
54 1
        $data = $user->getSessionDataUnserialized();
55 1
        if (!array_key_exists($key, $data)) {
56 1
            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 2
    #[Override]
74
    public function hasSessionValue(string $key, mixed $value): bool
75
    {
76 2
        $data = $this->getUserMandatory()->getSessionDataUnserialized();
77 2
        if (!array_key_exists($key, $data)) {
78 2
            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 3
    private function getUserMandatory(): UserInterface
97
    {
98 3
        $user = $this->session->getUser();
99 3
        if ($user === null) {
100
            throw new SessionInvalidException("No user logged in");
101
        }
102
103 3
        return $user;
104
    }
105
}
106