Completed
Push — master ( 1291d4...3adad0 )
by Freek
01:22
created

PermissionServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
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
        $this->publishes([
15
            __DIR__.'/../config/permission.php' => $this->app->configPath().'/permission.php',
16
        ], 'config');
17
18
        if (! class_exists('CreatePermissionTables')) {
19
            $timestamp = date('Y_m_d_His', time());
20
21
            $this->publishes([
22
                __DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_permission_tables.php",
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
23
            ], 'migrations');
24
        }
25
26
        $this->registerModelBindings();
27
28
        $permissionLoader->registerPermissions();
29
    }
30
31
    public function register()
32
    {
33
        $this->mergeConfigFrom(
34
            __DIR__.'/../config/permission.php',
35
            'permission'
36
        );
37
38
        $this->registerBladeExtensions();
39
    }
40
41
    protected function registerModelBindings()
42
    {
43
        $config = $this->app->config['permission.models'];
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
44
45
        $this->app->bind(PermissionContract::class, $config['permission']);
46
        $this->app->bind(RoleContract::class, $config['role']);
47
    }
48
49
    protected function registerBladeExtensions()
50
    {
51
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
52 View Code Duplication
            $bladeCompiler->directive('role', function ($arguments) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
                list($role, $guard) = explode(',', $arguments.',');
54
55
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
56
            });
57
            $bladeCompiler->directive('endrole', function () {
58
                return '<?php endif; ?>';
59
            });
60
61 View Code Duplication
            $bladeCompiler->directive('hasrole', function ($arguments) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
                list($role, $guard) = explode(',', $arguments.',');
63
64
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
65
            });
66
            $bladeCompiler->directive('endhasrole', function () {
67
                return '<?php endif; ?>';
68
            });
69
70 View Code Duplication
            $bladeCompiler->directive('hasanyrole', function ($arguments) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
                list($roles, $guard) = explode(',', $arguments.',');
72
                $roles = $this->convertPipeToArray($roles);
73
74
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
75
            });
76
            $bladeCompiler->directive('endhasanyrole', function () {
77
                return '<?php endif; ?>';
78
            });
79
80 View Code Duplication
            $bladeCompiler->directive('hasallroles', function ($arguments) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
                list($roles, $guard) = explode(',', $arguments.',');
82
                $roles = $this->convertPipeToArray($roles);
83
84
                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
85
            });
86
            $bladeCompiler->directive('endhasallroles', function () {
87
                return '<?php endif; ?>';
88
            });
89
        });
90
    }
91
92
    protected function convertPipeToArray(string $pipeString)
93
    {
94
        $pipeString = trim($pipeString);
95
        if (strlen($pipeString) > 2) {
96
            $char = substr($pipeString, 0, 1);
97
            if (in_array($char, ["'", '"'])) {
98
                $endChar = substr($pipeString, -1, 1);
99
                if ($char == $endChar) {
100
                    $pipeString = '['.implode($char.','.$char, explode('|', $pipeString)).']';
101
                }
102
            }
103
        }
104
105
        return $pipeString;
106
    }
107
}
108