Passed
Pull Request — master (#2)
by Takafumi
05:37
created

ServiceProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 99.01%

Importance

Changes 0
Metric Value
eloc 99
dl 0
loc 190
ccs 100
cts 101
cp 0.9901
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configPath() 0 3 1
B boot() 0 102 7
A getRouter() 0 3 1
A getFilesystem() 0 3 1
A register() 0 45 1
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 Intervention\Httpauth\Httpauth;
9
use Intervention\Httpauth\HttpauthServiceProvider;
10
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
11
use Enomotodev\LaractiveAdmin\Http\Middleware\LaractiveAdminAuthenticate;
12
use Enomotodev\LaractiveAdmin\Http\Middleware\HttpauthAuthenticate;
13
use Enomotodev\LaractiveAdmin\Http\Middleware\SharingDataWithAllViews;
14
use Enomotodev\LaractiveAdmin\Console\InstallCommand;
15
use Enomotodev\LaractiveAdmin\Console\UninstallCommand;
16
use Enomotodev\LaractiveAdmin\Console\SeedCommand;
17
18
class ServiceProvider extends BaseServiceProvider
19
{
20
    /**
21
     * Bootstrap the application events.
22
     *
23
     * @return void
24
     */
25 11
    public function boot()
26
    {
27 11
        $this->publishes([$this->configPath() => config_path('laractive-admin.php')], 'config');
28
29 11
        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'laractive-admin');
30
31
        $routeConfig = [
32 11
            'middleware' => ['web', 'laractive-admin', 'httpauth', 'sharing-data'],
33 11
            'namespace' => 'App\Admin',
34 11
            'prefix' => $this->app['config']->get('laractive-admin.route_prefix'),
35 11
            'as' => 'admin.',
36
            'where' => [
37
                'id' => '[0-9]+',
38
            ],
39
        ];
40
41 11
        if (is_dir($directory = app_path('Admin'))) {
42 11
            $this->getRouter()->group($routeConfig, function ($router) {
43
                /** @var $router \Illuminate\Routing\Router */
44 11
                $files = $this->getFilesystem()->allFiles(app_path('Admin'));
45
46 11
                foreach ($files as $file) {
47 11
                    $filename = $file->getFilename();
48 11
                    $className = substr($filename, 0, -4);
49 11
                    $adminClassName = "\App\Admin\\{$className}";
50 11
                    $adminClass = new $adminClassName;
51 11
                    if ($adminClass->model) {
52 11
                        $model = new $adminClass->model;
53 11
                        $routePrefix = $model->getTable();
54 11
                        $router->get("{$routePrefix}", [
55 11
                            'uses' => "\App\Admin\\{$className}@index",
56 11
                            'as' => "{$routePrefix}.index",
57
                        ]);
58 11
                        $router->get("{$routePrefix}/{id}", [
59 11
                            'uses' => "\App\Admin\\{$className}@show",
60 11
                            'as' => "{$routePrefix}.show",
61
                        ]);
62 11
                        $router->get("{$routePrefix}/new", [
63 11
                            'uses' => "\App\Admin\\{$className}@new",
64 11
                            'as' => "{$routePrefix}.new",
65
                        ]);
66 11
                        $router->post("{$routePrefix}", [
67 11
                            'uses' => "\App\Admin\\{$className}@create",
68 11
                            'as' => "{$routePrefix}.create",
69
                        ]);
70 11
                        $router->get("{$routePrefix}/{id}/edit", [
71 11
                            'uses' => "\App\Admin\\{$className}@edit",
72 11
                            'as' => "{$routePrefix}.edit",
73
                        ]);
74 11
                        $router->put("{$routePrefix}/{id}", [
75 11
                            'uses' => "\App\Admin\\{$className}@update",
76 11
                            'as' => "{$routePrefix}.update",
77
                        ]);
78 11
                        $router->delete("{$routePrefix}/{id}", [
79 11
                            'uses' => "\App\Admin\\{$className}@destroy",
80 11
                            'as' => "{$routePrefix}.destroy",
81
                        ]);
82 11
                        $router->post("{$routePrefix}/{id}/comments", [
83 11
                            'uses' => "\App\Admin\\{$className}@comments",
84 11
                            'as' => "{$routePrefix}.comments",
85
                        ]);
86
                    }
87
88 11
                    foreach ($adminClassName::$actions as $route) {
0 ignored issues
show
Bug introduced by
The property actions does not exist on string.
Loading history...
89 11
                        $routePrefix = $adminClass->model ? $model->getTable() : strtolower($className);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $model does not seem to be defined for all execution paths leading up to this point.
Loading history...
90 11
                        $router->{$route['method']}($route['uri'], [
91 11
                            'uses' => "\App\Admin\\{$className}@{$route['action']}",
92 11
                            'as' => "{$route['as']}",
93
                        ]);
94
                    }
95
96 11
                    if ($className !== 'Dashboard') {
97 11
                        app(Menu::class)->setPage([
98 11
                            'name' => $className,
99 11
                            'url' => route("admin.{$routePrefix}.index"),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $routePrefix does not seem to be defined for all execution paths leading up to this point.
Loading history...
100
                        ]);
101
                    }
102
                }
103 11
            });
104
        }
105
106
        // Authentication
107 11
        $this->getRouter()->group([
108 11
            'middleware' => ['web', 'httpauth'],
109 11
            'prefix' => $this->app['config']->get('laractive-admin.route_prefix'),
110 11
        ], function ($router) {
111
            /** @var $router \Illuminate\Routing\Router */
112 11
            $router->get('login', [
113 11
                '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...
114
                'as' => 'admin.login',
115
            ]);
116 11
            $router->post('login', [
117 11
                '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...
118
            ]);
119 11
            $router->get('logout', [
120 11
                '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...
121
                'as' => 'admin.logout',
122
            ]);
123 11
        });
124
125 11
        \Illuminate\Database\Eloquent\Builder::macro('comments', function () {
126 4
            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

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