Passed
Push — main ( 749c12...5bb564 )
by Tan
08:21 queued 05:11
created

RouteServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace CSlant\Blog\ApiPackage\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(40)->by($identifier);
30
        });
31
    }
32
}
33