Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.73% covered (warning)
72.73%
8 / 11
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Database
72.73% covered (warning)
72.73%
8 / 11
80.00% covered (warning)
80.00%
4 / 5
6.73
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%
2 / 2
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 bool
91     */
92    #[\ReturnTypeWillChange]
93    public function gc($sessMaxLifetime)
94    {
95        $this->getSessionService()->garbageCollect($sessMaxLifetime);
96        return true;
97    }
98
99    /**
100     * A function that is called internally when session data is to be saved.
101     *
102     * @param string $sessId The current session ID
103     * @param string $data   The session data to write
104     *
105     * @return bool
106     */
107    protected function saveSession($sessId, $data): bool
108    {
109        return $this->getSessionService()->writeSession($sessId, $data);
110    }
111
112    /**
113     * Get a session service object
114     *
115     * @return SessionServiceInterface
116     */
117    protected function getSessionService(): SessionServiceInterface
118    {
119        return $this->getDbService(SessionServiceInterface::class);
120    }
121}