Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SessionService
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 7
56
0.00% covered (danger)
0.00%
0 / 1
 getSessionById
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 readSession
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 writeSession
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 destroySession
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 garbageCollect
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createEntity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 deleteExpired
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Database service for Session.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2023.
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  Database
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
29 */
30
31namespace VuFind\Db\Service;
32
33use DateTime;
34use VuFind\Db\Entity\SessionEntityInterface;
35use VuFind\Db\Table\DbTableAwareInterface;
36use VuFind\Db\Table\DbTableAwareTrait;
37
38/**
39 * Database service for Session.
40 *
41 * @category VuFind
42 * @package  Database
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
47 */
48class SessionService extends AbstractDbService implements
49    DbTableAwareInterface,
50    SessionServiceInterface,
51    Feature\DeleteExpiredInterface
52{
53    use DbTableAwareTrait;
54
55    /**
56     * Retrieve an object from the database based on session ID; create a new
57     * row if no existing match is found.
58     *
59     * @param string $sid    Session ID to retrieve
60     * @param bool   $create Should we create rows that don't already exist?
61     *
62     * @return ?SessionEntityInterface
63     */
64    public function getSessionById(string $sid, bool $create = true): ?SessionEntityInterface
65    {
66        return $this->getDbTable('Session')->getBySessionId($sid, $create);
67    }
68
69    /**
70     * Retrieve data for the given session ID.
71     *
72     * @param string $sid      Session ID to retrieve
73     * @param int    $lifetime Session lifetime (in seconds)
74     *
75     * @throws SessionExpiredException
76     * @return string     Session data
77     */
78    public function readSession(string $sid, int $lifetime): string
79    {
80        return $this->getDbTable('Session')->readSession($sid, $lifetime);
81    }
82
83    /**
84     * Store data for the given session ID.
85     *
86     * @param string $sid  Session ID to retrieve
87     * @param string $data Data to store
88     *
89     * @return bool
90     */
91    public function writeSession(string $sid, string $data): bool
92    {
93        $this->getDbTable('Session')->writeSession($sid, $data);
94        return true;
95    }
96
97    /**
98     * Destroy data for the given session ID.
99     *
100     * @param string $sid Session ID to erase
101     *
102     * @return void
103     */
104    public function destroySession(string $sid): void
105    {
106        $this->getDbTable('Session')->destroySession($sid);
107    }
108
109    /**
110     * Garbage collect expired sessions.
111     *
112     * @param int $maxLifetime Maximum session lifetime.
113     *
114     * @return void
115     */
116    public function garbageCollect(int $maxLifetime): void
117    {
118        $this->getDbTable('Session')->garbageCollect($maxLifetime);
119    }
120
121    /**
122     * Create a session entity object.
123     *
124     * @return SessionEntityInterface
125     */
126    public function createEntity(): SessionEntityInterface
127    {
128        return $this->getDbTable('Session')->createRow();
129    }
130
131    /**
132     * Delete expired records. Allows setting a limit so that rows can be deleted in small batches.
133     *
134     * @param DateTime $dateLimit Date threshold of an "expired" record.
135     * @param ?int     $limit     Maximum number of rows to delete or null for no limit.
136     *
137     * @return int Number of rows deleted
138     */
139    public function deleteExpired(DateTime $dateLimit, ?int $limit = null): int
140    {
141        return $this->getDbTable('Session')->deleteExpired($dateLimit->format('Y-m-d H:i:s'), $limit);
142    }
143}