2020-12-01 17:02:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
|
2021-09-10 15:43:27 +02:00
|
|
|
use App\Models\Answer;
|
2021-09-08 16:43:44 +02:00
|
|
|
use App\Models\Brand;
|
2021-03-14 10:45:42 +01:00
|
|
|
use App\Models\Customer;
|
2021-08-23 21:04:01 +02:00
|
|
|
use App\Models\CustomerInfo;
|
2021-09-10 15:43:27 +02:00
|
|
|
use App\Models\Question;
|
2021-09-08 16:43:44 +02:00
|
|
|
use App\Models\Service;
|
|
|
|
|
use App\Models\SingleOptin;
|
2020-12-01 17:02:10 +01:00
|
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
2021-08-23 21:04:01 +02:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2020-12-01 17:02:10 +01:00
|
|
|
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 () {
|
2021-12-19 15:57:24 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-03-14 10:45:42 +01:00
|
|
|
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'));
|
2021-12-19 15:57:24 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-08-04 09:42:48 +02:00
|
|
|
});
|
2021-03-14 10:45:42 +01:00
|
|
|
|
2021-09-08 16:43:44 +02:00
|
|
|
Route::model('brand', Brand::class);
|
|
|
|
|
Route::model('service', Service::class);
|
|
|
|
|
Route::model('single_optin', SingleOptin::class);
|
2021-09-10 15:43:27 +02:00
|
|
|
Route::model('question', Question::class);
|
|
|
|
|
Route::model('answer', Answer::class);
|
2021-12-19 15:57:24 +01:00
|
|
|
Route::model('customer', Customer::class);
|
2021-09-08 16:43:44 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// * Api parameters Bind.
|
|
|
|
|
Route::model('serviceQuestion', Question::class);
|
|
|
|
|
Route::bind('serviceCustomer', function ($value) {
|
|
|
|
|
// * Load brand
|
|
|
|
|
/** @var \App\Models\Brand $brand */
|
|
|
|
|
$brand = app(Service::class)->brand;
|
|
|
|
|
|
|
|
|
|
// ? Parameter is email
|
|
|
|
|
if (strpos($value, '@') !== false) {
|
|
|
|
|
$customer = Customer::whereHas('brands', function ($q) use ($brand) {
|
|
|
|
|
$q->where('brands.id', '=', $brand->id);
|
|
|
|
|
})->where('email', '=', $value)->firstOrFail();
|
2021-08-23 21:04:01 +02:00
|
|
|
}
|
2021-12-19 15:57:24 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// ? Have we found a customer yet
|
|
|
|
|
if (!isset($customer) or \get_class($customer) !== Customer::class) {
|
|
|
|
|
// ? Parameter is hashcode
|
|
|
|
|
$customer = CustomerInfo::whereHas('brand', function ($q) use ($brand) {
|
|
|
|
|
$q->where('brands.id', '=', $brand->id);
|
|
|
|
|
})->where('hashcode', $value)
|
|
|
|
|
->with('customer')->firstOrFail()->customer;
|
2021-08-23 21:04:01 +02:00
|
|
|
}
|
2021-12-19 15:57:24 +01:00
|
|
|
|
|
|
|
|
// * Load Customer Login and Info for this brand.
|
2021-08-24 16:05:05 +02:00
|
|
|
/** @var \App\Models\Customer $customer */
|
2021-12-19 15:57:24 +01:00
|
|
|
|
|
|
|
|
return $customer->loadBrandData($brand);
|
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
|
// * Uniform Not found exception using model Customer.
|
|
|
|
|
throw (new ModelNotFoundException())->setModel(Customer::class);
|
2021-08-24 16:05:05 +02:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configure the rate limiters for the application.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function configureRateLimiting()
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
RateLimiter::for('web', function (Request $request) {
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
return Limit::perMinute(25)
|
|
|
|
|
->by(optional($request->user())->id ?: app('clientIp'));
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
});
|
|
|
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
2020-12-01 17:02:10 +01:00
|
|
|
RateLimiter::for('api', function (Request $request) {
|
2021-12-19 15:57:24 +01:00
|
|
|
return Limit::perMinute(config('api.apiRequestRatePerMinute', 100))
|
|
|
|
|
->by(app('clientIp'));
|
2020-12-01 17:02:10 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|