Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.00% covered (warning)
70.00%
7 / 10
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Database
70.00% covered (warning)
70.00%
7 / 10
80.00% covered (warning)
80.00%
4 / 5
6.97
0.00% covered (danger)
0.00%
0 / 1
 read
25.00% covered (danger)
25.00%
1 / 4
0.00% covered (danger)
0.00%
0 / 1
3.69
 destroy
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 gc
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 saveSession
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSessionService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Database session handler
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010-2024.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Session_Handlers
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:session_handlers Wiki
28 */
29
30namespace VuFind\Session;
31
32use VuFind\Db\Service\SessionServiceInterface;
33use VuFind\Exception\SessionExpired as SessionExpiredException;
34
35/**
36 * Database session handler
37 *
38 * @category VuFind
39 * @package  Session_Handlers
40 * @author   Demian Katz <demian.katz@villanova.edu>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development:plugins:session_handlers Wiki
43 */
44class Database extends AbstractBase
45{
46    /**
47     * Read function must return string value always to make save handler work as
48     * expected. Return empty string if there is no data to read.
49     *
50     * @param string $sessId The session ID to read
51     *
52     * @return string
53     */
54    public function read($sessId): string
55    {
56        // Try to read the session, but destroy it if it has expired:
57        try {
58            return $this->getSessionService()->readSession($sessId, $this->lifetime);
59        } catch (SessionExpiredException $e) {
60            $this->destroy($sessId);
61            return '';
62        }
63    }
64
65    /**
66     * The destroy handler, this is executed when a session is destroyed with
67     * session_destroy() and takes the session id as its only parameter.
68     *
69     * @param string $sessId The session ID to destroy
70     *
71     * @return bool
72     */
73    public function destroy($sessId): bool
74    {
75        // Perform standard actions required by all session methods:
76        parent::destroy($sessId);
77
78        // Now do database-specific destruction:
79        $this->getSessionService()->destroySession($sessId);
80
81        return true;
82    }
83
84    /**
85     * The garbage collector, this is executed when the session garbage collector
86     * is executed and takes the max session lifetime as its only parameter.
87     *
88     * @param int $sessMaxLifetime Maximum session lifetime.
89     *
90     * @return int|false
91     */
92    public function gc($sessMaxLifetime): int|false
93    {
94        return $this->getSessionService()->garbageCollect($sessMaxLifetime);
95    }
96
97    /**
98     * A function that is called internally when session data is to be saved.
99     *
100     * @param string $sessId The current session ID
101     * @param string $data   The session data to write
102     *
103     * @return bool
104     */
105    protected function saveSession($sessId, $data): bool
106    {
107        return $this->getSessionService()->writeSession($sessId, $data);
108    }
109
110    /**
111     * Get a session service object
112     *
113     * @return SessionServiceInterface
114     */
115    protected function getSessionService(): SessionServiceInterface
116    {
117        return $this->getDbService(SessionServiceInterface::class);
118    }
119}