6
6
7
7
use App \Enums \Framework ;
8
8
use App \Models \DocumentationLink ;
9
+ use DanHarrin \LivewireRateLimiting \Exceptions \TooManyRequestsException ;
10
+ use DanHarrin \LivewireRateLimiting \WithRateLimiting ;
9
11
use Illuminate \Support \Facades \Session ;
10
12
use Livewire \Component ;
11
13
12
14
final class DiscoverDocumentation extends Component
13
15
{
16
+ use WithRateLimiting;
17
+
14
18
public Framework $ selectedFramework = Framework::Laravel;
15
19
16
20
public ?DocumentationLink $ link = null ;
@@ -19,6 +23,10 @@ final class DiscoverDocumentation extends Component
19
23
20
24
public int $ highScore = 0 ;
21
25
26
+ public bool $ rateLimitExceeded = false ;
27
+
28
+ public int $ secondsUntilAvailable = 0 ;
29
+
22
30
protected $ listeners = [
23
31
'framework-switched ' => 'handleFrameworkSwitch ' ,
24
32
];
@@ -32,6 +40,10 @@ public function mount(): void
32
40
33
41
public function getRandomLink (): void
34
42
{
43
+ if (! $ this ->checkRateLimit ()) {
44
+ return ;
45
+ }
46
+
35
47
$ this ->link = DocumentationLink::query ()
36
48
->where ('framework ' , $ this ->selectedFramework )
37
49
->inRandomOrder ()
@@ -61,6 +73,21 @@ public function render()
61
73
return view ('livewire.discover-documentation ' );
62
74
}
63
75
76
+ private function checkRateLimit (): bool
77
+ {
78
+ try {
79
+ $ this ->rateLimit (3 , 5 );
80
+ $ this ->rateLimitExceeded = false ;
81
+
82
+ return true ;
83
+ } catch (TooManyRequestsException $ tooManyRequestsException ) {
84
+ $ this ->rateLimitExceeded = true ;
85
+ $ this ->secondsUntilAvailable = $ tooManyRequestsException ->secondsUntilAvailable ;
86
+
87
+ return false ;
88
+ }
89
+ }
90
+
64
91
private function loadFrameworkStats (): void
65
92
{
66
93
$ frameworkKey = $ this ->selectedFramework ->value ;
0 commit comments