Passed
Push — ft/appmove ( db87fd...97613e )
by Philippe
45:05 queued 26:47
created

ChiefServiceProvider::boot()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 80
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 51
nc 2
nop 0
dl 0
loc 80
c 0
b 0
f 0
cc 3
rs 9.069

How to fix   Long Method   

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
namespace Thinktomorrow\Chief\App\Providers;
4
5
use Illuminate\Routing\Router;
6
use Thinktomorrow\Chief\Users\User;
7
use Illuminate\Support\Facades\Blade;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Support\ServiceProvider;
10
use Illuminate\Support\Facades\Validator;
11
use Thinktomorrow\Chief\App\Console\Seed;
12
use Thinktomorrow\Chief\Urls\ChiefResponse;
13
use Thinktomorrow\Chief\Management\Register;
14
use Thinktomorrow\Chief\App\Console\CreateAdmin;
15
use Thinktomorrow\Squanto\SquantoServiceProvider;
16
use Thinktomorrow\Chief\Pages\Console\GeneratePage;
17
use Thinktomorrow\Chief\App\Console\CreateDeveloper;
18
use Thinktomorrow\Chief\App\Console\RefreshDatabase;
19
use Thinktomorrow\Squanto\SquantoManagerServiceProvider;
20
use Thinktomorrow\Chief\Settings\SettingsServiceProvider;
21
use Thinktomorrow\AssetLibrary\AssetLibraryServiceProvider;
22
use Thinktomorrow\Chief\App\Http\Middleware\ChiefValidateInvite;
23
use Thinktomorrow\Chief\Authorization\Console\GenerateRoleCommand;
24
use Thinktomorrow\Chief\HealthMonitor\Middleware\MonitorMiddleware;
25
use Thinktomorrow\Chief\App\Http\Middleware\AuthenticateChiefSession;
26
use Thinktomorrow\Chief\Authorization\Console\GeneratePermissionCommand;
27
use Thinktomorrow\Chief\App\Http\Middleware\ChiefRedirectIfAuthenticated;
28
29
class ChiefServiceProvider extends ServiceProvider
30
{
31
    public function boot()
32
    {
33
        $this->registerChiefGuard();
34
35
        $this->app['view']->addNamespace('squanto', __DIR__ . '/../../../resources/views/vendor/squanto');
36
        $this->app['view']->addNamespace('squanto', base_path() . '/resources/views/vendor/thinktomorrow/chief/vendor/squanto');
37
38
        (new MacrosServiceProvider($this->app))->boot();
39
        (new AuthServiceProvider($this->app))->boot();
40
        (new EventServiceProvider($this->app))->boot();
41
        (new ViewServiceProvider($this->app))->boot();
42
        (new SquantoServiceProvider($this->app))->boot();
43
        (new SquantoManagerServiceProvider($this->app))->boot();
44
        (new SettingsServiceProvider($this->app))->boot();
45
46
        (new AssetLibraryServiceProvider($this->app))->boot();
47
48
        // Project defaults
49
        (new ChiefProjectServiceProvider($this->app))->boot();
50
51
        $this->loadRoutesFrom(__DIR__ . '/../routes.php');
52
        $this->loadViewsFrom(__DIR__ . '/../../../resources/views', 'chief');
53
        $this->loadMigrationsFrom(__DIR__ . '/../../../database/migrations');
54
        $this->loadTranslationsFrom(__DIR__ . '/../../../resources/lang', 'chief');
55
56
        $this->publishes([
57
            __DIR__ . '/../../../config/chief.php' => config_path('thinktomorrow/chief.php'),
58
            __DIR__ . '/../../../config/chief-settings.php' => config_path('thinktomorrow/chief-settings.php'),
59
        ], 'chief-config');
60
61
        $this->publishes([
62
            __DIR__ . '/../../../public/chief-assets' => public_path('/chief-assets'),
63
        ], 'chief-assets');
64
65
        // Register commands
66
        if ($this->app->runningInConsole()) {
67
            $this->commands([
68
                // Local development
69
                'command.chief:refresh',
70
                'command.chief:seed',
71
72
                // Project setup tools
73
                'command.chief:permission',
74
                'command.chief:role',
75
                'command.chief:admin',
76
                'command.chief:developer',
77
                'command.chief:page',
78
            ]);
79
80
            // Bind our commands to the container
81
            $this->app->bind('command.chief:refresh', RefreshDatabase::class);
82
            $this->app->bind('command.chief:seed', Seed::class);
83
            $this->app->bind('command.chief:permission', GeneratePermissionCommand::class);
84
            $this->app->bind('command.chief:role', GenerateRoleCommand::class);
85
            $this->app->bind('command.chief:admin', CreateAdmin::class);
86
            $this->app->bind('command.chief:developer', CreateDeveloper::class);
87
            $this->app->bind('command.chief:page', function ($app) {
88
                return new GeneratePage($app['files'], [
89
                    'base_path' => base_path()
90
                ]);
91
            });
92
        }
93
94
        Blade::component('chief::back._layouts._partials.header', 'chiefheader');
95
        Blade::component('chief::back._elements.formgroup', 'chiefformgroup');
96
97
        // Custom validator for requiring on translations only the fallback locale
98
        // this is called in the validation as required-fallback-locale
99
        Validator::extendImplicit('requiredFallbackLocale', function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed. ( Ignorable by Annotation )

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

99
        Validator::extendImplicit('requiredFallbackLocale', function ($attribute, $value, /** @scrutinizer ignore-unused */ $parameters, $validator) {

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

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

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

99
        Validator::extendImplicit('requiredFallbackLocale', function ($attribute, $value, $parameters, /** @scrutinizer ignore-unused */ $validator) {

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

Loading history...
100
            $fallbackLocale = config('app.fallback_locale');
101
102
            if (false !== strpos($attribute, 'trans.' . $fallbackLocale . '.')) {
103
                return !!trim($value);
104
            }
105
106
            return true;
107
        }, 'Voor :attribute is minstens de default taal verplicht in te vullen, aub.');
108
109
        $this->autoloadMiddleware();
110
        $this->autoloadRoute();
111
    }
112
113
    public function register()
114
    {
115
        $this->mergeConfigFrom(__DIR__ . '/../../../config/chief.php', 'thinktomorrow.chief');
116
        $this->mergeConfigFrom(__DIR__ . '/../../../config/chief-settings.php', 'thinktomorrow.chief-settings');
117
118
        $this->setupEnvironmentProviders();
119
120
        // Manager register is globally available
121
        $this->app->singleton(Register::class, function () {
122
            return new Register();
123
        });
124
125
        (new MacrosServiceProvider($this->app))->register();
126
        (new AuthServiceProvider($this->app))->register();
127
        (new EventServiceProvider($this->app))->register();
128
        (new ViewServiceProvider($this->app))->register();
129
        (new SquantoServiceProvider($this->app))->register();
130
        (new SquantoManagerServiceProvider($this->app))->register();
131
        (new SettingsServiceProvider($this->app))->register();
132
133
        (new AssetLibraryServiceProvider($this->app))->register();
134
135
        // Project defaults
136
        (new ChiefProjectServiceProvider($this->app))->register();
137
    }
138
139
    /**
140
     * Conditionally loads providers for specific environments.
141
     *
142
     * The app()->register() will both trigger the register and boot
143
     * methods of the service provider
144
     */
145
    private function setupEnvironmentProviders()
146
    {
147
        if (!$this->app->environment('production') && $services = config('app.providers-' . app()->environment(), false)) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

147
        if (!$this->app->environment('production') && $services = config('app.providers-' . app()->/** @scrutinizer ignore-call */ environment(), false)) {
Loading history...
148
            foreach ($services as $service) {
149
                $this->app->register($service);
150
            }
151
        }
152
    }
153
154
    private function registerChiefGuard()
155
    {
156
        $this->app['config']["auth.guards.chief"] = [
157
            'driver'   => 'session',
158
            'provider' => 'chief',
159
        ];
160
161
        $this->app['config']["auth.providers.chief"] = [
162
            'driver' => 'chief-eloquent',
163
            'model'  => User::class,
164
        ];
165
166
        $this->app['config']["auth.passwords.chief"] = [
167
            'provider' => 'chief',
168
            'table'    => 'chief_password_resets',
169
            'expire'   => 60,
170
        ];
171
172
        // Custom models for permission
173
        $this->app['config']['permission.models'] = [
174
            'permission' => \Thinktomorrow\Chief\Authorization\Permission::class,
175
            'role'       => \Thinktomorrow\Chief\Authorization\Role::class,
176
        ];
177
    }
178
179
    private function autoloadRoute()
180
    {
181
        if (true !== config('thinktomorrow.chief.route.autoload')) {
182
            return;
183
        }
184
185
        app()->booted(function () {
0 ignored issues
show
introduced by
The method booted() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

185
        app()->/** @scrutinizer ignore-call */ booted(function () {
Loading history...
186
            $routeName = config('thinktomorrow.chief.route.name');
187
188
            Route::get('{slug?}', function ($slug = '/') use ($routeName) {
0 ignored issues
show
Unused Code introduced by
The import $routeName is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
189
                return ChiefResponse::fromSlug($slug);
190
            })->name($routeName)
191
              ->where('slug', '(.*)?')
192
              ->middleware('web');
193
        });
194
    }
195
196
    private function autoloadMiddleware()
197
    {
198
        app(Router::class)->middlewareGroup('web-chief', [
199
            AuthenticateChiefSession::class,
200
            MonitorMiddleware::class,
201
        ]);
202
203
        app(Router::class)->aliasMiddleware('chief-guest', ChiefRedirectIfAuthenticated::class);
204
        app(Router::class)->aliasMiddleware('chief-validate-invite', ChiefValidateInvite::class);
205
    }
206
}
207