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 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AuthHash
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 4
42
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
 getByHashAndType
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getLatestBySessionId
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 expirationCallback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Table Definition for auth_hash
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 * Copyright (C) The National Library of Finland 2019.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Db_Table
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @author   Ere Maijala <ere.maijala@helsinki.fi>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org Main Page
30 */
31
32namespace VuFind\Db\Table;
33
34use Laminas\Db\Adapter\Adapter;
35use VuFind\Db\Row\RowGateway;
36
37/**
38 * Table Definition for auth_hash
39 *
40 * @category VuFind
41 * @package  Db_Table
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @author   Ere Maijala <ere.maijala@helsinki.fi>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org Main Site
46 */
47class AuthHash extends Gateway
48{
49    use ExpirationTrait;
50
51    public const TYPE_EMAIL = 'email'; // EmailAuthenticator
52
53    /**
54     * Constructor
55     *
56     * @param Adapter       $adapter Database adapter
57     * @param PluginManager $tm      Table manager
58     * @param array         $cfg     Laminas configuration
59     * @param RowGateway    $rowObj  Row prototype object (null for default)
60     * @param string        $table   Name of database table to interface with
61     */
62    public function __construct(
63        Adapter $adapter,
64        PluginManager $tm,
65        $cfg,
66        ?RowGateway $rowObj = null,
67        $table = 'auth_hash'
68    ) {
69        parent::__construct($adapter, $tm, $cfg, $rowObj, $table);
70    }
71
72    /**
73     * Retrieve an object from the database based on hash and type; create a new
74     * row if no existing match is found.
75     *
76     * @param string $hash   Hash
77     * @param string $type   Hash type
78     * @param bool   $create Should we create rows that don't already exist?
79     *
80     * @return ?\VuFind\Db\Row\AuthHash
81     */
82    public function getByHashAndType($hash, $type, $create = true)
83    {
84        $row = $this->select(['hash' => $hash, 'type' => $type])->current();
85        if ($create && empty($row)) {
86            $row = $this->createRow();
87            $row->hash = $hash;
88            $row->type = $type;
89            $row->created = date('Y-m-d H:i:s');
90        }
91        return $row;
92    }
93
94    /**
95     * Retrieve last object from the database based on session id.
96     *
97     * @param string $sessionId Session ID
98     *
99     * @return ?\VuFind\Db\Row\AuthHash
100     */
101    public function getLatestBySessionId($sessionId)
102    {
103        $callback = function ($select) use ($sessionId) {
104            $select->where->equalTo('session_id', $sessionId);
105            $select->order('created DESC');
106        };
107        return $this->select($callback)->current();
108    }
109
110    /**
111     * Update the select statement to find records to delete.
112     *
113     * @param Select $select    Select clause
114     * @param string $dateLimit Date threshold of an "expired" record in format
115     * 'Y-m-d H:i:s'.
116     *
117     * @return void
118     */
119    protected function expirationCallback($select, $dateLimit)
120    {
121        $select->where->lessThan('created', $dateLimit);
122    }
123}