Passed
Push — master ( ca020b...6b9629 )
by Andrey
13:56
created

ServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 44
dl 0
loc 98
rs 10
c 5
b 1
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
A boot() 0 8 1
A blade() 0 40 2
A can() 0 11 2
A publishConfig() 0 5 1
A bootCommands() 0 7 1
A publishMigrations() 0 5 1
1
<?php
2
3
namespace Helldar\Roles;
4
5
use Helldar\Roles\Console\PermissionCreate;
6
use Helldar\Roles\Console\PermissionDelete;
7
use Helldar\Roles\Console\RoleCreate;
8
use Helldar\Roles\Console\RoleDelete;
9
use Helldar\Roles\Facades\Config;
10
use Helldar\Roles\Models\Permission;
11
use Helldar\Roles\Traits\Searchable;
12
use Illuminate\Contracts\Auth\Authenticatable;
13
use Illuminate\Support\Facades\Blade;
14
use Illuminate\Support\Facades\Gate;
15
16
class ServiceProvider extends \Illuminate\Support\ServiceProvider
17
{
18
    use Searchable;
19
20
    public function boot()
21
    {
22
        $this->publishConfig();
23
        $this->publishMigrations();
24
        $this->bootCommands();
25
26
        $this->blade();
27
        $this->can();
28
    }
29
30
    public function register()
31
    {
32
        $this->mergeConfigFrom(__DIR__ . '/../config/roles.php', Config::name());
33
    }
34
35
    protected function publishMigrations()
36
    {
37
        $this->publishes([
38
            __DIR__ . '/../database/migrations' => database_path('migrations'),
39
        ], 'migrations');
40
    }
41
42
    protected function publishConfig()
43
    {
44
        $this->publishes([
45
            __DIR__ . '/../config/roles.php' => config_path(Config::filename()),
46
        ], 'config');
47
    }
48
49
    protected function blade()
50
    {
51
        if (! Config::useBlade()) {
52
            return;
53
        }
54
55
        /* Role */
56
        Blade::directive('role', function ($role) {
57
            return "<?php if(\auth()->check() && \auth()->user()->hasRole($role)) { ?>";
58
        });
59
60
        Blade::directive('endrole', function () {
61
            return '<?php } ?>';
62
        });
63
64
        /* Roles */
65
        Blade::directive('role', function ($roles) {
66
            return "<?php if(\auth()->check() && \auth()->user()->hasRoles($roles)) { ?>";
67
        });
68
69
        Blade::directive('endroles', function () {
70
            return '<?php } ?>';
71
        });
72
73
        /* Permission */
74
        Blade::directive('permission', function ($permission) {
75
            return "<?php if(\auth()->check() && \auth()->user()->hasPermission($permission)) ?>";
76
        });
77
78
        Blade::directive('endpermission', function () {
79
            return '<?php } ?>';
80
        });
81
82
        /* Permissions */
83
        Blade::directive('permissions', function ($permissions) {
84
            return "<?php if(\auth()->check() && \auth()->user()->hasPermissions($permissions)) ?>";
85
        });
86
87
        Blade::directive('endpermissions', function () {
88
            return '<?php } ?>';
89
        });
90
    }
91
92
    protected function can()
93
    {
94
        if (! Config::useCanDirective()) {
95
            return;
96
        }
97
98
        Permission::query()
99
            ->get(['slug'])
100
            ->each(function (Permission $permission) {
101
                Gate::define($permission->slug, function (Authenticatable $user) use ($permission) {
102
                    return $user->hasPermission($permission);
0 ignored issues
show
Bug introduced by
The method hasPermission() 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

102
                    return $user->/** @scrutinizer ignore-call */ hasPermission($permission);
Loading history...
103
                });
104
            });
105
    }
106
107
    protected function bootCommands()
108
    {
109
        $this->commands([
110
            PermissionCreate::class,
111
            PermissionDelete::class,
112
            RoleCreate::class,
113
            RoleDelete::class,
114
        ]);
115
    }
116
}
117