SellerServiceProvider::injectAuthConfiguration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 19
ccs 14
cts 14
cp 1
crap 1
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
namespace App\Modules\Sellers;
4
5
use App\Modules\Sellers\Auth\SellerGuard;
6
use App\Modules\Sellers\Auth\SellerUserProvider;
7
use App\Modules\Sellers\Models\Seller;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Blade;
10
use Illuminate\Support\ServiceProvider;
11
12
class SellerServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Bootstrap the application services.
16
     */
17 75
    public function boot(): void
18
    {
19 75
        $this->loadMigrationsFrom(__DIR__.'/database/migrations');
20 75
        $this->loadRoutesFrom(__DIR__.'/routes/seller.php');
21 75
        $this->loadViewsFrom(__DIR__.'/resources/views', 'seller');
22
23 75
        Blade::component('seller-app-layout', View\Components\SellerAppLayout::class);
24 75
        Blade::component('seller-guest-layout', View\Components\SellerGuestLayout::class);
25
    }
26
27
    /**
28
     * Register the application services.
29
     */
30 75
    public function register(): void
31
    {
32 75
        $this->registerMiddleware();
33 75
        $this->injectAuthConfiguration();
34 75
        $this->registerAuthDrivers('sellers', 'seller', Seller::class);
35
    }
36
37
    /**
38
     * @see https://laracasts.com/discuss/channels/general-discussion/register-middleware-via-service-provider
39
     */
40 75
    protected function registerMiddleware()
41
    {
42 75
        $router = $this->app['router'];
43 75
        $router->aliasMiddleware('seller.auth', Http\Middleware\RedirectIfNotSeller::class);
44 75
        $router->aliasMiddleware('seller.guest', Http\Middleware\RedirectIfSeller::class);
45 75
        $router->aliasMiddleware('seller.verified', Http\Middleware\EnsureSellerEmailIsVerified::class);
46 75
        $router->aliasMiddleware('seller.password.confirm', Http\Middleware\RequireSellerPassword::class);
47
    }
48
49
    /**
50
     * @see \Illuminate\Auth\AuthManager
51
     * @see https://www.devrohit.com/custom-authentication-in-laravel
52
     */
53 75
    protected function registerAuthDrivers(string $provider, string $guard, string $model)
54
    {
55 75
        Auth::provider('seller_provider_driver', function ($app) use ($model) {
56 25
            return new SellerUserProvider($app['hash'], $model);
57 75
        });
58
59
        /* AuthManager->createSessionDriver() */
60 75
        Auth::extend('seller_guard_driver', function ($app) use ($provider, $guard) {
61 25
            $userProvider = Auth::createUserProvider($provider);
62
63 25
            $sellerGuard = new SellerGuard($guard, $userProvider, $app['session.store']);
0 ignored issues
show
Bug introduced by
It seems like $userProvider can also be of type null; however, parameter $provider of App\Modules\Sellers\Auth...lerGuard::__construct() does only seem to accept Illuminate\Contracts\Auth\UserProvider, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            $sellerGuard = new SellerGuard($guard, /** @scrutinizer ignore-type */ $userProvider, $app['session.store']);
Loading history...
64
65 25
            if (method_exists($sellerGuard, 'setCookieJar')) {
66 25
                $sellerGuard->setCookieJar($this->app['cookie']);
67
            }
68
69 25
            if (method_exists($sellerGuard, 'setDispatcher')) {
70 25
                $sellerGuard->setDispatcher($this->app['events']);
71
            }
72
73 25
            if (method_exists($sellerGuard, 'setRequest')) {
74 25
                $sellerGuard->setRequest($this->app->refresh('request', $sellerGuard, 'setRequest'));
0 ignored issues
show
Bug introduced by
It seems like $this->app->refresh('req...lerGuard, 'setRequest') can also be of type null; however, parameter $request of Illuminate\Auth\SessionGuard::setRequest() does only seem to accept Symfony\Component\HttpFoundation\Request, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
                $sellerGuard->setRequest(/** @scrutinizer ignore-type */ $this->app->refresh('request', $sellerGuard, 'setRequest'));
Loading history...
75
            }
76
77 25
            if (isset($config['remember'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config seems to never exist and therefore isset should always be false.
Loading history...
78
                $sellerGuard->setRememberDuration($config['remember']);
79
            }
80
81 25
            return $sellerGuard;
82 75
        });
83
    }
84
85 75
    protected function injectAuthConfiguration()
86
    {
87 75
        $this->app['config']->set('auth.guards.seller', [
88
            // 'driver' => 'session',
89 75
            'driver' => 'seller_guard_driver',
90 75
            'provider' => 'sellers',
91 75
        ]);
92
93 75
        $this->app['config']->set('auth.providers.sellers', [
94
            // 'driver' => 'eloquent',
95 75
            'driver' => 'seller_provider_driver',
96 75
            'model' => Models\Seller::class,
97 75
        ]);
98
99 75
        $this->app['config']->set('auth.passwords.sellers', [
100 75
            'provider' => 'sellers',
101 75
            'table' => 'seller_password_reset_tokens',
102 75
            'expire' => 60,
103 75
            'throttle' => 60,
104 75
        ]);
105
    }
106
}
107