Completed
Push — master ( 9e59ff...11ca4d )
by Abdelrahman
66:39 queued 62:06
created

AuthServiceProvider::boot()   C

Complexity

Conditions 10
Paths 256

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 5.3865
c 0
b 0
f 0
cc 10
nc 256
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Providers;
6
7
use Bouncer;
8
use Cortex\Auth\Models\Role;
9
use Illuminate\Http\Request;
10
use Cortex\Auth\Models\Admin;
11
use Cortex\Auth\Models\Member;
12
use Illuminate\Routing\Router;
13
use Cortex\Auth\Models\Ability;
14
use Cortex\Auth\Models\Manager;
15
use Cortex\Auth\Models\Session;
16
use Cortex\Auth\Models\Guardian;
17
use Cortex\Auth\Models\Socialite;
18
use Illuminate\Support\ServiceProvider;
19
use Rinvex\Support\Traits\ConsoleTools;
20
use Cortex\Auth\Handlers\GenericHandler;
21
use Cortex\Auth\Console\Commands\SeedCommand;
22
use Cortex\Auth\Http\Middleware\Reauthenticate;
23
use Cortex\Auth\Console\Commands\InstallCommand;
24
use Cortex\Auth\Console\Commands\MigrateCommand;
25
use Cortex\Auth\Console\Commands\PublishCommand;
26
use Cortex\Auth\Console\Commands\RollbackCommand;
27
use Cortex\Auth\Http\Middleware\UpdateLastActivity;
28
use Illuminate\Database\Eloquent\Relations\Relation;
29
use Cortex\Auth\Http\Middleware\RedirectIfAuthenticated;
30
31
class AuthServiceProvider extends ServiceProvider
32
{
33
    use ConsoleTools;
34
35
    /**
36
     * The commands to be registered.
37
     *
38
     * @var array
39
     */
40
    protected $commands = [
41
        SeedCommand::class => 'command.cortex.auth.seed',
42
        InstallCommand::class => 'command.cortex.auth.install',
43
        MigrateCommand::class => 'command.cortex.auth.migrate',
44
        PublishCommand::class => 'command.cortex.auth.publish',
45
        RollbackCommand::class => 'command.cortex.auth.rollback',
46
    ];
47
48
    /**
49
     * Register any application services.
50
     *
51
     * This service provider is a great spot to register your various container
52
     * bindings with the application. As you can see, we are registering our
53
     * "Registrar" implementation here. You can add your own bindings too!
54
     *
55
     * @return void
56
     */
57
    public function register(): void
58
    {
59
        // Merge config
60
        $this->app['config']->set('auth.model', config('cortex.auth.models.member'));
61
        $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'cortex.auth');
62
63
        // Register console commands
64
        ! $this->app->runningInConsole() || $this->registerCommands();
65
66
        // Bind eloquent models to IoC container
67
        $this->app->singleton('cortex.auth.session', $sessionModel = $this->app['config']['cortex.auth.models.session']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
68
        $sessionModel === Session::class || $this->app->alias('cortex.auth.session', Session::class);
69
70
        $this->app->singleton('cortex.auth.socialite', $socialiteModel = $this->app['config']['cortex.auth.models.socialite']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
71
        $socialiteModel === Socialite::class || $this->app->alias('cortex.auth.socialite', Socialite::class);
72
73
        $this->app->singleton('cortex.auth.admin', $adminModel = $this->app['config']['cortex.auth.models.admin']);
74
        $adminModel === Admin::class || $this->app->alias('cortex.auth.admin', Admin::class);
75
76
        $this->app->singleton('cortex.auth.member', $memberModel = $this->app['config']['cortex.auth.models.member']);
77
        $memberModel === Member::class || $this->app->alias('cortex.auth.member', Member::class);
78
79
        $this->app->singleton('cortex.auth.manager', $managerModel = $this->app['config']['cortex.auth.models.manager']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
80
        $managerModel === Manager::class || $this->app->alias('cortex.auth.manager', Manager::class);
81
82
        $this->app->singleton('cortex.auth.guardian', $guardianModel = $this->app['config']['cortex.auth.models.guardian']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
83
        $guardianModel === Guardian::class || $this->app->alias('cortex.auth.guardian', Guardian::class);
84
85
        $this->app->singleton('cortex.auth.role', $roleModel = $this->app['config']['cortex.auth.models.role']);
86
        $roleModel === Role::class || $this->app->alias('cortex.auth.role', Role::class);
87
88
        $this->app->singleton('cortex.auth.ability', $abilityModel = $this->app['config']['cortex.auth.models.ability']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
89
        $abilityModel === Ability::class || $this->app->alias('cortex.auth.ability', Ability::class);
90
    }
91
92
    /**
93
     * Bootstrap any application services.
94
     *
95
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
96
     *
97
     * @return void
98
     */
99
    public function boot(Router $router): void
100
    {
101
        // Attach request macro
102
        $this->attachRequestMacro();
103
104
        // Map bouncer models
105
        Bouncer::useRoleModel(config('cortex.auth.models.role'));
106
        Bouncer::useAbilityModel(config('cortex.auth.models.ability'));
107
108
        // Map bouncer tables (users, roles, abilities tables are set through their models)
109
        Bouncer::tables([
110
            'permissions' => config('cortex.auth.tables.permissions'),
111
            'assigned_roles' => config('cortex.auth.tables.assigned_roles'),
112
        ]);
113
114
        // Bind route models and constrains
115
        $router->pattern('role', '[a-zA-Z0-9-]+');
116
        $router->pattern('ability', '[a-zA-Z0-9-]+');
117
        $router->pattern('session', '[a-zA-Z0-9-]+');
118
        $router->pattern('admin', '[a-zA-Z0-9-]+');
119
        $router->pattern('member', '[a-zA-Z0-9-]+');
120
        $router->pattern('manager', '[a-zA-Z0-9-]+');
121
        $router->model('role', config('cortex.auth.models.role'));
122
        $router->model('admin', config('cortex.auth.models.admin'));
123
        $router->model('member', config('cortex.auth.models.member'));
124
        $router->model('manager', config('cortex.auth.models.manager'));
125
        $router->model('guardian', config('cortex.auth.models.guardian'));
126
        $router->model('ability', config('cortex.auth.models.ability'));
127
        $router->model('session', config('cortex.auth.models.session'));
128
129
        // Map relations
130
        Relation::morphMap([
131
            'role' => config('cortex.auth.models.role'),
132
            'admin' => config('cortex.auth.models.admin'),
133
            'member' => config('cortex.auth.models.member'),
134
            'manager' => config('cortex.auth.models.manager'),
135
            'guardian' => config('cortex.auth.models.guardian'),
136
            'ability' => config('cortex.auth.models.ability'),
137
        ]);
138
139
        // Load resources
140
        require __DIR__.'/../../routes/breadcrumbs/adminarea.php';
141
        $this->loadRoutesFrom(__DIR__.'/../../routes/web/adminarea.php');
142
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/auth');
143
        $this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/auth');
144
        $this->app->runningInConsole() || $this->app->afterResolving('blade.compiler', function () {
145
            require __DIR__.'/../../routes/menus/managerarea.php';
146
            require __DIR__.'/../../routes/menus/tenantarea.php';
147
            require __DIR__.'/../../routes/menus/adminarea.php';
148
            require __DIR__.'/../../routes/menus/frontarea.php';
149
        });
150
151
        // Publish Resources
152
        ! $this->app->runningInConsole() || $this->publishesLang('cortex/auth', true);
153
        ! $this->app->runningInConsole() || $this->publishesViews('cortex/auth', true);
154
        ! $this->app->runningInConsole() || $this->publishesConfig('cortex/auth', true);
155
        ! $this->app->runningInConsole() || $this->publishesMigrations('cortex/auth', true);
156
157
        // Register event handlers
158
        $this->app['events']->subscribe(GenericHandler::class);
159
160
        // Register attributes entities
161
        ! app()->bound('rinvex.attributes.entities') || app('rinvex.attributes.entities')->push('admin');
162
        ! app()->bound('rinvex.attributes.entities') || app('rinvex.attributes.entities')->push('member');
163
        ! app()->bound('rinvex.attributes.entities') || app('rinvex.attributes.entities')->push('manager');
164
165
        // Override middlware
166
        $this->overrideMiddleware($router);
167
168
        // Register menus
169
        $this->registerMenus();
170
171
        // Share current user instance with all views
172
        $this->app['view']->composer('*', function ($view) {
173
            ! config('rinvex.tenants.active') || $view->with('currentTenant', config('rinvex.tenants.active'));
174
            $view->with('currentUser', auth()->guard(request()->route('guard'))->user());
175
        });
176
    }
177
178
    /**
179
     * Register console commands.
180
     *
181
     * @return void
182
     */
183
    protected function attachRequestMacro(): void
184
    {
185
        Request::macro('attemptUser', function (string $guard = null) {
186
            $twofactor = $this->session()->get('cortex.auth.twofactor');
187
188
            return auth()->guard($guard)->getProvider()->retrieveById($twofactor['user_id']);
189
        });
190
    }
191
192
    /**
193
     * Register menus.
194
     *
195
     * @return void
196
     */
197
    protected function registerMenus(): void
198
    {
199
        $this->app['rinvex.menus.presenters']->put('account.sidebar', \Cortex\Auth\Presenters\AccountSidebarMenuPresenter::class);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
200
    }
201
202
    /**
203
     * Override middleware.
204
     *
205
     * @param \Illuminate\Routing\Router $router
206
     *
207
     * @return void
208
     */
209
    protected function overrideMiddleware(Router $router): void
210
    {
211
        // Append middleware to the 'web' middlware group
212
        $router->pushMiddlewareToGroup('web', UpdateLastActivity::class);
213
214
        // Override route middleware on the fly
215
        $router->aliasMiddleware('reauthenticate', Reauthenticate::class);
216
        $router->aliasMiddleware('guest', RedirectIfAuthenticated::class);
217
    }
218
}
219