Completed
Push — master ( 58ad96...f173b1 )
by Takafumi
04:38
created

ServiceProvider::boot()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 100
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 61
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 65
nc 2
nop 0
dl 0
loc 100
ccs 61
cts 62
cp 0.9839
crap 4
rs 8.1935
c 0
b 0
f 0

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 Enomotodev\LaractiveAdmin;
4
5
use Collective\Html\HtmlServiceProvider;
6
use Collective\Html\FormFacade;
7
use Collective\Html\HtmlFacade;
8
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
9
use Enomotodev\LaractiveAdmin\Http\Middleware\LaractiveAdminAuthenticate;
10
use Enomotodev\LaractiveAdmin\Console\InstallCommand;
11
use Enomotodev\LaractiveAdmin\Console\UninstallCommand;
12
use Enomotodev\LaractiveAdmin\Console\SeedCommand;
13
14
class ServiceProvider extends BaseServiceProvider
15
{
16
    /**
17
     * Bootstrap the application events.
18
     *
19
     * @return void
20
     */
21 7
    public function boot()
22
    {
23 7
        $this->publishes([$this->configPath() => config_path('laractive-admin.php')], 'config');
24
25 7
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'laractive-admin');
26
27
        $routeConfig = [
28 7
            'middleware' => ['web', 'laractive-admin'],
29 7
            'namespace' => 'App\Admin',
30 7
            'prefix' => $this->app['config']->get('laractive-admin.route_prefix'),
31 7
            'as' => 'admin.',
32
            'where' => [
33
                'id' => '[0-9]+',
34
            ],
35
        ];
36
37 7
        if (is_dir($directory = app_path('Admin'))) {
38 7
            $this->getRouter()->group($routeConfig, function ($router) {
39
                /** @var $router \Illuminate\Routing\Router */
40 7
                $files = $this->getFilesystem()->allFiles(app_path('Admin'));
41
42 7
                foreach ($files as $file) {
43 7
                    if ($file->getFilename() === 'DashboardController.php') {
44
                        continue;
45
                    }
46
47 7
                    $filename = $file->getFilename();
48 7
                    $className = substr($filename, 0, -4);
49 7
                    $adminClassName = "\App\Admin\\{$className}";
50 7
                    $adminClass = new $adminClassName;
51 7
                    $model = new $adminClass->model;
52 7
                    $routePrefix = $model->getTable();
53 7
                    $router->get("{$routePrefix}", [
54 7
                        'uses' => "\App\Admin\\{$className}@index",
55 7
                        'as' => "{$routePrefix}.index",
56
                    ]);
57 7
                    $router->get("{$routePrefix}/{id}", [
58 7
                        'uses' => "\App\Admin\\{$className}@show",
59 7
                        'as' => "{$routePrefix}.show",
60
                    ]);
61 7
                    $router->get("{$routePrefix}/new", [
62 7
                        'uses' => "\App\Admin\\{$className}@new",
63 7
                        'as' => "{$routePrefix}.new",
64
                    ]);
65 7
                    $router->post("{$routePrefix}", [
66 7
                        'uses' => "\App\Admin\\{$className}@create",
67 7
                        'as' => "{$routePrefix}.create",
68
                    ]);
69 7
                    $router->get("{$routePrefix}/{id}/edit", [
70 7
                        'uses' => "\App\Admin\\{$className}@edit",
71 7
                        'as' => "{$routePrefix}.edit",
72
                    ]);
73 7
                    $router->put("{$routePrefix}/{id}", [
74 7
                        'uses' => "\App\Admin\\{$className}@update",
75 7
                        'as' => "{$routePrefix}.update",
76
                    ]);
77 7
                    $router->delete("{$routePrefix}/{id}", [
78 7
                        'uses' => "\App\Admin\\{$className}@destroy",
79 7
                        'as' => "{$routePrefix}.destroy",
80
                    ]);
81 7
                    $router->post("{$routePrefix}/{id}/comments", [
82 7
                        'uses' => "\App\Admin\\{$className}@comments",
83 7
                        'as' => "{$routePrefix}.comments",
84
                    ]);
85
86 7
                    app(Menu::class)->setPage([
87 7
                        'name' => $className,
88 7
                        'url' => route("admin.{$routePrefix}.index"),
89
                    ]);
90
                }
91
92
                // Dashboard
93 7
                $router->get('/', [
94 7
                    'uses' => '\Enomotodev\LaractiveAdmin\Http\Controllers\DashboardController@index',
0 ignored issues
show
Bug introduced by
The controller App\Http\Controllers\\En...ers\DashboardController does not seem to exist.

If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.

If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed:

$app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
    $app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
    });
});
Loading history...
95
                    'as' => 'dashboard.index',
96
                ]);
