Completed
Pull Request — master (#431)
by Manel
28:26
created

rovider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Acacha\AdminLTETemplateLaravel\Providers;
4
5
use Illuminate\Routing\Router;
6
use Illuminate\Container\Container;
7
use Illuminate\Support\ServiceProvider;
8
use Creativeorange\Gravatar\Facades\Gravatar;
9
use Acacha\AdminLTETemplateLaravel\Facades\AdminLTE;
10
use Creativeorange\Gravatar\GravatarServiceProvider;
11
use Acacha\AdminLTETemplateLaravel\Http\Middleware\GuestUser;
12
13
/**
14
 * Class AdminLTETemplateServiceProvider.
15
 */
16
class AdminLTETemplateServiceProvider extends ServiceProvider
17
{
18
    
19
    /**
20
     * Register the application services.
21
     */
22
    public function register()
23
    {
24
        if (!defined('ADMINLTETEMPLATE_PATH')) {
25
            define('ADMINLTETEMPLATE_PATH', realpath(__DIR__.'/../../'));
26
        }
27
28
        if ($this->app->runningInConsole()) {
29
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\PublishAdminLTE::class]);
30
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\PublishAdminLTEAlt::class]);
31
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\PublishAdminLTESidebar::class]);
32
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\PublishAdminLTESidebarAlt::class]);
33
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\MakeAdminUserSeeder::class]);
34
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\AdminLTEAdmin::class]);
35
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\AdminLTEAdminAlt::class]);
36
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\MakeView::class]);
37
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\AdminLTEMenu::class]);
38
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\AdminLTEMenuAlt::class]);
39
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\MakeRoute::class]);
40
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\MakeMenu::class]);
41
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\MakeV::class]);
42
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\MakeVC::class]);
43
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\MakeMVC::class]);
44
            $this->commands([\Acacha\AdminLTETemplateLaravel\Console\Username::class]);
45
        }
46
47
        $this->app->bind('AdminLTE', function () {
48
            return new \Acacha\AdminLTETemplateLaravel\AdminLTE();
49
        });
50
51
        if (config('adminlte.gravatar', true)) {
52
            $this->registerGravatarServiceProvider();
53
        }
54
55
        if (config('auth.providers.users.field', 'email') === 'username'  &&
56
            config('adminlte.add_nullable_username', true)) {
57
            $this->loadMigrationsFrom(ADMINLTETEMPLATE_PATH .'/database/migrations/username_login');
58
        }
59
    }
60
61
    /**
62
     * Register Gravatar Service Provider.
63
     */
64
    protected function registerGravatarServiceProvider()
65
    {
66
        $this->app->register(GravatarServiceProvider::class);
67
        if (!class_exists('Gravatar')) {
68
            class_alias(Gravatar::class, 'Gravatar');
69
        }
70
    }
71
72
    /**
73
     * Bootstrap the application services.
74
     */
75
    public function boot(Router $router)
76
    {
77
        $router->pushMiddlewareToGroup('web', GuestUser::class);
78
79
        if (config('adminlte.install_routes', true)) {
80
            $this->defineRoutes();
81
        }
82
83
        //Publish
84
        $this->publishHomeController();
85
        $this->changeRegisterController();
86
        $this->changeLoginController();
87
        $this->changeForgotPasswordController();
88
        $this->publishNoGuestForgotPasswordController();
89
        $this->changeResetPasswordController();
90
        $this->publishPublicAssets();
91
        $this->publishViews();
92
        $this->publishResourceAssets();
93
        $this->publishTests();
94
        $this->publishLanguages();
95
        $this->publishGravatar();
96
        $this->publishConfig();
97
        $this->publishWebRoutes();
98
        $this->publishApiRoutes();
99
        $this->publishDusk();
100
        $this->publishDatabaseConfig();
101
102
        $this->enableSpatieMenu();
103
    }
104
105
    /**
106
     * Define the AdminLTETemplate routes.
107
     */
108
    protected function defineRoutes()
109
    {
110
        if (!$this->app->routesAreCached()) {
111
            $router = app('router');
112
113
            $router->group(['namespace' => Container::getInstance()->getNamespace().'Http\Controllers'], function () {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Illuminate\Container\Container as the method getNamespace() does only exist in the following sub-classes of Illuminate\Container\Container: Illuminate\Foundation\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
114
                require __DIR__.'/../Http/routes.php';
115
            });
116
        }
117
    }
118
119
    /**
120
     * Publish Home Controller.
121
     */
