|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Noxlogic\RateLimitBundle\Annotation; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Annotation; |
|
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationAnnotation; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @Annotation |
|
10
|
|
|
* @Target({"METHOD", "CLASS"}) |
|
11
|
|
|
*/ |
|
12
|
|
|
class RateLimit extends ConfigurationAnnotation |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array HTTP Methods protected by this annotation. Defaults to all method |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $methods = array(); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var int Number of calls per period |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $limit = -1; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int Number of seconds of the time period in which the calls can be made |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $period = 3600; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var mixed Generic payload |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $payload; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var bool allow the ratelimiter to fail open on any request where an exception is thrown |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $failOpen = false; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns the alias name for an annotated configuration. |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function getAliasName() |
|
45
|
|
|
{ |
|
46
|
1 |
|
return "x-rate-limit"; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns whether multiple annotations of this type are allowed |
|
51
|
|
|
* |
|
52
|
|
|
* @return Boolean |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function allowArray() |
|
55
|
|
|
{ |
|
56
|
1 |
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return int |
|
61
|
|
|
*/ |
|
62
|
16 |
|
public function getLimit() |
|
63
|
|
|
{ |
|
64
|
16 |
|
return $this->limit; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param int $limit |
|
69
|
|
|
*/ |
|
70
|
24 |
|
public function setLimit($limit) |
|
71
|
|
|
{ |
|
72
|
24 |
|
$this->limit = $limit; |
|
73
|
24 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
25 |
|
public function getMethods() |
|
79
|
|
|
{ |
|
80
|
25 |
|
return $this->methods; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $methods |
|
85
|
|
|
*/ |
|
86
|
13 |
|
public function setMethods($methods) |
|
87
|
|
|
{ |
|
88
|
13 |
|
$this->methods = (array) $methods; |
|
89
|
13 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return int |
|
93
|
|
|
*/ |
|
94
|
16 |
|
public function getPeriod() |
|
95
|
|
|
{ |
|
96
|
16 |
|
return $this->period; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param int $period |
|
101
|
|
|
*/ |
|
102
|
24 |
|
public function setPeriod($period) |
|
103
|
|
|
{ |
|
104
|
24 |
|
$this->period = $period; |
|
105
|
24 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
11 |
|
public function getPayload() |
|
111
|
|
|
{ |
|
112
|
11 |
|
return $this->payload; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param mixed $payload |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public function setPayload($payload) |
|
119
|
|
|
{ |
|
120
|
2 |
|
$this->payload = $payload; |
|
121
|
2 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function failOpen() |
|
127
|
|
|
{ |
|
128
|
1 |
|
return $this->failOpen; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param bool $failOpen |
|
133
|
|
|
*/ |
|
134
|
2 |
|
public function setFailOpen($failOpen) |
|
135
|
|
|
{ |
|
136
|
2 |
|
$this->failOpen = $failOpen; |
|
137
|
2 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|