Completed
Push — develop ( 994dad...3d4fcd )
by Abdelrahman
13:36
created

TenantsServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 127
rs 10
c 3
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 2
A boot() 0 47 3
A registerMenus() 0 6 1
A publishResources() 0 6 1
A registerCommands() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Providers;
6
7
use Illuminate\Routing\Router;
8
use Rinvex\Menus\Facades\Menu;
9
use Cortex\Tenants\Models\Tenant;
10
use Illuminate\Support\Facades\Auth;
11
use Illuminate\Support\ServiceProvider;
12
use Rinvex\Menus\Factories\MenuFactory;
13
use Rinvex\Tenants\Contracts\TenantContract;
14
use Cortex\Tenants\Http\Middleware\Tenantable;
15
use Cortex\Tenants\Console\Commands\SeedCommand;
16
use Cortex\Tenants\Console\Commands\InstallCommand;
17
use Cortex\Tenants\Console\Commands\MigrateCommand;
18
use Cortex\Tenants\Console\Commands\PublishCommand;
19
use Cortex\Tenants\Console\Commands\RollbackCommand;
20
use Illuminate\Database\Eloquent\Relations\Relation;
21
use Illuminate\Routing\Middleware\SubstituteBindings;
22
use Cortex\Tenants\Overrides\Illuminate\Auth\EloquentUserProvider;
23
24
class TenantsServiceProvider extends ServiceProvider
25
{
26
    /**
27
     * The commands to be registered.
28
     *
29
     * @var array
30
     */
31
    protected $commands = [
32
        SeedCommand::class => 'command.cortex.tenants.seed',
33
        InstallCommand::class => 'command.cortex.tenants.install',
34
        MigrateCommand::class => 'command.cortex.tenants.migrate',
35
        PublishCommand::class => 'command.cortex.tenants.publish',
36
        RollbackCommand::class => 'command.cortex.tenants.rollback',
37
    ];
38
39
    /**
40
     * Register any application services.
41
     *
42
     * This service provider is a great spot to register your various container
43
     * bindings with the application. As you can see, we are registering our
44
     * "Registrar" implementation here. You can add your own bindings too!
45
     *
46
     * @return void
47
     */
48
    public function register()
49
    {
50
        // Merge config
51
        $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'cortex.tenants');
52
53
        // Register console commands
54
        ! $this->app->runningInConsole() || $this->registerCommands();
55
    }
56
57
    /**
58
     * Bootstrap any application services.
59
     *
60
     * @return void
61
     */
62
    public function boot(Router $router)
63
    {
64
        // Bind route models and constrains
65
        $router->pattern('tenant', '[a-z0-9-]+');
66
        $router->model('tenant', TenantContract::class);
67
68
        // Map relations
69
        Relation::morphMap([
70
            'tenant' => config('rinvex.tenants.models.tenant'),
71
        ]);
72
73
        // Load resources
74
        require __DIR__.'/../../routes/breadcrumbs.php';
75
        $this->loadRoutesFrom(__DIR__.'/../../routes/web.php');
76
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/tenants');
77
        $this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/tenants');
78
        ! $this->app->runningInConsole() || $this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
79
        $this->app->afterResolving('blade.compiler', function () {
80
            require __DIR__.'/../../routes/menus.php';
81
        });
82
83
        // Publish Resources
84
        ! $this->app->runningInConsole() || $this->publishResources();
85
86
        // Inject tenantable middleware before route bindings substitution
87
        $pointer = array_search(SubstituteBindings::class, $router->middlewarePriority);
88
        $before = array_slice($router->middlewarePriority, 0, $pointer);
89
        $after = array_slice($router->middlewarePriority, $pointer);
90
91
        $router->middlewarePriority = array_merge($before, [Tenantable::class], $after);
92
        $router->pushMiddlewareToGroup('web', Tenantable::class);
93
94
        // Override EloquentUserProvider to remove tenantable
95
        // global scope when retrieving authenticated user instance
96
        Auth::provider('eloquent', function ($app, array $config) {
97
            return new EloquentUserProvider($app['hash'], $config['model']);
98
        });
99
100
        // Override fort controllers
101
        $this->app->singleton(\Cortex\Fort\Http\Controllers\Frontarea\RegistrationController::class, \Cortex\Tenants\Http\Controllers\Frontarea\RegistrationController::class);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 175 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...
102
103
        // Register attributes entities
104
        app('rinvex.attributes.entities')->push(Tenant::class);
105
106
        // Register menus
107
        $this->registerMenus();
108
    }
109
110
    /**
111
     * Register menus.
112
     *
113
     * @return void
114
     */
115
    protected function registerMenus()
116
    {
117
        Menu::make('tenantarea.topbar', 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...
118
        Menu::make('managerarea.topbar', 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...
119
        Menu::make('managerarea.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...
120
    }
121
122
    /**
123
     * Publish resources.
124
     *
125
     * @return void
126
     */
127
    protected function publishResources()
128
    {
129
        $this->publishes([realpath(__DIR__.'/../../config/config.php') => config_path('cortex.tenants.php')], 'cortex-tenants-config');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
130
        $this->publishes([realpath(__DIR__.'/../../resources/lang') => resource_path('lang/vendor/cortex/tenants')], 'cortex-tenants-lang');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 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...
131
        $this->publishes([realpath(__DIR__.'/../../resources/views') => resource_path('views/vendor/cortex/tenants')], 'cortex-tenants-views');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 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...
132
    }
133
134
    /**
135
     * Register console commands.
136
     *
137
     * @return void
138
     */
139
    protected function registerCommands()
140
    {
141
        // Register artisan commands
142
        foreach ($this->commands as $key => $value) {
143
            $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...
144
                return new $key();
145
            });
146
        }
147
148
        $this->commands(array_values($this->commands));
149
    }
150
}
151