Passed
Push — master ( f84414...a4646d )
by Andrey
04:10
created

ServiceProvider::getPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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\Cache;
15
use Illuminate\Support\Facades\Gate;
16
17
class ServiceProvider extends \Illuminate\Support\ServiceProvider
18
{
19
    use Searchable;
20
21
    public function boot()
22
    {
23
        $this->publishConfig();
24
        $this->publishMigrations();
25
        $this->bootCommands();
26
27
        $this->blade();
28
        $this->can();
29
    }
30
31
    public function register()
32
    {
33
        $this->mergeConfigFrom(__DIR__ . '/../config/roles.php', Config::name());
34
    }
35
36
    protected function publishMigrations()
37
    {
38
        $this->publishes([
39
            __DIR__ . '/../database/migrations' => database_path('migrations'),
40
        ], 'migrations');
41
    }
42
43
    protected function publishConfig()
44
    {
45
        $this->publishes([
46
            __DIR__ . '/../config/roles.php' => config_path(Config::filename()),
47
        ], 'config');
48
    }
49
50
    protected function blade()
51
    {
52
        if (! Config::useBlade()) {
53
            return;
54
        }
55
56
        /* Role */
57
        Blade::directive('role', function ($role) {
58
            return "<?php if(\auth()->check() && \auth()->user()->hasRole($role)): ?>";
59
        });
60
61
        Blade::directive('endrole', function () {
62
            return '<?php endif; ?>';
63
        });
64
65
        /* Roles */
66
        Blade::directive('roles', function ($roles) {
67
            return "<?php if(\auth()->check() && \auth()->user()->hasRoles($roles)): ?>";
68
        });
69
70
        Blade::directive('endroles', function () {
71
            return '<?php endif; ?>';
72
        });
73
74
        /* Permission */
75
        Blade::directive('permission', function ($permission) {
76
            return "<?php if(\auth()->check() && \auth()->user()->hasPermission($permission)): ?>";
77
        });
78
79
        Blade::directive('endpermission', function () {
80
            return '<?php endif; ?>';
81
        });
82
83
        /* Permissions */
84
        Blade::directive('permissions', function ($permissions) {
85
            return "<?php if(\auth()->check() && \auth()->user()->hasPermissions($permissions)): ?>";
86
        });
87
88
        Blade::directive('endpermissions', function () {
89
            return '<?php endif; ?>';
90
        });
91
    }
92
93
    protected function can()
94
    {
95
        if (! Config::useCanDirective()) {
96
            return;
97
        }
98
99
        foreach ($this->getPermissions() as $permission) {
100
            Gate::define($permission, function (Authenticatable $user) use ($permission) {
101
                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

101
                return $user->/** @scrutinizer ignore-call */ hasPermission($permission);
Loading history...
102
            });
103
        }
104
    }
105
106
    protected function bootCommands()
107
    {
108
        $this->commands([
109
            PermissionCreate::class,
110
            PermissionDelete::class,
111
            RoleCreate::class,
112
            RoleDelete::class,
113
        ]);
114
    }
115
116
    protected function getPermissions(): array
117
    {
118
        return Cache::remember('permissions-model', Config::cacheTtl(), static function () {
119
            return Permission::query()
120
                ->get(['slug'])
121
                ->pluck('slug')
122
                ->toArray();
123
        });
124
    }
125
}
126