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

RouteServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 3
eloc 5
c 1
b 1
f 0
dl 0
loc 19
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureRateLimiting() 0 8 2
A boot() 0 2 1
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