Passed
Push — main ( 275957...473697 )
by PRATIK
04:03
created

AdmineticServiceProvider::publishResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 25
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Pratiksh\Adminetic\Providers;
4
5
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Blade;
9
use Illuminate\Support\Facades\Gate;
10
use Illuminate\Support\Facades\Route;
11
use Illuminate\Support\ServiceProvider;
12
use Livewire\Livewire;
13
use Pratiksh\Adminetic\Console\Commands\AdmineticDummyCommand;
14
use Pratiksh\Adminetic\Console\Commands\InstallAdmineticCommand;
15
use Pratiksh\Adminetic\Console\Commands\MakeCRUDGeneratorCommand;
16
use Pratiksh\Adminetic\Console\Commands\MakePermissionCommand;
17
use Pratiksh\Adminetic\Console\Commands\MakeRepositoryPatternCommand;
18
use Pratiksh\Adminetic\Console\Commands\MakeServiceCommand;
19
use Pratiksh\Adminetic\Console\Commands\MakeSuperUserCommand;
20
use Pratiksh\Adminetic\Console\Commands\MakeTraitCommand;
21
use Pratiksh\Adminetic\Contracts\PermissionRepositoryInterface;
22
use Pratiksh\Adminetic\Contracts\PreferenceRepositoryInterface;
23
use Pratiksh\Adminetic\Contracts\ProfileRepositoryInterface;
24
use Pratiksh\Adminetic\Contracts\RoleRepositoryInterface;
25
use Pratiksh\Adminetic\Contracts\SettingRepositoryInterface;
26
use Pratiksh\Adminetic\Contracts\UserRepositoryInterface;
27
use Pratiksh\Adminetic\Http\Livewire\Admin\Profile\EditAccount;
28
use Pratiksh\Adminetic\Http\Livewire\Admin\Profile\EditProfile;
29
use Pratiksh\Adminetic\Http\Livewire\Admin\UserPreferences;
30
use Pratiksh\Adminetic\Http\Middleware\RoleMiddleware;
31
use Pratiksh\Adminetic\Mixins\AdmineticAuthMixins;
32
use Pratiksh\Adminetic\Models\Admin\Preference;
33
use Pratiksh\Adminetic\Models\Permission;
0 ignored issues
show
Bug introduced by
The type Pratiksh\Adminetic\Models\Permission was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
use Pratiksh\Adminetic\Models\Role;
0 ignored issues
show
Bug introduced by
The type Pratiksh\Adminetic\Models\Role was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use Pratiksh\Adminetic\Models\Setting;
0 ignored issues
show
Bug introduced by
The type Pratiksh\Adminetic\Models\Setting was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
use Pratiksh\Adminetic\Policies\PermissionPolicy;
37
use Pratiksh\Adminetic\Policies\PreferencePolicy;
38
use Pratiksh\Adminetic\Policies\RolePolicy;
39
use Pratiksh\Adminetic\Policies\SettingPolicy;
40
use Pratiksh\Adminetic\Policies\UserPolicy;
41
use Pratiksh\Adminetic\Repositories\PermissionRepository;
42
use Pratiksh\Adminetic\Repositories\PreferenceRepository;
43
use Pratiksh\Adminetic\Repositories\ProfileRepository;
44
use Pratiksh\Adminetic\Repositories\RoleRepository;
45
use Pratiksh\Adminetic\Repositories\SettingRepository;
46
use Pratiksh\Adminetic\Repositories\UserRepository;
47
use Pratiksh\Adminetic\Services\Adminetic;
48
use Pratiksh\Adminetic\View\Components\Action;
49
use Pratiksh\Adminetic\View\Components\Card;
50
use Pratiksh\Adminetic\View\Components\CreatePage;
51
use Pratiksh\Adminetic\View\Components\EditAddButton;
52
use Pratiksh\Adminetic\View\Components\EditPage;
53
use Pratiksh\Adminetic\View\Components\IndexPage;
54
use Pratiksh\Adminetic\View\Components\ShowPage;
55
use Pratiksh\Adminetic\View\Components\Sidebar;
56
57
class AdmineticServiceProvider extends ServiceProvider
58
{
59
    // Register Policies
60
    protected $policies = [
61
        User::class => UserPolicy::class,
62
        Permission::class => PermissionPolicy::class,
63
        Role::class => RolePolicy::class,
64
        Setting::class => SettingPolicy::class,
65
        Preference::class => PreferencePolicy::class,
66
    ];
67
    /**
68
     * The path to the "home" route for your application.
69
     *
70
     * This is used by Laravel authentication to redirect users after login.
71
     *
72
     * @var string
73
     */
74
    public const HOME = '/admin/dashboard';
75
76
    /**
77
     * Bootstrap services.
78
     *
79
     * @return void
80
     */
81
    public function boot()
82
    {
83
        // Publish Ressource
84
        if ($this->app->runningInConsole()) {
85
            $this->publishResource();
86
        }
87
        // Register Resources
88
        $this->registerResource();
89
        // Register Middleware
90
        $this->registerMiddleware();
91
        // Register Directives
92
        $this->directives();
93
        // Register Policies
94
        $this->registerPolicies();
95
        // Register View Components
96
        $this->registerComponents();
97
        // Register Livewire Component
98
        $this->registerLivewire();
99
    }
100
101
    /**
102
     * Register services.
103
     *
104
     * @return void
105
     */
106
    public function register()
107
    {
108
        // Register Package Commands
109
        $this->registerCommands();
110
        /* Repository Interface Binding */
111
        $this->repos();
112
        // Register Mixins
113
        Route::mixin(new AdmineticAuthMixins());
114
        // Register Package Service Providers
115
        $this->app->register(AdmineticEventServiceProvider::class);
116
        // Register Facades
117
        $this->getFacades();
118
    }
119
120
    /**
121
     * Publish Package Resource.
122
     *
123
     *@return void
124
     */
125
    protected function publishResource()
126
    {
127
        // Publish Config File
128
        $this->publishes([
129
            __DIR__ . '/../../config/adminetic.php' => config_path('adminetic.php'),
130
        ], 'adminetic-config');
131
        // Publish View Files
132
        $this->publishes([
133
            __DIR__ . '/../../resources/views' => resource_path('views/vendor/adminetic'),
134
        ], 'adminetic-views');
135
        // Publish Migration Files
136
        $this->publishes([
137
            __DIR__ . '/../../database/migrations' => database_path('migrations'),
138
        ], 'adminetic-migrations');
139
        // Publish Database Seeds
140
        $this->publishes([
141
            __DIR__ . '/../../database/seeders' => database_path('seeders'),
142
        ], 'adminetic-seeders');
143
        $this->publishes([
144
            __DIR__ . '/../../payload/assets' => public_path('adminetic/assets'),
145
        ], 'adminetic-assets-files');
146
        // Publish Static Files
147
        $this->publishes([
148
            __DIR__ . '/../../payload/static' => public_path('adminetic/static'),
149
        ], 'adminetic-static-files');
150
    }
151
152
    /**
153
     * Register Package Resource.
154
     *
155
     *@return void
156
     */
157
    protected function registerResource()
158
    {
159
        $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations'); // Loading Migration Files
160
        $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'adminetic'); // Loading Views Files
161
        $this->registerRoutes();
162
    }
