1 | <?php |
||
22 | class TrafficLimiter extends AbstractPersistence |
||
23 | { |
||
24 | /** |
||
25 | * time limit in seconds, defaults to 10s |
||
26 | * |
||
27 | * @access private |
||
28 | * @static |
||
29 | * @var int |
||
30 | */ |
||
31 | private static $_limit = 10; |
||
32 | |||
33 | /** |
||
34 | * key to fetch IP address |
||
35 | * |
||
36 | * @access private |
||
37 | * @static |
||
38 | * @var string |
||
39 | */ |
||
40 | private static $_ipKey = 'REMOTE_ADDR'; |
||
41 | |||
42 | /** |
||
43 | * set the time limit in seconds |
||
44 | * |
||
45 | * @access public |
||
46 | * @static |
||
47 | * @param int $limit |
||
48 | */ |
||
49 | 45 | public static function setLimit($limit) |
|
53 | |||
54 | /** |
||
55 | * set configuration options of the traffic limiter |
||
56 | * |
||
57 | * @access public |
||
58 | * @static |
||
59 | * @param Configuration $conf |
||
60 | */ |
||
61 | 44 | public static function setConfiguration(Configuration $conf) |
|
72 | |||
73 | /** |
||
74 | * get a HMAC of the current visitors IP address |
||
75 | * |
||
76 | * @access public |
||
77 | * @static |
||
78 | * @param string $algo |
||
79 | * @return string |
||
80 | */ |
||
81 | 20 | public static function getHash($algo = 'sha512') |
|
85 | |||
86 | /** |
||
87 | * traffic limiter |
||
88 | * |
||
89 | * Make sure the IP address makes at most 1 request every 10 seconds. |
||
90 | * |
||
91 | * @access public |
||
92 | * @static |
||
93 | * @throws Exception |
||
94 | * @return bool |
||
95 | */ |
||
96 | 45 | public static function canPass() |
|
140 | } |
||
141 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: