Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExpirationTrait
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 expirationCallback
n/a
0 / 0
n/a
0 / 0
0
 deleteExpired
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getExpiredBatchLastId
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Trait for tables that support expiration
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 * Copyright (C) The National Library of Finland 2016.
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\Sql\Select;
35
36/**
37 * Trait for tables that support expiration
38 *
39 * @category VuFind
40 * @package  Db_Table
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @author   Ere Maijala <ere.maijala@helsinki.fi>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Site
45 */
46trait ExpirationTrait
47{
48    /**
49     * Update the select statement to find records to delete.
50     *
51     * @param Select $select    Select clause
52     * @param string $dateLimit Date threshold of an "expired" record in format
53     * 'Y-m-d H:i:s'.
54     *
55     * @return void
56     */
57    abstract protected function expirationCallback($select, $dateLimit);
58
59    /**
60     * Delete expired records. Allows setting of 'from' and 'to' ID's so that rows
61     * can be deleted in small batches.
62     *
63     * @param string   $dateLimit Date threshold of an "expired" record in format
64     * 'Y-m-d H:i:s'.
65     * @param int|null $limit     Maximum number of rows to delete or null for no
66     * limit.
67     *
68     * @return int Number of rows deleted
69     */
70    public function deleteExpired($dateLimit, $limit = null)
71    {
72        // Determine the expiration parameters:
73        $lastId = $limit ? $this->getExpiredBatchLastId($dateLimit, $limit) : null;
74        $callback = function ($select) use ($dateLimit, $lastId) {
75            $this->expirationCallback($select, $dateLimit);
76            if (null !== $lastId) {
77                $select->where->and->lessThanOrEqualTo('id', $lastId);
78            }
79        };
80        return $this->delete($callback);
81    }
82
83    /**
84     * Get the highest id to delete in a batch.
85     *
86     * @param string $dateLimit Date threshold of an "expired" record in format
87     * 'Y-m-d H:i:s'.
88     * @param int    $limit     Maximum number of rows to delete.
89     *
90     * @return int|null Highest id value to delete or null if a limiting id is not
91     * available
92     */
93    protected function getExpiredBatchLastId($dateLimit, $limit)
94    {
95        // Determine the expiration date:
96        $callback = function ($select) use ($dateLimit, $limit) {
97            $this->expirationCallback($select, $dateLimit);
98            $select->columns(['id'])->order('id')->offset($limit - 1)->limit(1);
99        };
100        $result = $this->select($callback)->current();
101        return $result ? $result->id : null;
102    }
103}