Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
54.76% covered (warning)
54.76%
23 / 42
33.33% covered (danger)
33.33%
3 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Session
54.76% covered (warning)
54.76%
23 / 42
33.33% covered (danger)
33.33%
3 / 9
71.97
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 resetConfigure
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 has
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 get
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
4.37
 set
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 delete
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 clear
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getCurrentRequest
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getSession
50.00% covered (danger)
50.00%
6 / 12
0.00% covered (danger)
0.00%
0 / 1
10.50
1<?php
2
3namespace Olz\Common\Utils;
4
5use Symfony\Component\HttpFoundation\Request;
6use Symfony\Component\HttpFoundation\RequestStack;
7use Symfony\Component\HttpFoundation\Session\SessionInterface;
8
9class Session extends AbstractSession {
10    public function __construct(
11        protected ?RequestStack $requestStack = null,
12    ) {
13    }
14
15    /** @param array{timeout?: int} $config */
16    public function resetConfigure(array $config): void {
17        // Only called on login: this is the only place a session is started.
18        $timeout = $config['timeout'] ?? 43200; // keep 12 hours
19        $session = $this->getCurrentRequest()->getSession();
20        if (!$session->isStarted()) {
21            $session->start();
22        }
23        $session->invalidate($timeout);
24    }
25
26    public function has(string $key): bool {
27        $session = $this->getSession();
28        return $session !== null && $session->has($key);
29    }
30
31    public function get(string $key): ?string {
32        $session = $this->getSession();
33        if ($session === null) {
34            return null;
35        }
36        try {
37            $value = $session->get($key);
38        } catch (\Throwable $th) {
39            // The session could not be read (e.g. the session store being
40            // unavailable). Treat it as if the attribute were not set, instead
41            // of blowing up the current operation.
42            return null;
43        }
44        return is_scalar($value) ? "{$value}" : null;
45    }
46
47    public function set(string $key, ?string $new_value): void {
48        $session = $this->getSession();
49        if ($session === null) {
50            return;
51        }
52        $session->set($key, $new_value);
53    }
54
55    public function delete(string $key): void {
56        $session = $this->getSession();
57        if ($session === null) {
58            return;
59        }
60        $session->remove($key);
61    }
62
63    public function clear(): void {
64        $session = $this->getSession();
65        if ($session !== null) {
66            $session->invalidate();
67        }
68    }
69
70    protected function getCurrentRequest(): Request {
71        $request = $this->requestStack?->getCurrentRequest();
72        if ($request === null) {
73            throw new \LogicException('Cannot access the session without a current request.');
74        }
75        return $request;
76    }
77
78    /**
79     * Returns the session if it is already started, or resumes it from a session
80     * cookie when one is present. Never starts a fresh session (that only happens
81     * on login), and treats a malformed session cookie as "no session".
82     */
83    protected function getSession(): ?SessionInterface {
84        $request = $this->requestStack?->getCurrentRequest();
85        if ($request === null || !$request->hasSession()) {
86            return null;
87        }
88        $session = $request->getSession();
89        if ($session->isStarted()) {
90            return $session;
91        }
92        if (!$request->hasPreviousSession()) {
93            return null;
94        }
95        try {
96            $session->start();
97        } catch (\Throwable $th) {
98            return null;
99        }
100        return $session;
101    }
102}