Completed
Pull Request — master (#823)
by Burak
02:27
created

PermissionServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 21.98 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 20
loc 91
rs 10
wmc 8
lcom 1
cbo 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spatie\Permission;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\View\Compilers\BladeCompiler;
7
use Spatie\Permission\Contracts\Role as RoleContract;
8
use Spatie\Permission\Contracts\Permission as PermissionContract;
9
10
class PermissionServiceProvider extends ServiceProvider
11
{
12
    public function boot(PermissionRegistrar $permissionLoader)
13
    {
14
        if (isNotLumen()) {
15
            $this->publishes([
16
                __DIR__.'/../config/permission.php' => config_path('permission.php'),
17
            ], 'config');
18
19
            if (! class_exists('CreatePermissionTables')) {
20
                $timestamp = date('Y_m_d_His', time());
21
22
                $this->publishes([
23
                    __DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_permission_tables.php",
24
                ], 'migrations');
25
            }
26
        }
27
28
        if ($this->app->runningInConsole()) {
29
            $this->commands([[
30
                Commands\AssignRole::class,
31
                Commands\CreateRole::class,
32
                Commands\CreatePermission::class,
33
                Commands\GivePermissionUser::class,
34
            ]);
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')', expecting ']'
Loading history...
35
        }
36
37
        $this->registerModelBindings();
38
39
        $permissionLoader->registerPermissions();
40
    }
41
42
    public function register()
43
    {
44
        if (isNotLumen()) {
45
            $this->mergeConfigFrom(
46
                __DIR__.'/../config/permission.php',
47
                'permission'
48
            );
49
        }
50
51
        $this->registerBladeExtensions();
52
    }
53
54
    protected function registerModelBindings()
55
    {
56
        $config = $this->app->config['permission.models'];
57
58
        $this->app->bind(PermissionContract::class, $config['permission']);
59
        $this->app->bind(RoleContract::class, $config['role']);
60
    }
61
62
    protected function registerBladeExtensions()
63
    {
64
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
65
            $bladeCompiler->directive('role', function ($arguments) {
66
                list($role, $guard) = explode(',', $arguments.',');
67
68
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
69
            });
70
            $bladeCompiler->directive('endrole', function () {
71
                return '<?php endif; ?>';
72
            });
73
74
            $bladeCompiler->directive('hasrole', function ($arguments) {
75
                list($role, $guard) = explode(',', $arguments.',');
76
77
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
78
            });
79
            $bladeCompiler->directive('endhasrole', function () {
80
                return '<?php endif; ?>';
81
            });
82
83
            $bladeCompiler->directive('hasanyrole', function ($arguments) {
84
                list($roles, $guard) = explode(',', $arguments.',');
85
86
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
87
            });
88
            $bladeCompiler->directive('endhasanyrole', function () {
89
                return '<?php endif; ?>';
90
            });
91
92
            $bladeCompiler->directive('hasallroles', function ($arguments) {
93
                list($roles, $guard) = explode(',', $arguments.',');
94
95
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
96
            });
97
            $bladeCompiler->directive('endhasallroles', function () {
98
                return '<?php endif; ?>';
99
            });
100
        });
101
    }
102
}
103