122
    private function publishHomeController()
123
    {
124
        $this->publishes(AdminLTE::homeController(), 'adminlte');
125
    }
126
127
    /**
128
     * Change default Laravel RegisterController.
129
     */
130
    private function changeRegisterController()
131
    {
132
        $this->publishes(AdminLTE::registerController(), 'adminlte');
133
    }
134
135
    /**
136
     * Change default Laravel LoginController.
137
     */
138
    private function changeLoginController()
139
    {
140
        $this->publishes(AdminLTE::loginController(), 'adminlte');
141
    }
142
143
    /**
144
     * Change default Laravel forgot password Controller.
145
     */
146
    private function changeForgotPasswordController()
147
    {
148
        $this->publishes(AdminLTE::forgotPasswordController(), 'adminlte');
149
    }
150
151
    /**
152
     * Publish no guest forgot password Controller.
153
     */
154
    private function publishNoGuestForgotPasswordController()
155
    {
156
        $this->publishes(AdminLTE::noGuestForgotPasswordController(), 'adminlte');
157
    }
158
159
    /**
160
     * Change default Laravel reset password Controller.
161
     */
162
    private function changeResetPasswordController()
163
    {
164
        $this->publishes(AdminLTE::resetPasswordController(), 'adminlte');
165
    }
166
167
    /**
168
     * Publish public resource assets to Laravel project.
169
     */
170
    private function publishPublicAssets()
171
    {
172
        $this->publishes(AdminLTE::publicAssets(), 'adminlte');
173
    }
174
175
    /**
176
     * Publish package views to Laravel project.
177
     */
178
    private function publishViews()
179
    {
180
        $this->loadViewsFrom(ADMINLTETEMPLATE_PATH.'/resources/views/', 'adminlte');
181
182
        $this->publishes(AdminLTE::views(), 'adminlte');
183
    }
184
185
    /**
186
     * Publish package resource assets to Laravel project.
187
     */
188
    private function publishResourceAssets()
189
    {
190
        $this->publishes(AdminLTE::resourceAssets(), 'adminlte');
191
    }
192
193
    /**
194
     * Publish package tests to Laravel project.
195
     */
196
    private function publishTests()
197
    {
198
        $this->publishes(AdminLTE::tests(), 'adminlte');
199
    }
200
201
    /**
202
     * Publish package language to Laravel project.
203
     */
204
    private function publishLanguages()
205
    {
206
        $this->loadTranslationsFrom(ADMINLTETEMPLATE_PATH.'/resources/lang/', 'adminlte_lang');
207
208
        $this->publishes(AdminLTE::languages(), 'adminlte_lang');
209
    }
210
211
    /**
212
     * Publish config Gravatar file using group.
213
     */
214
    private function publishGravatar()
215
    {
216
        $this->publishes(AdminLTE::gravatar(), 'adminlte');
217
    }
218
219
    /**
220
     * Publish adminlte package config.
221
     */
222
    private function publishConfig()
223
    {
224
        $this->publishes(AdminLTE::config(), 'adminlte');
225
    }
226
227
    /**
228
     * Publish routes/web.php file.
229
     */
230
    private function publishWebRoutes()
231
    {
232
        $this->publishes(AdminLTE::webroutes(), 'adminlte');
233
    }
234
235
    /**
236
     * Publish routes/api.php file.
237
     */
238
    private function publishApiRoutes()
239
    {
240
        $this->publishes(AdminLTE::apiroutes(), 'adminlte');
241
    }
242
243
    /**
244
     * Publish dusk tests files.
245
     */
246
    private function publishDusk()
247
    {
248
        $this->publishDuskEnvironment();
249
    }
250
251
    /**
252
     * Publish dusk environment files.
253
     */
254
    private function publishDuskEnvironment()
255
    {
256
        $this->publishes(AdminLTE::duskEnvironment(), 'adminlte');
257
    }
258
259
    /**
260
     * Publish database config files.
261
     */
262
    private function publishDatabaseConfig()
263
    {
264
        $this->publishes(AdminLTE::databaseConfig(), 'adminlte');
265
    }
266
267
    /**
268
     * Enable (if active) spatie menu.
269
     */
270
    private function enableSpatieMenu()
271
    {
272
        if ($this->app->getProvider('Spatie\Menu\Laravel\MenuServiceProvider')) {
0 ignored issues
show
Bug introduced by
The method getProvider() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean getProviders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
273
            require config_path('menu.php');
274
        }
275
    }
276
}
277