Passed
Push — main ( 39302f...c089c9 )
by Tan
02:57 queued 14s
created

RouteServiceProvider::configureRateLimiting()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 1
nop 0
1
<?php
2
3
namespace CSlant\Blog\Api\Providers;
4
5
use CSlant\Blog\Core\Models\User;
6
use Illuminate\Cache\RateLimiting\Limit;
7
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\RateLimiter;
10
11
class RouteServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * Move base routes to a service provider to make sure all filters & actions can hook to base routes
15
     */
16
    public function boot(): void
17
    {
18
        // add rate limit for api
19
        // $this->configureRateLimiting();
20
    }
21
22
    protected function configureRateLimiting(): void
23
    {
24
        RateLimiter::for((string) config('blog-api.defaults.route_prefix'), function (Request $request) {
25
            /** @var null|User $user */
26
            $user = $request->user();
27
            $identifier = $user ? $user->id : $request->ip();
28
29
            return Limit::perMinute(50)->by($identifier);
30
        });
31
    }
32
}
33