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