163
164
    /**
165
     * Register Routes.
166
     *
167
     * @return void
168
     */
169
    protected function registerRoutes()
170
    {
171
        Route::group($this->routeConfiguration(), function () {
0 ignored issues
show
Bug introduced by
$this->routeConfiguration() of type void is incompatible with the type Closure|array|string expected by parameter $attributes of Illuminate\Support\Facades\Route::group(). ( Ignorable by Annotation )

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

171
        Route::group(/** @scrutinizer ignore-type */ $this->routeConfiguration(), function () {
Loading history...
Bug introduced by
Are you sure the usage of $this->routeConfiguration() targeting Pratiksh\Adminetic\Provi...r::routeConfiguration() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
172
            $this->loadRoutesFrom(__DIR__ . '/../../routes/web.php');
173
        });
174
    }
175
176
    /**
177
     * Register Route Configuration.
178
     *
179
     * @return void
180
     */
181
    protected function routeConfiguration()
182
    {
183
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('prefix' =>... array('web', 'auth'))) returns the type array which is incompatible with the documented return type void.
Loading history...
184
            'prefix' => config('adminetic.prefix', 'admin'),
185
            'middleware' => config('adminetic.middleware', ['web', 'auth']),
186
        ];
187
    }
188
189
    /**
190
     * Register Package Command.
191
     *
192
     *@return void
193
     */
