ldc-crm/app/Providers/RouteServiceProvider.php
2021-09-10 15:43:27 +02:00

86 lines
2.8 KiB
PHP

<?php
namespace App\Providers;
use App\Models\Answer;
use App\Models\Brand;
use App\Models\Customer;
use App\Models\CustomerInfo;
use App\Models\Question;
use App\Models\Service;
use App\Models\SingleOptin;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* 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 = '/customers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php'));
Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));
});
Route::model('brand', Brand::class);
Route::model('service', Service::class);
Route::model('single_optin', SingleOptin::class);
Route::model('question', Question::class);
Route::model('answer', Answer::class);
Route::bind('customer', function ($value) {
if (\ctype_digit($value)) {
$customer = Customer::where('id', '=', $value)->firstOrFail();
}
if (!isset($customer) and strpos($value, '@') !== false) {
$customer = Customer::where('email', '=', $value)->firstOrFail();
}
if (!isset($customer)) {
try {
$customer = CustomerInfo::with('customer')
->where('hashcode', $value)->firstOrFail()->customer;
} catch (ModelNotFoundException $e) {
// Uniform Not found exception using model Customer
throw (new ModelNotFoundException())->setModel(Customer::class);
}
}
if ($brand = app('App\Models\Service')->brand) {
/** @var \App\Models\Customer $customer */
$customer = $customer->loadBrandData($brand);
}
return $customer;
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(config('api.apiRequestRatePerMinute'))
->by(optional($request->user())->id ?: $request->ip());
});
}
}