Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
45.83% covered (danger)
45.83%
11 / 24
20.00% covered (danger)
20.00%
3 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
SessionContainer
45.83% covered (danger)
45.83%
11 / 24
20.00% covered (danger)
20.00%
3 / 15
69.49
0.00% covered (danger)
0.00%
0 / 1
 fromContainer
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 writeData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEntity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 remove
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 clear
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 restart
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 clearGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 has
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isEmpty
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSession
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 loadSession
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
1<?php
2
3namespace BO\Slim\Middleware\Session;
4
5class SessionContainer implements SessionInterface
6{
7    private ?SessionData $sessionContainer = null;
8
9    /** @var callable */
10    private mixed $sessionLoader = null;
11
12    public static function fromContainer(callable $sessionLoader): static
13    {
14        $instance = new static();
15        $instance->sessionLoader = $sessionLoader;
16        return $instance;
17    }
18
19    /**
20     * @return void
21     */
22    #[\Override]
23    public function setGroup(array $group, bool $clear = false): void
24    {
25        $this->getSession()->setGroup($group, $clear);
26    }
27
28    /** @psalm-api */
29    public function writeData(): void
30    {
31        $this->getSession()->writeData();
32    }
33
34    /**
35     * @return void
36     */
37    #[\Override]
38    public function set(int|string $key, mixed $value, int|string|null $groupIndex = null): void
39    {
40        $this->getSession()->set($key, $value, $groupIndex);
41    }
42
43    #[\Override]
44    public function get(int|string $key, int|string|null $groupIndex = null, mixed $default = null): mixed
45    {
46        return $this->getSession()->get($key, $groupIndex, $default);
47    }
48
49    #[\Override]
50    public function getEntity(): mixed
51    {
52        return $this->getSession()->getEntity();
53    }
54
55    /**
56     * @return void
57     */
58    #[\Override]
59    public function remove(int|string $key, int|string|null $groupIndex = null): void
60    {
61        $this->getSession()->remove($key, $groupIndex);
62    }
63
64    /**
65     * @return void
66     */
67    #[\Override]
68    public function clear(): void
69    {
70        $this->getSession()->clear();
71    }
72
73    /** @psalm-api */
74    public function restart(): void
75    {
76        $this->getSession()->restart();
77    }
78
79    /**
80     * @return void
81     */
82    #[\Override]
83    public function clearGroup(int|string|null $groupIndex = null): void
84    {
85        $this->getSession()->clearGroup($groupIndex);
86    }
87
88    #[\Override]
89    public function has(int|string $key, int|string|null $groupIndex = null): ?bool
90    {
91        return $this->getSession()->has($key, $groupIndex);
92    }
93
94    #[\Override]
95    public function isEmpty(): bool
96    {
97        return $this->getSession()->isEmpty();
98    }
99
100    #[\Override]
101    public function jsonSerialize(): mixed
102    {
103        return $this->getSession()->jsonSerialize();
104    }
105
106    private function getSession(): SessionData
107    {
108        if (!$this->sessionContainer) {
109            $this->sessionContainer = $this->loadSession();
110        }
111        return $this->sessionContainer;
112    }
113
114    private function loadSession(): SessionData
115    {
116        if (!is_callable($this->sessionLoader)) {
117            throw new \RuntimeException('Session loader is not set');
118        }
119        $session = ($this->sessionLoader)();
120        if (!$session instanceof SessionData) {
121            throw new \RuntimeException('Session loader must return SessionData');
122        }
123        return $session;
124    }
125}