Total Complexity | 6 |
Total Lines | 61 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
14 | class PhpSession implements SessionInterface |
||
15 | { |
||
16 | use GetInfoTrait; |
||
17 | |||
18 | protected string $key; |
||
19 | |||
20 | /** |
||
21 | * Service constructor. |
||
22 | * |
||
23 | * @param string $key |
||
24 | */ |
||
25 | 9 | public function __construct(string $key = 'auth') |
|
26 | { |
||
27 | 9 | $this->key = $key; |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * Assert that there is an active session. |
||
32 | * |
||
33 | * @throws RuntimeException if there is no active session |
||
34 | */ |
||
35 | 9 | protected function assertSessionStarted(): void |
|
36 | { |
||
37 | 9 | if (session_status() !== PHP_SESSION_ACTIVE) { |
|
38 | 3 | throw new RuntimeException("Unable to use session for auth info: Session not started"); |
|
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @inheritDoc |
||
44 | */ |
||
45 | 5 | public function getInfo(): array |
|
46 | { |
||
47 | 5 | $this->assertSessionStarted(); |
|
48 | |||
49 | 4 | return $this->getInfoFromData($_SESSION[$this->key] ?? []); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @inheritDoc |
||
54 | */ |
||
55 | 2 | public function persist(mixed $userId, mixed $contextId, ?string $checksum, ?DateTimeInterface $timestamp): void |
|
56 | { |
||
57 | 2 | $this->assertSessionStarted(); |
|
58 | |||
59 | 1 | $_SESSION[$this->key] = [ |
|
60 | 1 | 'user' => $userId, |
|
61 | 1 | 'context' => $contextId, |
|
62 | 1 | 'checksum' => $checksum, |
|
63 | 1 | 'timestamp' => $timestamp?->getTimestamp(), |
|
64 | 1 | ]; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @inheritDoc |
||
69 | */ |
||
70 | 2 | public function clear(): void |
|
75 | } |
||
76 | } |
||
77 |