Completed
Push — master ( b6e961...8692a9 )
by Abdelrahman
15:05 queued 12:57
created

FortDeferredServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 158
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 9 2
A boot() 0 5 1
A registerPasswordBroker() 0 10 1
A registerVerificationBroker() 0 10 1
B registerBladeExtensions() 0 37 1
A provides() 0 8 1
A registerCommands() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Fort\Providers;
6
7
use Illuminate\Support\ServiceProvider;
8
use Illuminate\View\Compilers\BladeCompiler;
9
use Rinvex\Fort\Console\Commands\SeedCommand;
10
use Rinvex\Fort\Console\Commands\MigrateCommand;
11
use Rinvex\Fort\Console\Commands\PublishCommand;
12
use Rinvex\Fort\Console\Commands\MakeAuthCommand;
13
use Rinvex\Fort\Console\Commands\RollbackCommand;
14
use Rinvex\Fort\Services\PasswordResetBrokerManager;
15
use Rinvex\Fort\Services\EmailVerificationBrokerManager;
16
17
class FortDeferredServiceProvider extends ServiceProvider
18
{
19
    /**
20
     * The commands to be registered.
21
     *
22
     * @var array
23
     */
24
    protected $commands = [
25
        SeedCommand::class => 'command.rinvex.fort.seed',
26
        MigrateCommand::class => 'command.rinvex.fort.migrate',
27
        PublishCommand::class => 'command.rinvex.fort.publish',
28
        RollbackCommand::class => 'command.rinvex.fort.rollback',
29
    ];
30
31
    /**
32
     * Indicates if loading of the provider is deferred.
33
     *
34
     * @var bool
35
     */
36
    protected $defer = true;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function register()
42
    {
43
        // Register bindings
44
        $this->registerPasswordBroker();
45
        $this->registerVerificationBroker();
46
47
        // Register console commands
48
        ! $this->app->runningInConsole() || $this->registerCommands();
49
    }
50
51
    /**
52
     * Bootstrap the application events.
53
     *
54
     * @return void
55
     */
56
    public function boot()
57
    {
58
        // Register blade extensions
59
        $this->registerBladeExtensions();
60
    }
61
62
    /**
63
     * Register the password broker.
64
     *
65
     * @return void
66
     */
67
    protected function registerPasswordBroker()
68
    {
69
        $this->app->singleton('auth.password', function ($app) {
70
            return new PasswordResetBrokerManager($app);
71
        });
72
73
        $this->app->bind('auth.password.broker', function ($app) {
74
            return $app->make('auth.password')->broker();
75
        });
76
    }
77
78
    /**
79
     * Register the verification broker.
80
     *
81
     * @return void
82
     */
83
    protected function registerVerificationBroker()
84
    {
85
        $this->app->singleton('rinvex.fort.emailverification', function ($app) {
86
            return new EmailVerificationBrokerManager($app);
87
        });
88
89
        $this->app->bind('rinvex.fort.emailverification.broker', function ($app) {
90
            return $app->make('rinvex.fort.emailverification')->broker();
91
        });
92
    }
93
94
    /**
95
     * Register the blade extensions.
96
     *
97
     * @return void
98
     */
99
    protected function registerBladeExtensions()
100
    {
101
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
102
103
            // @role('writer') / @hasrole(['writer', 'editor'])
104
            $bladeCompiler->directive('role', function ($expression) {
105
                return "<?php if(auth()->user()->hasRole({$expression})): ?>";
106
            });
107
            $bladeCompiler->directive('endrole', function () {
108
                return '<?php endif; ?>';
109
            });
110
111
            // @hasrole('writer') / @hasrole(['writer', 'editor'])
112
            $bladeCompiler->directive('hasrole', function ($expression) {
113
                return "<?php if(auth()->user()->hasRole({$expression})): ?>";
114
            });
115
            $bladeCompiler->directive('endhasrole', function () {
116
                return '<?php endif; ?>';
117
            });
118
119
            // @hasanyrole(['writer', 'editor'])
120
            $bladeCompiler->directive('hasanyrole', function ($expression) {
121
                return "<?php if(auth()->user()->hasAnyRole({$expression})): ?>";
122
            });
123
            $bladeCompiler->directive('endhasanyrole', function () {
124
                return '<?php endif; ?>';
125
            });
126
127
            // @hasallroles(['writer', 'editor'])
128
            $bladeCompiler->directive('hasallroles', function ($expression) {
129
                return "<?php if(auth()->user()->hasAllRoles({$expression})): ?>";
130
            });
131
            $bladeCompiler->directive('endhasallroles', function () {
132
                return '<?php endif; ?>';
133
            });
134
        });
135
    }
136
137
    /**
138
     * Get the services provided by the provider.
139
     *
140
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
141
     */
142
    public function provides()
143
    {
144
        return [
145
            'auth.password',
146
            'rinvex.fort.emailverification',
147
            \Illuminate\Contracts\Debug\ExceptionHandler::class,
148
        ];
149
    }
150
151
    /**
152
     * Register console commands.
153
     *
154
     * @return void
155
     */
156
    protected function registerCommands()
157
    {
158
        if (config('rinvex.fort.boot.override_makeauth_command')) {
159
            $this->app->singleton('command.auth.make', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
                return new MakeAuthCommand();
161
            });
162
            $this->commands('command.auth.make');
163
        }
164
165
        // Register artisan commands
166
        foreach ($this->commands as $key => $value) {
167
            $this->app->singleton($value, function ($app) use ($key) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
168
                return new $key();
169
            });
170
        }
171
172
        $this->commands(array_values($this->commands));
173
    }
174
}
175