AccessServiceProvider::register()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Providers;
4
5
use App\Services\Access\Access;
6
use Illuminate\Support\Facades\Blade;
7
use Illuminate\Support\ServiceProvider;
8
9
/**
10
 * Class AccessServiceProvider.
11
 */
12
class AccessServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Indicates if loading of the provider is deferred.
16
     *
17
     * @var bool
18
     */
19
    protected $defer = false;
20
21
    /**
22
     * Package boot method.
23
     */
24
    public function boot()
25
    {
26
        $this->registerBladeExtensions();
27
    }
28
29
    /**
30
     * Register the service provider.
31
     *
32
     * @return void
33
     */
34
    public function register()
35
    {
36
        $this->registerAccess();
37
        $this->registerFacade();
38
    }
39
40
    /**
41
     * Register the application bindings.
42
     *
43
     * @return void
44
     */
45
    private function registerAccess()
46
    {
47
        $this->app->bind('access', function ($app) {
48
            return new Access($app);
0 ignored issues
show
Unused Code introduced by
The call to App\Services\Access\Access::__construct() has too many arguments starting with $app. ( Ignorable by Annotation )

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

48
            return /** @scrutinizer ignore-call */ new Access($app);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
        });
50
    }
51
52
    /**
53
     * Register the vault facade without the user having to add it to the app.php file.
54
     *
55
     * @return void
56
     */
57
    public function registerFacade()
58
    {
59
        $this->app->booting(function () {
60
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
61
            $loader->alias('Access', \App\Services\Access\Facades\Access::class);
62
        });
63
    }
64
65
    /**
66
     * Register the blade extender to use new blade sections.
67
     */
68
    protected function registerBladeExtensions()
69
    {
70
        /*
71
         * Role based blade extensions
72
         * Accepts either string of Role Name or Role ID
73
         */
74
        Blade::directive('role', function ($role) {
75
            return "<?php if (access()->hasRole({$role})): ?>";
76
        });
77
78
        /*
79
         * Accepts array of names or id's
80
         */
81
        Blade::directive('roles', function ($roles) {
82
            return "<?php if (access()->hasRoles({$roles})): ?>";
83
        });
84
85
        Blade::directive('needsroles', function ($roles) {
86
            return '<?php if (access()->hasRoles(' . $roles . ', true)): ?>';
87
        });
88
89
        /*
90
         * Permission based blade extensions
91
         * Accepts wither string of Permission Name or Permission ID
92
         */
93
        Blade::directive('permission', function ($permission) {
94
            return "<?php if (access()->allow({$permission})): ?>";
95
        });
96
97
        /*
98
         * Accepts array of names or id's
99
         */
100
        Blade::directive('permissions', function ($permissions) {
101
            return "<?php if (access()->allowMultiple({$permissions})): ?>";
102
        });
103
104
        Blade::directive('needspermissions', function ($permissions) {
105
            return '<?php if (access()->allowMultiple(' . $permissions . ', true)): ?>';
106
        });
107
108
        /*
109
         * Generic if closer to not interfere with built in blade
110
         */
111
        Blade::directive('endauth', function () {
112
            return '<?php endif; ?>';
113
        });
114
    }
115
}
116