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