Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AccessTokenService
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 5
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getByIdAndType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 storeNonce
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getNonce
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 access tokens.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 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   Aleksi Peebles <aleksi.peebles@helsinki.fi>
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 Laminas\Log\LoggerAwareInterface;
34use VuFind\Db\Entity\AccessTokenEntityInterface;
35use VuFind\Db\Table\AccessToken;
36use VuFind\Log\LoggerAwareTrait;
37
38/**
39 * Database service for access tokens.
40 *
41 * @category VuFind
42 * @package  Database
43 * @author   Aleksi Peebles <aleksi.peebles@helsinki.fi>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
46 */
47class AccessTokenService extends AbstractDbService implements
48    AccessTokenServiceInterface,
49    Feature\DeleteExpiredInterface,
50    LoggerAwareInterface
51{
52    use LoggerAwareTrait;
53
54    /**
55     * Constructor.
56     *
57     * @param AccessToken $accessTokenTable Access token table
58     */
59    public function __construct(protected AccessToken $accessTokenTable)
60    {
61    }
62
63    /**
64     * Retrieve an object from the database based on id and type; create a new
65     * row if no existing match is found.
66     *
67     * @param string $id     Token ID
68     * @param string $type   Token type
69     * @param bool   $create Should we create rows that don't already exist?
70     *
71     * @return ?AccessTokenEntityInterface
72     */
73    public function getByIdAndType(
74        string $id,
75        string $type,
76        bool $create = true
77    ): ?AccessTokenEntityInterface {
78        return $this->accessTokenTable->getByIdAndType($id, $type, $create);
79    }
80
81    /**
82     * Add or replace an OpenID nonce for a user
83     *
84     * @param int     $userId User ID
85     * @param ?string $nonce  Nonce
86     *
87     * @return void
88     */
89    public function storeNonce(int $userId, ?string $nonce): void
90    {
91        $this->accessTokenTable->storeNonce($userId, $nonce);
92    }
93
94    /**
95     * Retrieve an OpenID nonce for a user
96     *
97     * @param int $userId User ID
98     *
99     * @return ?string
100     */
101    public function getNonce(int $userId): ?string
102    {
103        return $this->accessTokenTable->getNonce($userId);
104    }
105
106    /**
107     * Delete expired records. Allows setting a limit so that rows can be deleted in small batches.
108     *
109     * @param DateTime $dateLimit Date threshold of an "expired" record.
110     * @param ?int     $limit     Maximum number of rows to delete or null for no limit.
111     *
112     * @return int Number of rows deleted
113     */
114    public function deleteExpired(DateTime $dateLimit, ?int $limit = null): int
115    {
116        return $this->accessTokenTable->deleteExpired($dateLimit->format('Y-m-d H:i:s'), $limit);
117    }
118}