Passed
Pull Request — main (#29)
by Tan
03:07
created

RouteServiceProvider::configureRateLimiting()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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