Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExternalSessionService
81.82% covered (warning)
81.82%
9 / 11
60.00% covered (warning)
60.00%
3 / 5
5.15
0.00% covered (danger)
0.00%
0 / 1
 createEntity
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addSessionMapping
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getAllByExternalSessionId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 destroySession
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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 external_session table.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 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  Database
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:database_gateways Wiki
28 */
29
30namespace VuFind\Db\Service;
31
32use DateTime;
33use VuFind\Db\Entity\ExternalSessionEntityInterface;
34use VuFind\Db\Table\DbTableAwareInterface;
35use VuFind\Db\Table\DbTableAwareTrait;
36
37/**
38 * Database service for external_session table.
39 *
40 * @category VuFind
41 * @package  Database
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
45 */
46class ExternalSessionService extends AbstractDbService implements
47    ExternalSessionServiceInterface,
48    Feature\DeleteExpiredInterface,
49    DbTableAwareInterface
50{
51    use DbTableAwareTrait;
52
53    /**
54     * Create a new external session entity.
55     *
56     * @return ExternalSessionEntityInterface
57     */
58    public function createEntity(): ExternalSessionEntityInterface
59    {
60        return $this->getDbTable('ExternalSession')->createRow();
61    }
62
63    /**
64     * Add a mapping between local and external session id's; return the newly-created entity.
65     *
66     * @param string $localSessionId    Local (VuFind) session id
67     * @param string $externalSessionId External session id
68     *
69     * @return ExternalSessionEntityInterface
70     */
71    public function addSessionMapping(
72        string $localSessionId,
73        string $externalSessionId
74    ): ExternalSessionEntityInterface {
75        $this->destroySession($localSessionId);
76        $row = $this->createEntity()
77            ->setSessionId($localSessionId)
78            ->setExternalSessionId($externalSessionId)
79            ->setCreated(new DateTime());
80        $this->persistEntity($row);
81        return $row;
82    }
83
84    /**
85     * Retrieve objects from the database based on an external session ID
86     *
87     * @param string $sid External session ID to retrieve
88     *
89     * @return ExternalSessionEntityInterface[]
90     */
91    public function getAllByExternalSessionId(string $sid): array
92    {
93        return iterator_to_array($this->getDbTable('ExternalSession')->select(['external_session_id' => $sid]));
94    }
95
96    /**
97     * Destroy data for the given session ID.
98     *
99     * @param string $sid Session ID to erase
100     *
101     * @return void
102     */
103    public function destroySession(string $sid): void
104    {
105        $this->getDbTable('ExternalSession')->delete(['session_id' => $sid]);
106    }
107
108    /**
109     * Delete expired records. Allows setting a limit so that rows can be deleted in small batches.
110     *
111     * @param DateTime $dateLimit Date threshold of an "expired" record.
112     * @param ?int     $limit     Maximum number of rows to delete or null for no limit.
113     *
114     * @return int Number of rows deleted
115     */
116    public function deleteExpired(DateTime $dateLimit, ?int $limit = null): int
117    {
118        return $this->getDbTable('ExternalSession')->deleteExpired($dateLimit->format('Y-m-d H:i:s'), $limit);
119    }
120}