Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AuthHashService
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 createEntity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 deleteAuthHash
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getByHashAndType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLatestBySessionId
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 auth_hash 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\AuthHashEntityInterface;
34use VuFind\Db\Table\DbTableAwareInterface;
35use VuFind\Db\Table\DbTableAwareTrait;
36
37/**
38 * Database service for auth_hash 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 AuthHashService extends AbstractDbService implements
47    AuthHashServiceInterface,
48    DbTableAwareInterface,
49    Feature\DeleteExpiredInterface
50{
51    use DbTableAwareTrait;
52
53    /**
54     * Create an auth_hash entity object.
55     *
56     * @return AuthHashEntityInterface
57     */
58    public function createEntity(): AuthHashEntityInterface
59    {
60        return $this->getDbTable('AuthHash')->createRow();
61    }
62
63    /**
64     * Delete an auth_hash entity object.
65     *
66     * @param AuthHashEntityInterface|int $authHashOrId Object or ID value representing auth_hash to delete
67     *
68     * @return void
69     */
70    public function deleteAuthHash(AuthHashEntityInterface|int $authHashOrId): void
71    {
72        $authHashId = $authHashOrId instanceof AuthHashEntityInterface ? $authHashOrId->getId() : $authHashOrId;
73        $this->getDbTable('AuthHash')->delete(['id' => $authHashId]);
74    }
75
76    /**
77     * Retrieve an object from the database based on hash and type; possibly create a new
78     * row if no existing match is found.
79     *
80     * @param string $hash   Hash
81     * @param string $type   Hash type
82     * @param bool   $create Should we create rows that don't already exist?
83     *
84     * @return ?AuthHashEntityInterface
85     */
86    public function getByHashAndType(string $hash, string $type, bool $create = true): ?AuthHashEntityInterface
87    {
88        return $this->getDbTable('AuthHash')->getByHashAndType($hash, $type, $create);
89    }
90
91    /**
92     * Retrieve last object from the database based on session id.
93     *
94     * @param string $sessionId Session ID
95     *
96     * @return ?AuthHashEntityInterface
97     */
98    public function getLatestBySessionId(string $sessionId): ?AuthHashEntityInterface
99    {
100        return $this->getDbTable('AuthHash')->getLatestBySessionId($sessionId);
101    }
102
103    /**
104     * Delete expired records. Allows setting a limit so that rows can be deleted in small batches.
105     *
106     * @param DateTime $dateLimit Date threshold of an "expired" record.
107     * @param ?int     $limit     Maximum number of rows to delete or null for no limit.
108     *
109     * @return int Number of rows deleted
110     */
111    public function deleteExpired(DateTime $dateLimit, ?int $limit = null): int
112    {
113        return $this->getDbTable('AuthHash')->deleteExpired($dateLimit->format('Y-m-d H:i:s'), $limit);
114    }
115}