Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RouteServiceProvider
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 boot
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
5
 configureRateLimiting
100.00% covered (success)
100.00%
4 / 4
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\Database\Eloquent\ModelNotFoundException;
14use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
15use Illuminate\Http\Request;
16use Illuminate\Support\Facades\RateLimiter;
17use Illuminate\Support\Facades\Route;
18
19class 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}