97 7
            });
98
        }
99
100
        // Authentication
101 7
        $this->getRouter()->group([
102 7
            'middleware' => ['web'],
103 7
            'prefix' => $this->app['config']->get('laractive-admin.route_prefix'),
104 7
        ], function($router) {
105
            /** @var $router \Illuminate\Routing\Router */
106 7
            $router->get('login', [
107 7
                'uses' => '\Enomotodev\LaractiveAdmin\Http\Controllers\Auth\LoginController@showLoginForm',
0 ignored issues
show
Bug introduced by
The controller App\Http\Controllers\\En...rs\Auth\LoginController does not seem to exist.

If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.

If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed:

$app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
    $app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
    });
});
Loading history...
108
                'as' => 'admin.login',
109
            ]);
110 7
            $router->post('login', [
111 7
                'uses' => '\Enomotodev\LaractiveAdmin\Http\Controllers\Auth\LoginController@login',
0 ignored issues
show
Bug introduced by
The controller App\Http\Controllers\\En...rs\Auth\LoginController does not seem to exist.

If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.

If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed:

$app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
    $app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
    });
});
Loading history...
112
            ]);
113 7
            $router->get('logout', [
114 7
                'uses' => '\Enomotodev\LaractiveAdmin\Http\Controllers\Auth\LoginController@logout',
0 ignored issues
show
Bug introduced by
The controller App\Http\Controllers\\En...rs\Auth\LoginController does not seem to exist.

If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.

If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed:

$app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
    $app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
        $app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
    });
});
Loading history...
115
                'as' => 'admin.logout',
116
            ]);
117 7
        });
118
119 7
        \Illuminate\Database\Eloquent\Builder::macro('comments', function () {
120 2
            return $this->getModel()->morphMany(LaractiveAdminComment::class, 'commentable');
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on Enomotodev\LaractiveAdmin\ServiceProvider. ( Ignorable by Annotation )

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

120
            return $this->/** @scrutinizer ignore-call */ getModel()->morphMany(LaractiveAdminComment::class, 'commentable');

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...
121 7
        });
122 7
    }
123
124
    /**
125
     * Register a service provider with the application.
126
     *
127
     * @return void
128
     */
129 7
    public function register()
130
    {
131 7
        $this->mergeConfigFrom($this->configPath(), 'laractive-admin');
132
133 7
        $this->app->register(HtmlServiceProvider::class);
134
135 7
        $this->app->alias('Form', FormFacade::class);
136 7
        $this->app->alias('Html', HtmlFacade::class);
137
138 7
        $this->app->singleton(Menu::class);
139
140 7
        $this->app['config']['auth.guards'] += [
141
            'laractive-admin' => [
142
                'driver' => 'session',
143
                'provider' => 'admin_users',
144
            ],
145
        ];
146 7
        $this->app['config']['auth.providers'] += [
147
            'admin_users' => [
148
                'driver' => 'eloquent',
149
                'model' => AdminUser::class,
150
            ],
151
        ];
152
153 7
        $this->getRouter()->aliasMiddleware('laractive-admin', LaractiveAdminAuthenticate::class);
154
155 7
        $this->app->singleton('command.laractive-admin.install', function ($app) {
156 7
            return new InstallCommand($app['files'], $app['composer']);
157 7
        });
158 7
        $this->app->singleton('command.laractive-admin.uninstall', function ($app) {
159 7
            return new UninstallCommand($app['files'], $app['composer']);
160 7
        });
161 7
        $this->app->singleton('command.laractive-admin.seed', function () {
162 7
            return new SeedCommand;
163 7
        });
164
165 7
        $this->commands(['command.laractive-admin.install']);
166 7
        $this->commands(['command.laractive-admin.uninstall']);
167 7
        $this->commands(['command.laractive-admin.seed']);
168 7
    }
169
170
    /**
171
     * @return string
172
     */
173 7
    protected function configPath()
174
    {
175 7
        return __DIR__ . '/../config/laractive-admin.php';
176
    }
177
178
    /**
179
     * Get the active router.
180
     *
181
     * @return \Illuminate\Routing\Router
182
     */
183 7
    protected function getRouter()
184
    {
185 7
        return $this->app['router'];
186
    }
187
188
    /**
189
     * Get the filesystem.
190
     *
191
     * @return \Illuminate\Filesystem\Filesystem
192
     */
193 7
    protected function getFilesystem()
194
    {
195 7
        return $this->app['files'];
196
    }
197
}
198