Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.06% covered (success)
97.06%
33 / 34
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SessionHandler
97.06% covered (success)
97.06%
33 / 34
87.50% covered (warning)
87.50%
7 / 8
19
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%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 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    #[\Override]
48    public function open(string $path, string $name): bool
49    {
50        $this->sessionName = $name;
51        return true;
52    }
53
54    #[\Override]
55    public function close(): bool
56    {
57        return true;
58    }
59
60    #[\Override]
61    public function read(string $id, array $params = []): string
62    {
63        $hashedSessionId = hash('sha256', $id);
64        $params['sync'] = static::$useSyncFlag;
65        try {
66            $session = $this->http->readGetResult(
67                '/session/' . $this->sessionName . '/' . $hashedSessionId . '/',
68                $params
69            )
70            ->getEntity();
71        } catch (Exception\ApiFailed $exception) {
72            throw $exception;
73        } catch (Exception $exception) {
74            if ($exception->getCode() == 404) {
75                $session = false;
76            } else {
77                throw $exception;
78            }
79        }
80        if (isset($params['oidc']) && 1 == $params['oidc'] && $session) {
81            $session = $session->withOidcDataOnly();
82        }
83        return ($session && isset($session['content'])) ? serialize($session->getContent()) : '';
84    }
85
86    #[\Override]
87    public function write(string $id, string $data, array $params = []): bool
88    {
89        $hashedSessionId = hash('sha256', $id);
90        $entity = new \BO\Zmsentities\Session();
91        $entity->id = $hashedSessionId;
92        $entity->name = $this->sessionName;
93        $entity->content = unserialize($data);
94
95        $session = $this->http->readPostResult('/session/', $entity, $params)
96            ->getEntity();
97
98        return (null !== $session) ? true : false;
99    }
100
101    #[\Override]
102    public function destroy(string $id): bool
103    {
104        $hashedSessionId = hash('sha256', $id);
105        $result = $this->http->readDeleteResult('/session/' . $this->sessionName . '/' . $hashedSessionId . '/');
106        return ($result) ? true : false;
107    }
108
109    /**
110     * @SuppressWarnings(UnusedFormalParameter)
111     * @SuppressWarnings(ShortMethodName)
112     * @codeCoverageIgnore
113     */
114    #[\Override]
115    public function gc(int $max_lifetime): int|false
116    {
117        /*
118         * $compareTs = time() - $max_lifetime;
119         * $query = '
120         * DELETE FROM
121         * sessiondata
122         * WHERE
123         * UNIX_TIMESTAMP(`ts`) < ? AND
124         * sessionname=?
125         * ';
126         * $statement = $this->getWriter()->prepare($query);
127         * return $statement->execute(array(
128         * $compareTs,
129         * $this->sessionName
130         * ));
131         */
132        return 1;
133    }
134}