|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of laravel-quota |
|
5
|
|
|
* |
|
6
|
|
|
* (c) David Faith <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Full copyright and license information is available |
|
9
|
|
|
* in the LICENSE file distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Projectmentor\Quota; |
|
13
|
|
|
|
|
14
|
|
|
use bandwidthThrottle\tokenBucket\Rate; |
|
15
|
|
|
use Projectmentor\Quota\Contracts\Quota as QuotaInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This is the Quota class. |
|
19
|
|
|
* |
|
20
|
|
|
* NOTE: While currently implemented as a |
|
21
|
|
|
* concrete class for testing. This class |
|
22
|
|
|
* should be used only for inheritance. |
|
23
|
|
|
*/ |
|
24
|
|
|
class Quota implements QuotaInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The configured connection name. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $connection; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The configured connection index. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $index; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The quota limit |
|
42
|
|
|
* |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $limit; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The period to limit |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $period; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Construct instance. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $connection |
|
58
|
|
|
* @return void |
|
|
|
|
|
|
59
|
|
|
*/ |
|
60
|
14 |
|
public function __construct($connection) |
|
61
|
|
|
{ |
|
62
|
14 |
|
$this->connection = $connection; |
|
63
|
14 |
|
$this->index = 'quota.connections.' . $connection; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
14 |
|
$this->limit = config($this->index . '.limit'); |
|
66
|
|
|
|
|
67
|
14 |
|
$period = config($this->index . '.period'); |
|
68
|
14 |
|
$this->setPeriod($period); |
|
69
|
14 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get a list of valid periods. |
|
73
|
|
|
* @See bandwidthThrottle\tokenBucket\Rate::CONSTANTS |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
14 |
|
public function validPeriods() |
|
78
|
|
|
{ |
|
79
|
14 |
|
$r = new \ReflectionClass(Rate::class); |
|
|
|
|
|
|
80
|
|
|
//print_r($r->getConstants()); |
|
|
|
|
|
|
81
|
14 |
|
return $r->getConstants(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Validate period against Rate::CONSTANT values |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $period one of: |
|
88
|
|
|
* bandwidthThrottle\tokenBucket\Rate::CONSTANTS |
|
89
|
|
|
* @return boolean true on success |
|
90
|
|
|
*/ |
|
91
|
14 |
|
public function validatePeriod($period) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
14 |
|
$result = false; |
|
|
|
|
|
|
94
|
14 |
|
$constants = $this->validPeriods(); |
|
95
|
14 |
|
$values = array_values($constants); |
|
|
|
|
|
|
96
|
14 |
|
if (in_array($period, $values)) { |
|
97
|
14 |
|
$result = true; |
|
98
|
14 |
|
} |
|
99
|
14 |
|
return $result; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Public interface. |
|
104
|
|
|
* |
|
105
|
|
|
* NOTE: this class and method are really abstract, |
|
106
|
|
|
* but we leave it instantiable for easier testing. |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function enforce() |
|
109
|
|
|
{ |
|
110
|
|
|
//Override this in descendants. |
|
111
|
1 |
|
throw new \Exception('Not implemented here.'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the connection name. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
9 |
|
public function getConnection() |
|
120
|
|
|
{ |
|
121
|
9 |
|
return $this->connection; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get the index for the connection |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function getIndex() |
|
130
|
|
|
{ |
|
131
|
3 |
|
return $this->index; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the limit |
|
136
|
|
|
* |
|
137
|
|
|
* @return int |
|
138
|
|
|
*/ |
|
139
|
5 |
|
public function getLimit() |
|
140
|
|
|
{ |
|
141
|
5 |
|
return $this->limit; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get the period. |
|
146
|
|
|
* |
|
147
|
|
|
* @return string \bandwidthThrottle\tokenBucket\Rate::CONSTANT |
|
148
|
|
|
*/ |
|
149
|
3 |
|
public function getPeriod() |
|
150
|
|
|
{ |
|
151
|
3 |
|
return $this->period; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Set the connection name. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $connection |
|
158
|
|
|
* @return void |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setConnection($connection) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->connection = $connection; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Set the index |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $index |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setIndex($index) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->index = $index; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Set the limit |
|
178
|
|
|
* |
|
179
|
|
|
* @param int $limit |
|
180
|
|
|
* @return void |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setLimit($limit) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->limit = $limit; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Set the period. |
|
189
|
|
|
* |
|
190
|
|
|
* @param string \bandwidthThrottle\tokenBucket\Rate::CONSTANT |
|
191
|
|
|
* @return void |
|
192
|
|
|
* @throws InvalidArgumentException |
|
193
|
|
|
* |
|
194
|
|
|
*/ |
|
195
|
14 |
|
public function setPeriod($period) |
|
196
|
|
|
{ |
|
197
|
14 |
|
if (! $this->validatePeriod($period)) { |
|
198
|
|
|
$expected = $this->validPeriods(); |
|
199
|
|
|
|
|
200
|
|
|
throw new \InvalidArgumentException( |
|
201
|
|
|
__CLASS__.'::'.__FUNCTION__. |
|
202
|
|
|
' Invalid period: ' . $period . |
|
203
|
|
|
' in connection configuration: ' . $this->connection . |
|
204
|
|
|
' expected one of: ' . PHP_EOL . print_r($expected, 1) |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
14 |
|
$this->period = $period; |
|
209
|
14 |
|
} |
|
210
|
|
|
} |
|
|
|
|
|
|
211
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.