|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2015 Krishnaprasad MG <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
* copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace Sunspikes\Ratelimit\Throttle\Throttler; |
|
27
|
|
|
|
|
28
|
|
|
use Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterInterface; |
|
29
|
|
|
use Sunspikes\Ratelimit\Time\TimeAdapterInterface; |
|
30
|
|
|
|
|
31
|
|
|
abstract class AbstractWindowThrottler |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var CacheAdapterInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $cache; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int|null |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $cacheTtl; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var int |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $hitLimit; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $key; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var int |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $timeLimit; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var TimeAdapterInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $timeProvider; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param CacheAdapterInterface $cache |
|
65
|
|
|
* @param TimeAdapterInterface $timeAdapter |
|
66
|
|
|
* @param string $key Cache key prefix |
|
67
|
|
|
* @param int $hitLimit Maximum number of hits |
|
68
|
|
|
* @param int $timeLimit Length of window |
|
69
|
|
|
* @param int|null $cacheTtl Cache ttl time (default: null => CacheAdapter ttl) |
|
70
|
|
|
*/ |
|
71
|
36 |
|
public function __construct( |
|
72
|
|
|
CacheAdapterInterface $cache, |
|
73
|
|
|
TimeAdapterInterface $timeAdapter, |
|
74
|
|
|
$key, |
|
75
|
|
|
$hitLimit, |
|
76
|
|
|
$timeLimit, |
|
77
|
|
|
$cacheTtl = null |
|
78
|
|
|
) { |
|
79
|
36 |
|
$this->cache = $cache; |
|
80
|
36 |
|
$this->timeProvider = $timeAdapter; |
|
81
|
36 |
|
$this->key = $key; |
|
82
|
36 |
|
$this->hitLimit = $hitLimit; |
|
83
|
36 |
|
$this->timeLimit = $timeLimit; |
|
84
|
36 |
|
$this->cacheTtl = $cacheTtl; |
|
85
|
36 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritdoc |
|
89
|
|
|
*/ |
|
90
|
6 |
|
public function access() |
|
91
|
|
|
{ |
|
92
|
6 |
|
$status = $this->check(); |
|
93
|
6 |
|
$this->hit(); |
|
94
|
|
|
|
|
95
|
6 |
|
return $status; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritdoc |
|
101
|
|
|
*/ |
|
102
|
19 |
|
public function check() |
|
103
|
|
|
{ |
|
104
|
19 |
|
return $this->count() < $this->hitLimit; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @inheritdoc |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getTime() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->timeLimit; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @inheritdoc |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getLimit() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->hitLimit; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @inheritdoc |
|
125
|
|
|
*/ |
|
126
|
|
|
abstract public function hit(); |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @inheritdoc |
|
130
|
|
|
*/ |
|
131
|
|
|
abstract public function count(); |
|
132
|
|
|
} |
|
133
|
|
|
|