Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PurgeLimiter
91.67% covered (success)
91.67%
11 / 12
66.67% covered (warning)
66.67%
2 / 3
6.02
0.00% covered (danger)
0.00%
0 / 1
 setLimit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setConfiguration
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 canPurge
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
4.02
1<?php declare(strict_types=1);
2/**
3 * PrivateBin
4 *
5 * a zero-knowledge paste bin
6 *
7 * @link      https://github.com/PrivateBin/PrivateBin
8 * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
9 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
10 */
11
12namespace PrivateBin\Persistence;
13
14use PrivateBin\Configuration;
15
16/**
17 * PurgeLimiter
18 *
19 * Handles purge limiting, so purging is not triggered too frequently.
20 */
21class PurgeLimiter extends AbstractPersistence
22{
23    /**
24     * time limit in seconds, defaults to 300s
25     *
26     * @access private
27     * @static
28     * @var    int
29     */
30    private static $_limit = 300;
31
32    /**
33     * set the time limit in seconds
34     *
35     * @access public
36     * @static
37     * @param  int $limit
38     */
39    public static function setLimit($limit)
40    {
41        self::$_limit = $limit;
42    }
43
44    /**
45     * set configuration options of the traffic limiter
46     *
47     * @access public
48     * @static
49     * @param Configuration $conf
50     */
51    public static function setConfiguration(Configuration $conf)
52    {
53        self::setLimit($conf->getKey('limit', 'purge'));
54    }
55
56    /**
57     * check if the purge can be performed
58     *
59     * @access public
60     * @static
61     * @return bool
62     */
63    public static function canPurge()
64    {
65        // disable limits if set to less then 1
66        if (self::$_limit < 1) {
67            return true;
68        }
69
70        $now  = time();
71        $pl   = (int) self::$_store->getValue('purge_limiter');
72        if ($pl + self::$_limit >= $now) {
73            return false;
74        }
75        $hasStored = self::$_store->setValue((string) $now, 'purge_limiter');
76        if (!$hasStored) {
77            error_log('failed to store the purge limiter, skipping purge cycle to avoid getting stuck in a purge loop');
78        }
79        return $hasStored;
80    }
81}