1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\RateLimitedMiddleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Facades\Redis; |
7
|
|
|
|
8
|
|
|
class RateLimited |
9
|
|
|
{ |
10
|
|
|
/** @var bool|\Closure */ |
11
|
|
|
protected $enabled = true; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $connectionName = ''; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $key; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
protected $timeSpanInSeconds = 1; |
21
|
|
|
|
22
|
|
|
/** @var int */ |
23
|
|
|
protected $allowedNumberOfJobsInTimeSpan = 5; |
24
|
|
|
|
25
|
|
|
/** @var int */ |
26
|
|
|
protected $releaseInSeconds = 5; |
27
|
|
|
|
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$calledByClass = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class']; |
31
|
|
|
|
32
|
|
|
$this->key($calledByClass); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param bool|\Closure $enabled |
37
|
|
|
* |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
|
public function enabled($enabled = true) |
41
|
|
|
{ |
42
|
|
|
$this->enabled = $enabled; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function connectionName(string $connectionName) |
48
|
|
|
{ |
49
|
|
|
$this->connectionName = $connectionName; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function key(string $key) |
55
|
|
|
{ |
56
|
|
|
$this->key = $key; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function allow(int $allowedNumberOfJobsInTimeSpan) |
62
|
|
|
{ |
63
|
|
|
$this->allowedNumberOfJobsInTimeSpan = $allowedNumberOfJobsInTimeSpan; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function everySecond(int $timespanInSeconds = 1) |
69
|
|
|
{ |
70
|
|
|
$this->timeSpanInSeconds = $timespanInSeconds; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function everySeconds(int $timespanInSeconds) |
76
|
|
|
{ |
77
|
|
|
return $this->every($timespanInSeconds); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function everyMinute(int $timespanInMinutes = 1) |
81
|
|
|
{ |
82
|
|
|
return $this->every($timespanInMinutes * 60); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function everyMinutes(int $timespanInMinutes) |
86
|
|
|
{ |
87
|
|
|
return $this->everySeconds($timespanInMinutes * 60); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function releaseAfterOneSecond() |
91
|
|
|
{ |
92
|
|
|
return $this->releaseAfterSeconds(1); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function releaseAfterSeconds(int $releaseInSeconds) |
96
|
|
|
{ |
97
|
|
|
$this->releaseInSeconds = $releaseInSeconds; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function releaseAfterOneMinute() |
103
|
|
|
{ |
104
|
|
|
return $this->releaseAfterMinutes(1); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function releaseAfterMinutes(int $releaseInSeconds) |
108
|
|
|
{ |
109
|
|
|
return $this->releaseAfterSeconds($releaseInSeconds * 60); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function handle($job, $next) |
113
|
|
|
{ |
114
|
|
|
if ($this->enabled instanceof Closure) { |
115
|
|
|
$this->enabled = (bool)$this->enabled(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!$this->enabled) { |
119
|
|
|
return $next($job); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
Redis::connection($this->connectionName) |
123
|
|
|
->throttle($this->key) |
124
|
|
|
->block(0) |
125
|
|
|
->allow($this->allowedNumberOfJobsInTimeSpan) |
126
|
|
|
->every($this->timeSpanInSeconds) |
127
|
|
|
->then(function () use ($job, $next) { |
128
|
|
|
$next($job); |
129
|
|
|
}, function () use ($job) { |
130
|
|
|
$job->release($this->releaseInSeconds); |
131
|
|
|
}); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.