Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
33 / 33 |
| RouteServiceProvider | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
33 / 33 |
| boot | |
100.00% |
1 / 1 |
5 | |
100.00% |
26 / 26 |
|||
| configureRateLimiting | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Providers; |
| 4 | |
| 5 | use App\Models\Answer; |
| 6 | use App\Models\Brand; |
| 7 | use App\Models\Customer; |
| 8 | use App\Models\CustomerInfo; |
| 9 | use App\Models\Question; |
| 10 | use App\Models\Service; |
| 11 | use App\Models\SingleOptin; |
| 12 | use Illuminate\Cache\RateLimiting\Limit; |
| 13 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 14 | use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; |
| 15 | use Illuminate\Http\Request; |
| 16 | use Illuminate\Support\Facades\RateLimiter; |
| 17 | use Illuminate\Support\Facades\Route; |
| 18 | |
| 19 | class RouteServiceProvider extends ServiceProvider |
| 20 | { |
| 21 | /** |
| 22 | * The path to the "home" route for your application. |
| 23 | * |
| 24 | * This is used by Laravel authentication to redirect users after login. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public const HOME = '/customers'; |
| 29 | |
| 30 | /** |
| 31 | * Define your route model bindings, pattern filters, etc. |
| 32 | * |
| 33 | * @return void |
| 34 | */ |
| 35 | public function boot() |
| 36 | { |
| 37 | $this->configureRateLimiting(); |
| 38 | |
| 39 | $this->routes(function () { |
| 40 | // @codeCoverageIgnoreStart |
| 41 | Route::prefix('api')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php')); |
| 42 | Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php')); |
| 43 | // @codeCoverageIgnoreEnd |
| 44 | }); |
| 45 | |
| 46 | Route::model('brand', Brand::class); |
| 47 | Route::model('service', Service::class); |
| 48 | Route::model('single_optin', SingleOptin::class); |
| 49 | Route::model('question', Question::class); |
| 50 | Route::model('answer', Answer::class); |
| 51 | Route::model('customer', Customer::class); |
| 52 | |
| 53 | // * Api parameters Bind. |
| 54 | Route::model('serviceQuestion', Question::class); |
| 55 | Route::bind('serviceCustomer', function ($value) { |
| 56 | // * Load brand |
| 57 | /** @var \App\Models\Brand $brand */ |
| 58 | $brand = app(Service::class)->brand; |
| 59 | |
| 60 | // ? Parameter is email |
| 61 | if (strpos($value, '@') !== false) { |
| 62 | $customer = Customer::whereHas('brands', function ($q) use ($brand) { |
| 63 | $q->where('brands.id', '=', $brand->id); |
| 64 | })->where('email', '=', $value)->firstOrFail(); |
| 65 | } |
| 66 | |
| 67 | try { |
| 68 | // ? Have we found a customer yet |
| 69 | if (!isset($customer) or \get_class($customer) !== Customer::class) { |
| 70 | // ? Parameter is hashcode |
| 71 | $customer = CustomerInfo::whereHas('brand', function ($q) use ($brand) { |
| 72 | $q->where('brands.id', '=', $brand->id); |
| 73 | })->where('hashcode', $value) |
| 74 | ->with('customer')->firstOrFail()->customer; |
| 75 | } |
| 76 | |
| 77 | // * Load Customer Login and Info for this brand. |
| 78 | /** @var \App\Models\Customer $customer */ |
| 79 | |
| 80 | return $customer->loadBrandData($brand); |
| 81 | } catch (ModelNotFoundException $e) { |
| 82 | // * Uniform Not found exception using model Customer. |
| 83 | throw (new ModelNotFoundException())->setModel(Customer::class); |
| 84 | } |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Configure the rate limiters for the application. |
| 90 | * |
| 91 | * @return void |
| 92 | */ |
| 93 | protected function configureRateLimiting() |
| 94 | { |
| 95 | RateLimiter::for('web', function (Request $request) { |
| 96 | // @codeCoverageIgnoreStart |
| 97 | return Limit::perMinute(25) |
| 98 | ->by(optional($request->user())->id ?: app('clientIp')); |
| 99 | // @codeCoverageIgnoreEnd |
| 100 | }); |
| 101 | // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass |
| 102 | RateLimiter::for('api', function (Request $request) { |
| 103 | return Limit::perMinute(config('api.apiRequestRatePerMinute', 100)) |
| 104 | ->by(app('clientIp')); |
| 105 | }); |
| 106 | } |
| 107 | } |