Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
OaiResumptionService
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 removeExpired
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createAndPersistToken
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 createEntity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 encodeParams
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Database service for OaiResumption.
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   Sudharma Kellampalli <skellamp@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 Laminas\Log\LoggerAwareInterface;
33use VuFind\Db\Entity\OaiResumptionEntityInterface;
34use VuFind\Db\Table\DbTableAwareInterface;
35use VuFind\Db\Table\DbTableAwareTrait;
36use VuFind\Log\LoggerAwareTrait;
37
38/**
39 * Database service for OaiResumption.
40 *
41 * @category VuFind
42 * @package  Database
43 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
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 OaiResumptionService extends AbstractDbService implements
48    DbTableAwareInterface,
49    LoggerAwareInterface,
50    OaiResumptionServiceInterface
51{
52    use DbTableAwareTrait;
53    use LoggerAwareTrait;
54
55    /**
56     * Remove all expired tokens from the database.
57     *
58     * @return void
59     */
60    public function removeExpired(): void
61    {
62        $this->getDbTable('oairesumption')->removeExpired();
63    }
64
65    /**
66     * Retrieve a row from the database based on primary key; return null if it
67     * is not found.
68     *
69     * @param string $token The resumption token to retrieve.
70     *
71     * @return ?OaiResumptionEntityInterface
72     */
73    public function findToken(string $token): ?OaiResumptionEntityInterface
74    {
75        return $this->getDbTable('oairesumption')->findToken($token);
76    }
77
78    /**
79     * Create and persist a new resumption token.
80     *
81     * @param array $params Parameters associated with the token.
82     * @param int   $expire Expiration time for token (Unix timestamp).
83     *
84     * @return OaiResumptionEntityInterface
85     * @throws \Exception
86     */
87    public function createAndPersistToken(array $params, int $expire): OaiResumptionEntityInterface
88    {
89        $row = $this->createEntity()
90            ->setResumptionParameters($this->encodeParams($params))
91            ->setExpiry(\DateTime::createFromFormat('U', $expire));
92        try {
93            $this->persistEntity($row);
94        } catch (\Exception $e) {
95            $this->logError('Could not save token: ' . $e->getMessage());
96            throw $e;
97        }
98        return $row;
99    }
100
101    /**
102     * Create a OaiResumption entity object.
103     *
104     * @return OaiResumptionEntityInterface
105     */
106    public function createEntity(): OaiResumptionEntityInterface
107    {
108        return $this->getDbTable('oairesumption')->createRow();
109    }
110
111    /**
112     * Encode an array of parameters into the object.
113     *
114     * @param array $params Parameters to save.
115     *
116     * @return string
117     */
118    protected function encodeParams(array $params): string
119    {
120        ksort($params);
121        $processedParams = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
122        return $processedParams;
123    }
124}