Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.37% covered (success)
97.37%
37 / 38
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SessionHandler
97.37% covered (success)
97.37%
37 / 38
87.50% covered (warning)
87.50%
7 / 8
21
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLastInstance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setHttpHandler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 open
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 close
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 read
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
9.02
 write
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 destroy
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 gc
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3namespace BO\Zmsclient;
4
5/**
6 * Session handler for mysql
7 */
8class SessionHandler implements \SessionHandlerInterface
9{
10    public $sessionName;
11
12    /**
13     * Adds a parameter "sync" on reading the session from the API
14     * Use a value of 1 to enable synchronous reads
15     * if a former session write happened during a redirect
16     */
17    public static $useSyncFlag = 0;
18
19    protected static $lastInstance = null;
20
21    /**
22     * @var \BO\Zmsclient\Http $http
23     *
24     */
25    protected $http = null;
26
27
28    public function __construct(Http $http)
29    {
30        $this->setHttpHandler($http);
31        static::$lastInstance = $this;
32    }
33
34    public static function getLastInstance(): ?self
35    {
36        return static::$lastInstance;
37    }
38
39    public function setHttpHandler(Http $http): void
40    {
41        $this->http = $http;
42    }
43
44    /**
45     * @SuppressWarnings(UnusedFormalParameter)
46     */
47    public function open(string $save_path, string $name): bool
48    {
49        $this->sessionName = $name;
50        return true;
51    }
52
53    public function close(): bool
54    {
55        return true;
56    }
57
58    public function read(string $sessionId, array $params = []): string
59    {
60        $hashedSessionId = hash('sha256', $sessionId);
61        $params['sync'] = static::$useSyncFlag;
62        try {
63            $session = $this->http->readGetResult(
64                '/session/' . $this->sessionName . '/' . $hashedSessionId . '/',
65                $params
66            )
67            ->getEntity();
68        } catch (Exception\ApiFailed $exception) {
69            throw $exception;
70        } catch (Exception $exception) {
71            if ($exception->getCode() == 404) {
72                $session = false;
73            } else {
74                throw $exception;
75            }
76        }
77        if (isset($params['oidc']) && 1 == $params['oidc'] && $session) {
78            $session = $session->withOidcDataOnly();
79        }
80        return ($session && isset($session['content'])) ? serialize($session->getContent()) : '';
81    }
82
83    public function write(string $sessionId, string $sessionData, array $params = []): bool
84    {
85        $hashedSessionId = hash('sha256', $sessionId);
86        $entity = new \BO\Zmsentities\Session();
87        $entity->id = $hashedSessionId;
88        $entity->name = $this->sessionName;
89        $entity->content = unserialize($sessionData);
90
91        try {
92            $session = $this->http->readPostResult('/session/', $entity, $params)
93                ->getEntity();
94        } catch (Exception $exception) {
95            if ($exception->getCode() == 404) {
96                $session = null;
97            }
98            throw $exception;
99        }
100
101        return (null !== $session) ? true : false;
102    }
103
104    public function destroy(string $sessionId): bool
105    {
106        $hashedSessionId = hash('sha256', $sessionId);
107        $result = $this->http->readDeleteResult('/session/' . $this->sessionName . '/' . $hashedSessionId . '/');
108        return ($result) ? true : false;
109    }
110
111    /**
112     * @SuppressWarnings(UnusedFormalParameter)
113     * @SuppressWarnings(ShortMethodName)
114     * @codeCoverageIgnore
115     */
116    public function gc(int $max_lifetime): int|false
117    {
118        /*
119         * $compareTs = time() - $max_lifetime;
120         * $query = '
121         * DELETE FROM
122         * sessiondata
123         * WHERE
124         * UNIX_TIMESTAMP(`ts`) < ? AND
125         * sessionname=?
126         * ';
127         * $statement = $this->getWriter()->prepare($query);
128         * return $statement->execute(array(
129         * $compareTs,
130         * $this->sessionName
131         * ));
132         */
133        return 1;
134    }
135}