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
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 * @version   1.7.0
11 */
12
13namespace PrivateBin\Persistence;
14
15use PrivateBin\Configuration;
16
17/**
18 * PurgeLimiter
19 *
20 * Handles purge limiting, so purging is not triggered too frequently.
21 */
22class PurgeLimiter extends AbstractPersistence
23{
24    /**
25     * time limit in seconds, defaults to 300s
26     *
27     * @access private
28     * @static
29     * @var    int
30     */
31    private static $_limit = 300;
32
33    /**
34     * set the time limit in seconds
35     *
36     * @access public
37     * @static
38     * @param  int $limit
39     */
40    public static function setLimit($limit)
41    {
42        self::$_limit = $limit;
43    }
44
45    /**
46     * set configuration options of the traffic limiter
47     *
48     * @access public
49     * @static
50     * @param Configuration $conf
51     */
52    public static function setConfiguration(Configuration $conf)
53    {
54        self::setLimit($conf->getKey('limit', 'purge'));
55    }
56
57    /**
58     * check if the purge can be performed
59     *
60     * @access public
61     * @static
62     * @return bool
63     */
64    public static function canPurge()
65    {
66        // disable limits if set to less then 1
67        if (self::$_limit < 1) {
68            return true;
69        }
70
71        $now  = time();
72        $pl   = (int) self::$_store->getValue('purge_limiter');
73        if ($pl + self::$_limit >= $now) {
74            return false;
75        }
76        $hasStored = self::$_store->setValue((string) $now, 'purge_limiter');
77        if (!$hasStored) {
78            error_log('failed to store the purge limiter, skipping purge cycle to avoid getting stuck in a purge loop');
79        }
80        return $hasStored;
81    }
82}