ldc-dam/app/Providers/RouteServiceProvider.php

86 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2019-05-09 15:00:05 +02:00
<?php
namespace App\Providers;
2022-07-28 12:32:37 +02:00
use App\Enums\MediaStatut;
use App\Models\Article;
use App\Models\Media;
2022-04-01 10:18:04 +02:00
use App\Models\Panier;
2022-04-19 14:49:09 +02:00
use Illuminate\Cache\RateLimiting\Limit;
2019-05-09 15:00:05 +02:00
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
2022-04-19 14:49:09 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
2022-04-19 10:49:38 +02:00
use Illuminate\Support\Facades\Route;
2019-05-09 15:00:05 +02:00
class RouteServiceProvider extends ServiceProvider
{
2022-04-19 10:49:38 +02:00
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/';
2019-05-09 15:00:05 +02:00
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
2022-04-19 14:49:09 +02:00
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->group(base_path('routes/web.php'));
2022-04-20 15:23:31 +02:00
Route::middleware('api')
->prefix('api')
2022-04-19 14:49:09 +02:00
->group(base_path('routes/api.php'));
});
2022-04-01 10:18:04 +02:00
Route::bind('basket_uuid', function ($value) {
return Panier::where('uid', $value)
->with('owner')->firstOrFail();
2022-07-28 12:32:37 +02:00
});
// * API
Route::bind('apiNoArticle', function ($value) {
return Article::where('noArt', $value)->firstOrFail();
});
Route::bind('apiMedia', function ($value) {
return Media::where(function ($q) use ($value) {
$q->where('id', $value);
})->whereIn('statut', [
MediaStatut::public,
MediaStatut::public_etendu
])->firstOrFail();
});
Route::bind('apiMediaArcadiaId', function ($value) {
return Media::where(function ($q) use ($value) {
$q->where('arcadia_id', $value)
->whereNotNull('arcadia_id');
})->whereIn('statut', [
MediaStatut::public,
MediaStatut::public_etendu
])->firstOrFail();
2022-04-01 10:18:04 +02:00
});
2019-05-09 15:00:05 +02:00
}
/**
2022-04-19 14:49:09 +02:00
* Configure the rate limiters for the application.
2019-05-09 15:00:05 +02:00
*
* @return void
*/
2022-04-19 14:49:09 +02:00
protected function configureRateLimiting()
2019-05-09 15:00:05 +02:00
{
2022-07-28 12:32:37 +02:00
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
2022-04-19 14:49:09 +02:00
RateLimiter::for('api', function (Request $request) {
2022-07-28 12:32:37 +02:00
return Limit::perMinute(config('api.apiRequestRatePerMinute', 100))
->by($request->user()?->id ?: app('clientIp'));
2022-04-19 14:49:09 +02:00
});
2019-05-09 15:00:05 +02:00
}
}