Completed
Push — develop ( d878ad...d28e4b )
by Abdelrahman
05:14
created

FortServiceProvider::boot()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 2
eloc 23
nc 2
nop 1
dl 0
loc 40
rs 8.8571
c 6
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Providers;
6
7
use Illuminate\Http\Request;
8
use Rinvex\Fort\Models\Role;
9
use Rinvex\Fort\Models\User;
10
use Illuminate\Routing\Router;
11
use Rinvex\Menus\Facades\Menu;
12
use Rinvex\Fort\Models\Ability;
13
use Rinvex\Fort\Models\Session;
14
use Illuminate\Support\ServiceProvider;
15
use Rinvex\Menus\Factories\MenuFactory;
16
use Cortex\Fort\Console\Commands\SeedCommand;
17
use Cortex\Fort\Console\Commands\InstallCommand;
18
use Cortex\Fort\Console\Commands\MigrateCommand;
19
use Cortex\Fort\Console\Commands\PublishCommand;
20
use Cortex\Fort\Console\Commands\RollbackCommand;
21
use Illuminate\Database\Eloquent\Relations\Relation;
22
23
class FortServiceProvider extends ServiceProvider
24
{
25
    /**
26
     * The commands to be registered.
27
     *
28
     * @var array
29
     */
30
    protected $commands = [
31
        SeedCommand::class => 'command.cortex.fort.seed',
32
        InstallCommand::class => 'command.cortex.fort.install',
33
        MigrateCommand::class => 'command.cortex.fort.migrate',
34
        PublishCommand::class => 'command.cortex.fort.publish',
35
        RollbackCommand::class => 'command.cortex.fort.rollback',
36
    ];
37
38
    /**
39
     * Register any application services.
40
     *
41
     * This service provider is a great spot to register your various container
42
     * bindings with the application. As you can see, we are registering our
43
     * "Registrar" implementation here. You can add your own bindings too!
44
     *
45
     * @return void
46
     */
47
    public function register(): void
48
    {
49
        $this->app->singleton('cortex.fort.user.tabs', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
            return collect();
51
        });
52
        $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'cortex.fort');
53
54
        // Register console commands
55
        ! $this->app->runningInConsole() || $this->registerCommands();
56
    }
57
58
    /**
59
     * Bootstrap any application services.
60
     *
61
     * @return void
62
     */
63
    public function boot(Router $router): void
64
    {
65
        // Attach request macro
66
        $this->attachRequestMacro();
67
68
        // Bind route models and constrains
69
        $router->pattern('ability', '[0-9]+');
70
        $router->pattern('role', '[a-z0-9-]+');
71
        $router->pattern('user', '[a-zA-Z0-9_-]+');
72
        $router->pattern('session', '[a-zA-Z0-9]+');
73
        $router->model('ability', Ability::class);
74
        $router->model('session', Session::class);
75
        $router->model('role', Role::class);
76
        $router->model('user', User::class);
77
78
        // Map relations
79
        Relation::morphMap([
80
            'role' => config('rinvex.fort.models.role'),
81
            'ability' => config('rinvex.fort.models.ability'),
82
            'user' => config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 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
        ]);
84
85
        // Load resources
86
        require __DIR__.'/../../routes/breadcrumbs.php';
87
        $this->loadRoutesFrom(__DIR__.'/../../routes/web.php');
88
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/fort');
89
        $this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/fort');
90
        $this->app->afterResolving('blade.compiler', function () {
91
            require __DIR__.'/../../routes/menus.php';
92
        });
93
94
        // Publish Resources
95
        ! $this->app->runningInConsole() || $this->publishResources();
96
97
        // Register attributes entities
98
        app('rinvex.attributes.entities')->push('user');
99
100
        // Register menus
101
        $this->registerMenus();
102
    }
103
104
    /**
105
     * Publish resources.
106
     *
107
     * @return void
108
     */
109
    protected function publishResources(): void
110
    {
111
        $this->publishes([realpath(__DIR__.'/../../config/config.php') => config_path('cortex.fort.php')], 'cortex-fort-config');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 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...
112
        $this->publishes([realpath(__DIR__.'/../../resources/lang') => resource_path('lang/vendor/cortex/fort')], 'cortex-fort-lang');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
113
        $this->publishes([realpath(__DIR__.'/../../resources/views') => resource_path('views/vendor/cortex/fort')], 'cortex-fort-views');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 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...
114
    }
115
116
    /**
117
     * Register console commands.
118
     *
119
     * @return void
120
     */
121
    protected function registerCommands(): void
122
    {
123
        // Register artisan commands
124
        foreach ($this->commands as $key => $value) {
125
            $this->app->singleton($value, function ($app) use ($key) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
                return new $key();
127
            });
128
        }
129
130
        $this->commands(array_values($this->commands));
131
    }
132
133
    /**
134
     * Register console commands.
135
     *
136
     * @return void
137
     */
138
    protected function attachRequestMacro(): void
139
    {
140
        Request::macro('attemptUser', function (string $guard = null) {
141
            $twofactor = $this->session()->get('rinvex.fort.twofactor');
0 ignored issues
show
Bug introduced by
The method session() does not seem to exist on object<Cortex\Fort\Providers\FortServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
            return auth()->guard($guard)->getProvider()->retrieveById($twofactor['user_id']);
0 ignored issues
show
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
143
        });
144
    }
145
146
    /**
147
     * Register menus.
148
     *
149
     * @return void
150
     */
151
    protected function registerMenus(): void
152
    {
153
        $this->app['rinvex.menus.presenters']->put('account.sidebar', \Cortex\Fort\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...
154
155
        Menu::make('frontarea.account.sidebar', function (MenuFactory $menu) {
0 ignored issues
show
Unused Code introduced by
The parameter $menu is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
        });
157
        Menu::make('tenantarea.account.sidebar', function (MenuFactory $menu) {
0 ignored issues
show
Unused Code introduced by
The parameter $menu is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
        });
159
    }
160
}
161