194
    protected function registerCommands()
195
    {
196
        $this->commands([
197
            MakeCRUDGeneratorCommand::class,
198
            MakePermissionCommand::class,
199
            MakeRepositoryPatternCommand::class,
200
            MakeServiceCommand::class,
201
            MakeSuperUserCommand::class,
202
            MakeTraitCommand::class,
203
            InstallAdmineticCommand::class,
204
            AdmineticDummyCommand::class,
205
        ]);
206
    }
207
208
    /**
209
     * Blade Directives.
210
     *
211
     * @return void
212
     */
213
    protected function directives()
214
    {
215
        Blade::if('hasRole', function ($roles) {
216
            $hasAccess = false;
217
            $roles_array = explode('|', $roles);
218
            foreach ($roles_array as $role) {
219
                $hasAccess = $hasAccess || Auth::user()->hasRole($role) || Auth::user()->isSuperAdmin();
0 ignored issues
show
Bug introduced by
The method hasRole() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

219
                $hasAccess = $hasAccess || Auth::user()->/** @scrutinizer ignore-call */ hasRole($role) || Auth::user()->isSuperAdmin();
Loading history...
Bug introduced by
The method isSuperAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

219
                $hasAccess = $hasAccess || Auth::user()->hasRole($role) || Auth::user()->/** @scrutinizer ignore-call */ isSuperAdmin();
Loading history...
220
            }
221
222
            return $hasAccess;
223
        });
224
        Blade::if('preference', function ($preference_name, $default_value) {
225
            return preference($preference_name, $default_value);
226
        });
227
        Blade::directive('setting', function ($setting_name) {
228
            return "<?php echo setting($setting_name) ?>";
229
        });
230
    }
231
232
    /**
233
     * Repository Binding.
234
     *
235
     * @return void
236
     */
237
    protected function repos()
238
    {
239
        $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
240
        $this->app->bind(ProfileRepositoryInterface::class, ProfileRepository::class);
241
        $this->app->bind(RoleRepositoryInterface::class, RoleRepository::class);
242
        $this->app->bind(PermissionRepositoryInterface::class, PermissionRepository::class);
243
        $this->app->bind(SettingRepositoryInterface::class, SettingRepository::class);
244
        $this->app->bind(PreferenceRepositoryInterface::class, PreferenceRepository::class);
245
    }
246
247
    /**
248
     * Register Middlewares.
249
     *
250
     *@return void
251
     */
252
    protected function registerMiddleware()
253
    {
254
        $router = $this->app->make(Router::class);
255
        $router->aliasMiddleware('role', RoleMiddleware::class);
256
    }
257
258
    /**
259
     * Register View Components.
260
     *
261
     *@return void
262
     */
263
    protected function registerComponents()
264
    {
265
        $this->loadViewComponentsAs('adminetic', [
266
            Sidebar::class,
267
            Action::class,
268
            Card::class,
269
            CreatePage::class,
270
            EditPage::class,
271
            IndexPage::class,
272
            ShowPage::class,
273
            EditAddButton::class,
274
        ]);
275
    }
276
277
    /**
278
     * Register Policies.
279
     *
280
     *@return void
281
     */
282
    protected function registerPolicies()
283
    {
284
        foreach ($this->policies as $key => $value) {
285
            Gate::policy($key, $value);
286
        }
287
    }
288
289
    /**
290
     * Register Livewire Components.
291
     */
292
    protected function registerLivewire()
293
    {
294
        Livewire::component('admin.user-preferences', UserPreferences::class);
295
        Livewire::component('admin.profile.edit-account', EditAccount::class);
296
        Livewire::component('admin.profile.edit-profile', EditProfile::class);
297
    }
298
299
    /**
300
     *
301
     * Facades
302
     *
303
     */
304
    protected function getFacades()
305
    {
306
        $this->app->bind('adminetic', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app 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

306
        $this->app->bind('adminetic', function (/** @scrutinizer ignore-unused */ $app) {

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...
307
            return new Adminetic();
308
        });
309
    }
310
}
311