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\Throttle\Entity\Data; |
29
|
|
|
use Sunspikes\Ratelimit\Throttle\Settings\ThrottleSettingsInterface; |
30
|
|
|
use Sunspikes\src\Throttle\Cache\ThrottlerCacheInterface; |
31
|
|
|
|
32
|
|
|
abstract class AbstractWindowThrottler implements \Countable |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var ThrottlerCacheInterface |
36
|
|
|
*/ |
37
|
|
|
protected $throttlerCache; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Data |
41
|
|
|
*/ |
42
|
|
|
protected $data; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ThrottleSettingsInterface |
46
|
|
|
*/ |
47
|
|
|
protected $settings; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
protected $counter; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param ThrottlerCacheInterface $throttlerCache |
56
|
|
|
* @param Data $data |
57
|
|
|
* @param ThrottleSettingsInterface $settings |
58
|
|
|
*/ |
59
|
|
|
public function __construct(ThrottlerCacheInterface $throttlerCache, Data $data, ThrottleSettingsInterface $settings) |
60
|
|
|
{ |
61
|
|
|
$this->throttlerCache = $throttlerCache; |
62
|
|
|
$this->data = $data; |
63
|
|
|
$this->settings = $settings; |
64
|
|
|
$this->counter = 0; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function access(): bool |
71
|
36 |
|
{ |
72
|
|
|
$status = $this->check(); |
73
|
|
|
|
74
|
|
|
$this->hit(); |
75
|
|
|
|
76
|
|
|
return $status; |
77
|
|
|
} |
78
|
|
|
|
79
|
36 |
|
/** |
80
|
36 |
|
* @inheritdoc |
81
|
36 |
|
*/ |
82
|
36 |
|
public function clear(): ThrottlerInterface |
83
|
36 |
|
{ |
84
|
36 |
|
$this->counter = 0; |
85
|
36 |
|
$this->throttlerCache->remove($this->data->getKey()); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
/** |
91
|
|
|
* @inheritdoc |
92
|
6 |
|
*/ |
93
|
6 |
|
public function count() |
94
|
|
|
{ |
95
|
6 |
|
$this->counter = $this->throttlerCache->count($this->data->getKey()); |
96
|
|
|
|
97
|
|
|
return $this->counter; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
19 |
|
*/ |
103
|
|
|
public function check(): bool |
104
|
19 |
|
{ |
105
|
|
|
return (! $this->throttlerCache->isExpired($this->data->getKey())) |
106
|
|
|
&& ($this->throttlerCache->count($this->data->getKey()) < $this->settings->getLimit()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|